]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG/EMCAL/AliEmcalSetupTask.cxx
implemented possibility to switch off messing with OCDB
[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   }
152   AliEMCALGeometry *geom = AliEMCALGeometry::GetInstance(geoname);
153   if (!geom) {
154     AliFatal(Form("Can not create geometry: %s",geoname.Data()));
155     return;
156   }
157
158   if (runno<=0)
159     return;
160
161   // Setup CDB manager
162   AliCDBManager *man = 0;
163   if (!fNoOCDB) {
164     man = AliCDBManager::Instance();
165     if (!man)
166       AliFatal(Form("Did not get pointer to CDB manager"));
167
168     if (man->IsDefaultStorageSet()) {
169       AliInfo(Form("Default OCDB storage already set"));
170     } else {
171       if (fOcdbPath.Length()==0) {
172         man = 0; // do not use OCDB
173       } else if (fOcdbPath != "uselocal") {
174         AliInfo(Form("Setting up OCDB to point to %s",fOcdbPath.Data()));
175         man->SetDefaultStorage(fOcdbPath);
176       } else { // use local copy of OCDB
177         TString tmpdir=gSystem->WorkingDirectory();
178         if (gSystem->AccessPathName(tmpdir))
179           tmpdir = "/tmp";
180         tmpdir+="/";
181         tmpdir+=gSystem->GetUid();
182         tmpdir+="-";
183         TDatime t;
184         tmpdir+=t.Get();
185         tmpdir+="-";
186         Int_t counter = 0;
187         fLocalOcdb = tmpdir;
188         fLocalOcdb += Form("%d%d%d",gRandom->Integer(999999999),gRandom->Integer(999999999),gRandom->Integer(999999999));
189         while (!gSystem->AccessPathName(fLocalOcdb)) {
190           fLocalOcdb = tmpdir;
191           fLocalOcdb += Form("%d%d%d",gRandom->Integer(999999999),gRandom->Integer(999999999),gRandom->Integer(999999999));
192           counter++;
193           if (counter>100) {
194             AliFatal(Form("Could not create local directory for OCDB at %s",tmpdir.Data()));
195           }
196         }
197         gSystem->MakeDirectory(fLocalOcdb);
198         TString filename(Form("$ALICE_ROOT/PWG/EMCAL/data/%d.dat",year));
199         TString cmd(Form("cd %s && tar -xf %s",fLocalOcdb.Data(),filename.Data()));
200         Int_t ret = gSystem->Exec(cmd);
201         if (ret==0) {
202           TString locdb("local://");
203           locdb+=fLocalOcdb;
204           locdb+="/";
205           locdb+=year;
206           AliInfo(Form("Setting up local OCDB at %s",locdb.Data()));
207           man->SetDefaultStorage(locdb);
208           fLocalOcdbStor = locdb;
209         } else {
210           AliFatal(Form("Could not set up local OCDB at %s",fLocalOcdb.Data()));
211         }
212       }
213     }
214   }
215
216   // Load geometry from OCDB 
217   if (man) {
218     if (man->GetRun()!=runno)
219       man->SetRun(runno);
220     AliInfo(Form("Loading grp data from OCDB for run %d", runno));
221     AliGRPManager GRPManager;
222     GRPManager.ReadGRPEntry();
223     GRPManager.SetMagField();
224     AliInfo(Form("Loading geometry from OCDB"));
225     AliGeomManager::LoadGeometry();
226     if (!fObjs.IsNull())
227       AliGeomManager::ApplyAlignObjsFromCDB(fObjs);
228   }
229
230   // Load geometry from file (does not use misalignment of ITS/TPC!)
231   TGeoManager *geo = AliGeomManager::GetGeometry();
232   if (!geo && fGeoPath.Length()>0) {
233     TString fname(gSystem->ExpandPathName(Form("%s/geometry_%d.root", fGeoPath.Data(), year)));
234     if (gSystem->AccessPathName(fname)==0) {
235       AliInfo(Form("Loading geometry from file %s (should be avoided!)", fname.Data()));
236       AliGeomManager::LoadGeometry(fname);
237       geo = AliGeomManager::GetGeometry();
238     }
239   }
240
241   // Lock geometry
242   if (geo) {
243     AliInfo(Form("Locking geometry"));
244     geo->LockGeometry();
245   }
246
247   // Construct field map
248   if (!TGeoGlobalMagField::Instance()->GetField()) { 
249     InputEvent()->InitMagneticField();
250   }
251
252   // Apply mis-alignment matrices from OADB
253   if (fOadbPath.Length()>0) {
254     AliOADBContainer emcalgeoCont(Form("emcal"));
255     emcalgeoCont.InitFromFile(Form("%s/EMCALlocal2master.root",fOadbPath.Data()),
256                               Form("AliEMCALgeo"));
257     TObjArray *mobj=dynamic_cast<TObjArray*>(emcalgeoCont.GetObject(runno,"EmcalMatrices"));
258     if (mobj) {
259       for(Int_t mod=0; mod < (geom->GetEMCGeometry())->GetNumberOfSuperModules(); mod++){
260         //AliInfo(Form("Misalignment matrix %d", mod));
261         geom->SetMisalMatrix((TGeoHMatrix*) mobj->At(mod),mod);
262       } 
263     }
264   }
265
266   fIsInit = kTRUE;
267 }
268
269 //________________________________________________________________________
270 void AliEmcalSetupTask::Terminate(Option_t *) 
271 {
272   // Called at the end.
273
274   if (fLocalOcdb.Length()>0) {
275     TString cmd(Form("rm -rf %s", fLocalOcdb.Data()));
276     gSystem->Exec(cmd);
277   }
278 }