AliPhysics  vAN-20150630 (513c479)
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Macros
AliEmcalSetupTask.cxx
Go to the documentation of this file.
1 // $Id$
2 //
3 // Task to setup emcal related global objects.
4 //
5 // Author: C.Loizides
6 
7 #include "AliEmcalSetupTask.h"
8 #include <TClonesArray.h>
9 #include <TGeoGlobalMagField.h>
10 #include <TGeoManager.h>
11 #include <TRandom.h>
12 #include "AliAODEvent.h"
13 #include "AliAnalysisManager.h"
14 #include "AliCDBManager.h"
15 #include "AliEMCALGeometry.h"
16 #include "AliESDEvent.h"
17 #include "AliGRPManager.h"
18 #include "AliGeomManager.h"
19 #include "AliMagF.h"
20 #include "AliOADBContainer.h"
21 #include "AliTender.h"
22 
24 
25 //________________________________________________________________________
27  AliAnalysisTaskSE(),
28  fOcdbPath("uselocal"),
29  fOadbPath("$ALICE_PHYSICS/OADB/EMCAL"),
30  fGeoPath("$ALICE_PHYSICS/OADB/EMCAL"),
31  fObjs("GRP ITS TPC TRD EMCAL"),
32  fNoOCDB(kFALSE),
33  fIsInit(kFALSE),
34  fLocalOcdb(),
35  fLocalOcdbStor()
36 {
37  // Constructor.
38 }
39 
40 //________________________________________________________________________
42  AliAnalysisTaskSE(name),
43  fOcdbPath("uselocal"),
44  fOadbPath("$ALICE_PHYSICS/OADB/EMCAL"),
45  fGeoPath("$ALICE_PHYSICS/OADB/EMCAL"),
46  fObjs("GRP ITS TPC TRD EMCAL"),
47  fNoOCDB(kFALSE),
48  fIsInit(kFALSE),
49  fLocalOcdb(),
50  fLocalOcdbStor()
51 {
52  // Constructor.
53  fBranchNames = "ESD:AliESDHeader.,AliESDRun.";
54 }
55 
56 //________________________________________________________________________
58 {
59  // Destructor.
60 }
61 
62 //________________________________________________________________________
64 {
65  // Connect input data
66 
67  AliAnalysisTaskSE::ConnectInputData(option);
68 
69  if (fOcdbPath.Length()==0)
70  return;
71 
72  AliCDBManager *man = AliCDBManager::Instance();
73  if (man->IsDefaultStorageSet())
74  return;
75 
76  if (fIsInit)
77  return;
78 
79  AliAnalysisManager *am = AliAnalysisManager::GetAnalysisManager();
80  if (!am)
81  return;
82 
83  TObjArray *tasks = am->GetTasks();
84  if (!tasks)
85  return;
86 
87  AliTender *tender = 0;
88  for (Int_t i=0; i<tasks->GetEntries(); ++i) {
89  tender = dynamic_cast<AliTender*>(tasks->At(i));
90  if (tender)
91  break;
92  }
93 
94  if (!tender)
95  return;
96 
97  if (fOcdbPath != "uselocal") {
98  tender->SetDefaultCDBStorage(fOcdbPath);
99  return;
100  }
101 
102  Int_t runno = AliAnalysisManager::GetAnalysisManager()->GetRunFromPath();
103  if (runno<=0) {
104  AliWarning(Form("Disabling tender, ignore possible message from tender below"));
105  tender->SetDefaultCDBStorage("donotuse");
106  return;
107  }
108 
109  AliWarning(Form("Intercepting tender for run %d, ignore possible message from tender below", runno));
110  Setup(runno);
111  tender->SetDefaultCDBStorage(fLocalOcdbStor);
112 }
113 
114 //________________________________________________________________________
116 {
117  // Main loop, called for each event.
118 
119  if (fIsInit)
120  return;
121 
122  AliAnalysisManager *am = AliAnalysisManager::GetAnalysisManager();
123  if (!am) {
124  AliError("Manager zero, returning");
125  return;
126  }
127 
128  am->LoadBranch("AliESDRun.");
129  am->LoadBranch("AliESDHeader.");
130 
131  Int_t runno = InputEvent()->GetRunNumber();
132  Setup(runno);
133 }
134 
135 //________________________________________________________________________
136 void AliEmcalSetupTask::Setup(Int_t runno)
137 {
138  // Setup everything
139 
140  if ( runno < 0 ) // Run number 0 can occur for MC
141  return;
142 
143  AliEMCALGeometry *geom = AliEMCALGeometry::GetInstanceFromRunNumber(runno);
144 
145  if (!geom) {
146  AliFatal("Can not create geometry!");
147  return;
148  }
149 
150  // Setup AliEMCALGeometry corresponding to year
151  Int_t year = 2013;
152  if (runno>0 && runno<=139517) {
153  year = 2010;
154  } else if ((runno > 139517) && (runno <= 170593)) {
155  year = 2011;
156  } else if ((runno > 170593) && (runno <= 193766)) {
157  year = 2012;
158  } else if ((runno > 193766) && (runno <= 199161)) {
159  year = 2013;
160  } else if ( runno > 199161) { //MV: is this the last run of run 1?
161  year = 2015;
162  }
163 
164  // Setup CDB manager
165  AliCDBManager *man = 0;
166  if (!fNoOCDB) {
167  man = AliCDBManager::Instance();
168  if (!man)
169  AliFatal(Form("Did not get pointer to CDB manager"));
170 
171  if (man->IsDefaultStorageSet()) {
172  AliInfo(Form("Default OCDB storage already set"));
173  } else {
174  if (fOcdbPath.Length()==0) {
175  man = 0; // do not use OCDB
176  } else if (fOcdbPath != "uselocal") {
177  AliInfo(Form("Setting up OCDB to point to %s",fOcdbPath.Data()));
178  man->SetDefaultStorage(fOcdbPath);
179  } else { // use local copy of OCDB
180  TString tmpdir=gSystem->WorkingDirectory();
181  if (gSystem->AccessPathName(tmpdir))
182  tmpdir = "/tmp";
183  tmpdir+="/";
184  tmpdir+=gSystem->GetUid();
185  tmpdir+="-";
186  TDatime t;
187  tmpdir+=t.Get();
188  tmpdir+="-";
189  Int_t counter = 0;
190  fLocalOcdb = tmpdir;
191  fLocalOcdb += Form("%d%d%d",gRandom->Integer(999999999),gRandom->Integer(999999999),gRandom->Integer(999999999));
192  while (!gSystem->AccessPathName(fLocalOcdb)) {
193  fLocalOcdb = tmpdir;
194  fLocalOcdb += Form("%d%d%d",gRandom->Integer(999999999),gRandom->Integer(999999999),gRandom->Integer(999999999));
195  counter++;
196  if (counter>100) {
197  AliFatal(Form("Could not create local directory for OCDB at %s",tmpdir.Data()));
198  }
199  }
200  gSystem->MakeDirectory(fLocalOcdb);
201  TString filename(Form("$ALICE_PHYSICS/PWG/EMCAL/data/%d.dat",year));
202  TString cmd(Form("cd %s && tar -xf %s",fLocalOcdb.Data(),filename.Data()));
203  Int_t ret = gSystem->Exec(cmd);
204  if (ret==0) {
205  TString locdb("local://");
206  locdb+=fLocalOcdb;
207  locdb+="/";
208  locdb+=year;
209  AliInfo(Form("Setting up local OCDB at %s",locdb.Data()));
210  man->SetDefaultStorage(locdb);
211  fLocalOcdbStor = locdb;
212  } else {
213  AliFatal(Form("Could not set up local OCDB at %s",fLocalOcdb.Data()));
214  }
215  }
216  }
217  }
218 
219  // Load geometry from OCDB
220  if (man) {
221  if (man->GetRun()!=runno)
222  man->SetRun(runno);
223  AliInfo(Form("Loading grp data from OCDB for run %d", runno));
224  AliGRPManager GRPManager;
225  GRPManager.ReadGRPEntry();
226  GRPManager.SetMagField();
227  AliInfo(Form("Loading geometry from OCDB"));
228  AliGeomManager::LoadGeometry();
229  if (!fObjs.IsNull())
230  AliGeomManager::ApplyAlignObjsFromCDB(fObjs);
231  }
232 
233  // Load geometry from file (does not use misalignment of ITS/TPC!)
234  TGeoManager *geo = AliGeomManager::GetGeometry();
235  if (!geo && fGeoPath.Length()>0) {
236  TString fname(gSystem->ExpandPathName(Form("%s/geometry_%d.root", fGeoPath.Data(), year)));
237  if (gSystem->AccessPathName(fname)==0) {
238  AliInfo(Form("Loading geometry from file %s (should be avoided!)", fname.Data()));
239  AliGeomManager::LoadGeometry(fname);
240  geo = AliGeomManager::GetGeometry();
241  }
242  }
243 
244  // Lock geometry
245  if (geo) {
246  AliInfo(Form("Locking geometry"));
247  geo->LockGeometry();
248  }
249 
250  // Construct field map
251  if (!TGeoGlobalMagField::Instance()->GetField()) {
252  InputEvent()->InitMagneticField();
253  }
254 
255  // Apply mis-alignment matrices from OADB
256  if (fOadbPath.Length()>0) {
257  AliOADBContainer emcalgeoCont(Form("emcal"));
258  emcalgeoCont.InitFromFile(Form("%s/EMCALlocal2master.root",fOadbPath.Data()),
259  Form("AliEMCALgeo"));
260  TObjArray *mobj=dynamic_cast<TObjArray*>(emcalgeoCont.GetObject(runno,"EmcalMatrices"));
261  if (mobj) {
262  for(Int_t mod=0; mod < (geom->GetEMCGeometry())->GetNumberOfSuperModules(); mod++){
263  //AliInfo(Form("Misalignment matrix %d", mod));
264  geom->SetMisalMatrix((TGeoHMatrix*) mobj->At(mod),mod);
265  }
266  }
267  }
268 
269  fIsInit = kTRUE;
270 }
271 
272 //________________________________________________________________________
274 {
275  // Called at the end.
276 
277  if (fLocalOcdb.Length()>0) {
278  TString cmd(Form("rm -rf %s", fLocalOcdb.Data()));
279  gSystem->Exec(cmd);
280  }
281 }
TString fLocalOcdbStor
directory path to local ocdb
ClassImp(AliEmcalSetupTask) AliEmcalSetupTask
void ConnectInputData(Option_t *option="")
void Terminate(Option_t *option)
void Setup(Int_t runno)
void UserExec(Option_t *option)
TString fLocalOcdb
=true then already initialized