]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG/EMCAL/AliEmcalSetupTask.cxx
Merge branch 'master' of https://git.cern.ch/reps/AliRoot
[u/mrichter/AliRoot.git] / PWG / EMCAL / AliEmcalSetupTask.cxx
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
23 ClassImp(AliEmcalSetupTask)
24
25 //________________________________________________________________________
26 AliEmcalSetupTask::AliEmcalSetupTask() : 
27   AliAnalysisTaskSE(),
28   fOcdbPath("uselocal"),
29   fOadbPath("$ALICE_ROOT/OADB/EMCAL"),
30   fGeoPath("$ALICE_ROOT/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 //________________________________________________________________________
41 AliEmcalSetupTask::AliEmcalSetupTask(const char *name) : 
42   AliAnalysisTaskSE(name),
43   fOcdbPath("uselocal"),
44   fOadbPath("$ALICE_ROOT/OADB/EMCAL"),
45   fGeoPath("$ALICE_ROOT/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 //________________________________________________________________________
57 AliEmcalSetupTask::~AliEmcalSetupTask()
58 {
59   // Destructor.
60 }
61
62 //________________________________________________________________________
63 void AliEmcalSetupTask::ConnectInputData(Option_t *option)
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 //________________________________________________________________________
115 void AliEmcalSetupTask::UserExec(Option_t *) 
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   // Setup AliEMCALGeometry corresponding to year
141   TString geoname("EMCAL_COMPLETE12SMV1");
142   Int_t year = 2013;
143   if (runno>0 && runno<=139517) {
144     year = 2010;
145     geoname = "EMCAL_FIRSTYEARV1";
146   } else if ((runno>139517) && (runno<=170593)) {
147     year = 2011;
148     geoname = "EMCAL_COMPLETEV1";
149   } else if ((runno>170593) && (runno<=193766)) {
150     year = 2012;
151     geoname = "EMCAL_COMPLETE12SMV1";
152   } else if ((runno>193766) && (runno <= 199161)) {
153     geoname = "EMCAL_COMPLETE12SMV1";
154     year = 2013;
155   } else if (runno>199161) { //MV: is this the last run of run 1?
156     geoname = "EMCAL_COMPLETE12SMV1_DCAL_8SM";
157     year = 2015;
158   }
159   AliEMCALGeometry *geom = AliEMCALGeometry::GetInstance(geoname);
160   if (!geom) {
161     AliFatal(Form("Can not create geometry: %s",geoname.Data()));
162     return;
163   }
164
165   if (runno<0)  // Run number 0 can occur for MC
166     return;
167
168   // Setup CDB manager
169   AliCDBManager *man = 0;
170   if (!fNoOCDB) {
171     man = AliCDBManager::Instance();
172     if (!man)
173       AliFatal(Form("Did not get pointer to CDB manager"));
174
175     if (man->IsDefaultStorageSet()) {
176       AliInfo(Form("Default OCDB storage already set"));
177     } else {
178       if (fOcdbPath.Length()==0) {
179         man = 0; // do not use OCDB
180       } else if (fOcdbPath != "uselocal") {
181         AliInfo(Form("Setting up OCDB to point to %s",fOcdbPath.Data()));
182         man->SetDefaultStorage(fOcdbPath);
183       } else { // use local copy of OCDB
184         TString tmpdir=gSystem->WorkingDirectory();
185         if (gSystem->AccessPathName(tmpdir))
186           tmpdir = "/tmp";
187         tmpdir+="/";
188         tmpdir+=gSystem->GetUid();
189         tmpdir+="-";
190         TDatime t;
191         tmpdir+=t.Get();
192         tmpdir+="-";
193         Int_t counter = 0;
194         fLocalOcdb = tmpdir;
195         fLocalOcdb += Form("%d%d%d",gRandom->Integer(999999999),gRandom->Integer(999999999),gRandom->Integer(999999999));
196         while (!gSystem->AccessPathName(fLocalOcdb)) {
197           fLocalOcdb = tmpdir;
198           fLocalOcdb += Form("%d%d%d",gRandom->Integer(999999999),gRandom->Integer(999999999),gRandom->Integer(999999999));
199           counter++;
200           if (counter>100) {
201             AliFatal(Form("Could not create local directory for OCDB at %s",tmpdir.Data()));
202           }
203         }
204         gSystem->MakeDirectory(fLocalOcdb);
205         TString filename(Form("$ALICE_ROOT/PWG/EMCAL/data/%d.dat",year));
206         TString cmd(Form("cd %s && tar -xf %s",fLocalOcdb.Data(),filename.Data()));
207         Int_t ret = gSystem->Exec(cmd);
208         if (ret==0) {
209           TString locdb("local://");
210           locdb+=fLocalOcdb;
211           locdb+="/";
212           locdb+=year;
213           AliInfo(Form("Setting up local OCDB at %s",locdb.Data()));
214           man->SetDefaultStorage(locdb);
215           fLocalOcdbStor = locdb;
216         } else {
217           AliFatal(Form("Could not set up local OCDB at %s",fLocalOcdb.Data()));
218         }
219       }
220     }
221   }
222
223   // Load geometry from OCDB 
224   if (man) {
225     if (man->GetRun()!=runno)
226       man->SetRun(runno);
227     AliInfo(Form("Loading grp data from OCDB for run %d", runno));
228     AliGRPManager GRPManager;
229     GRPManager.ReadGRPEntry();
230     GRPManager.SetMagField();
231     AliInfo(Form("Loading geometry from OCDB"));
232     AliGeomManager::LoadGeometry();
233     if (!fObjs.IsNull())
234       AliGeomManager::ApplyAlignObjsFromCDB(fObjs);
235   }
236
237   // Load geometry from file (does not use misalignment of ITS/TPC!)
238   TGeoManager *geo = AliGeomManager::GetGeometry();
239   if (!geo && fGeoPath.Length()>0) {
240     TString fname(gSystem->ExpandPathName(Form("%s/geometry_%d.root", fGeoPath.Data(), year)));
241     if (gSystem->AccessPathName(fname)==0) {
242       AliInfo(Form("Loading geometry from file %s (should be avoided!)", fname.Data()));
243       AliGeomManager::LoadGeometry(fname);
244       geo = AliGeomManager::GetGeometry();
245     }
246   }
247
248   // Lock geometry
249   if (geo) {
250     AliInfo(Form("Locking geometry"));
251     geo->LockGeometry();
252   }
253
254   // Construct field map
255   if (!TGeoGlobalMagField::Instance()->GetField()) { 
256     InputEvent()->InitMagneticField();
257   }
258
259   // Apply mis-alignment matrices from OADB
260   if (fOadbPath.Length()>0) {
261     AliOADBContainer emcalgeoCont(Form("emcal"));
262     emcalgeoCont.InitFromFile(Form("%s/EMCALlocal2master.root",fOadbPath.Data()),
263                               Form("AliEMCALgeo"));
264     TObjArray *mobj=dynamic_cast<TObjArray*>(emcalgeoCont.GetObject(runno,"EmcalMatrices"));
265     if (mobj) {
266       for(Int_t mod=0; mod < (geom->GetEMCGeometry())->GetNumberOfSuperModules(); mod++){
267         //AliInfo(Form("Misalignment matrix %d", mod));
268         geom->SetMisalMatrix((TGeoHMatrix*) mobj->At(mod),mod);
269       } 
270     }
271   }
272
273   fIsInit = kTRUE;
274 }
275
276 //________________________________________________________________________
277 void AliEmcalSetupTask::Terminate(Option_t *) 
278 {
279   // Called at the end.
280
281   if (fLocalOcdb.Length()>0) {
282     TString cmd(Form("rm -rf %s", fLocalOcdb.Data()));
283     gSystem->Exec(cmd);
284   }
285 }