]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG/EMCAL/AliEmcalSetupTask.cxx
expand particle map when needed
[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   fIsInit(kFALSE),
33   fLocalOcdb(),
34   fLocalOcdbStor()
35 {
36   // Constructor.
37 }
38
39 //________________________________________________________________________
40 AliEmcalSetupTask::AliEmcalSetupTask(const char *name) : 
41   AliAnalysisTaskSE(name),
42   fOcdbPath("uselocal"),
43   fOadbPath("$ALICE_ROOT/OADB/EMCAL"),
44   fGeoPath("$ALICE_ROOT/OADB/EMCAL"),
45   fObjs("GRP ITS TPC TRD EMCAL"),
46   fIsInit(kFALSE),
47   fLocalOcdb(),
48   fLocalOcdbStor()
49 {
50   // Constructor.
51   fBranchNames = "ESD:AliESDHeader.,AliESDRun.";
52 }
53
54 //________________________________________________________________________
55 AliEmcalSetupTask::~AliEmcalSetupTask()
56 {
57   // Destructor.
58 }
59
60 //________________________________________________________________________
61 void AliEmcalSetupTask::ConnectInputData(Option_t *option)
62 {
63   // Connect input data
64
65   AliAnalysisTaskSE::ConnectInputData(option);
66
67   if (fOcdbPath.Length()==0)
68     return;
69
70   AliCDBManager *man = AliCDBManager::Instance();
71   if (man->IsDefaultStorageSet()) 
72     return;
73
74   if (fIsInit)
75     return;
76
77   AliAnalysisManager *am = AliAnalysisManager::GetAnalysisManager();
78   if (!am)
79     return;
80
81   TObjArray *tasks = am->GetTasks();
82   if (!tasks)
83     return;
84
85   AliTender *tender = 0;
86   for (Int_t i=0; i<tasks->GetEntries(); ++i) {
87     tender = dynamic_cast<AliTender*>(tasks->At(i));
88     if (tender)
89       break;
90   }
91
92   if (!tender)
93     return;
94
95   if (fOcdbPath != "uselocal") {
96     tender->SetDefaultCDBStorage(fOcdbPath);
97     return;
98   }
99
100   Int_t runno = AliAnalysisManager::GetAnalysisManager()->GetRunFromPath();
101   if (runno<=0) {
102     AliWarning(Form("Disabling tender, ignore possible message from tender below"));
103     tender->SetDefaultCDBStorage("donotuse");
104     return;
105   }
106
107   AliWarning(Form("Intercepting tender for run %d, ignore possible message from tender below", runno));
108   Setup(runno);
109   tender->SetDefaultCDBStorage(fLocalOcdbStor);
110 }
111
112 //________________________________________________________________________
113 void AliEmcalSetupTask::UserExec(Option_t *) 
114 {
115   // Main loop, called for each event.
116
117   if (fIsInit)
118     return;
119
120   AliAnalysisManager *am = AliAnalysisManager::GetAnalysisManager();
121   if (!am) {
122     AliError("Manager zero, returning");
123     return;
124   }
125
126   am->LoadBranch("AliESDRun.");
127   am->LoadBranch("AliESDHeader.");
128
129   Int_t runno = InputEvent()->GetRunNumber();
130   Setup(runno);
131 }
132
133 //________________________________________________________________________
134 void AliEmcalSetupTask::Setup(Int_t runno) 
135 {
136   // Setup everything
137
138   // Setup AliEMCALGeometry corresponding to year
139   TString geoname("EMCAL_COMPLETE12SMV1");
140   Int_t year = 2013;
141   if (runno>0 && runno<=139517) {
142     year = 2010;
143     geoname = "EMCAL_FIRSTYEARV1";
144   } else if ((runno>139517) && (runno<=170593)) {
145     year = 2011;
146     geoname = "EMCAL_COMPLETEV1";
147   } else if ((runno>170593) && (runno<=193766)) {
148     year = 2012;
149   }
150   AliEMCALGeometry *geom = AliEMCALGeometry::GetInstance(geoname);
151   if (!geom) {
152     AliFatal(Form("Can not create geometry: %s",geoname.Data()));
153     return;
154   }
155
156   if (runno<=0)
157     return;
158
159   // Setup CDB manager
160   AliCDBManager *man = 0;
161   man = AliCDBManager::Instance();
162   if (man->IsDefaultStorageSet()) {
163     AliInfo(Form("Default OCDB storage already set"));
164   } else {
165     if (fOcdbPath.Length()==0) {
166       man = 0; // do not use OCDB
167     } else if (fOcdbPath != "uselocal") {
168       AliInfo(Form("Setting up OCDB to point to %s",fOcdbPath.Data()));
169       man->SetDefaultStorage(fOcdbPath);
170     } else { // use local copy of OCDB
171       TString tmpdir=gSystem->WorkingDirectory();
172       if (gSystem->AccessPathName(tmpdir))
173         tmpdir = "/tmp";
174       tmpdir+="/";
175       tmpdir+=gSystem->GetUid();
176       tmpdir+="-";
177       TDatime t;
178       tmpdir+=t.Get();
179       tmpdir+="-";
180       Int_t counter = 0;
181       fLocalOcdb = tmpdir;
182       fLocalOcdb += Form("%d%d%d",gRandom->Integer(999999999),gRandom->Integer(999999999),gRandom->Integer(999999999));
183       while (!gSystem->AccessPathName(fLocalOcdb)) {
184         fLocalOcdb = tmpdir;
185         fLocalOcdb += Form("%d%d%d",gRandom->Integer(999999999),gRandom->Integer(999999999),gRandom->Integer(999999999));
186         counter++;
187         if (counter>100) {
188           AliFatal(Form("Could not create local directory for OCDB at %s",tmpdir.Data()));
189         }
190       }
191       gSystem->MakeDirectory(fLocalOcdb);
192       TString filename(Form("$ALICE_ROOT/PWG/EMCAL/data/%d.dat",year));
193       TString cmd(Form("cd %s && tar -xf %s",fLocalOcdb.Data(),filename.Data()));
194       Int_t ret = gSystem->Exec(cmd);
195       if (ret==0) {
196         TString locdb("local://");
197         locdb+=fLocalOcdb;
198         locdb+="/";
199         locdb+=year;
200         AliInfo(Form("Setting up local OCDB at %s",locdb.Data()));
201         man->SetDefaultStorage(locdb);
202         fLocalOcdbStor = locdb;
203       } else {
204         AliFatal(Form("Could not set up local OCDB at %s",fLocalOcdb.Data()));
205       }
206     }
207   }
208   
209   // Load geometry from OCDB 
210   if (man) {
211     if (man->GetRun()!=runno)
212       man->SetRun(runno);
213     AliInfo(Form("Loading grp data from OCDB for run %d", runno));
214     AliGRPManager GRPManager;
215     GRPManager.ReadGRPEntry();
216     GRPManager.SetMagField();
217     AliInfo(Form("Loading geometry from OCDB"));
218     AliGeomManager::LoadGeometry();
219     if (!fObjs.IsNull())
220       AliGeomManager::ApplyAlignObjsFromCDB(fObjs);
221   }
222
223   // Load geometry from file (does not use misalignment of ITS/TPC!)
224   TGeoManager *geo = AliGeomManager::GetGeometry();
225   if (!geo && fGeoPath.Length()>0) {
226     TString fname(gSystem->ExpandPathName(Form("%s/geometry_%d.root", fGeoPath.Data(), year)));
227     if (gSystem->AccessPathName(fname)==0) {
228       AliInfo(Form("Loading geometry from file %s (should be avoided!)", fname.Data()));
229       AliGeomManager::LoadGeometry(fname);
230       geo = AliGeomManager::GetGeometry();
231     }
232   }
233
234   // Lock geometry
235   if (geo) {
236     AliInfo(Form("Locking geometry"));
237     geo->LockGeometry();
238   }
239
240   // Construct field map
241   if (!TGeoGlobalMagField::Instance()->GetField()) { 
242     InputEvent()->InitMagneticField();
243   }
244
245   // Apply mis-alignment matrices from OADB
246   if (fOadbPath.Length()>0) {
247     AliOADBContainer emcalgeoCont(Form("emcal"));
248     emcalgeoCont.InitFromFile(Form("%s/EMCALlocal2master.root",fOadbPath.Data()),
249                               Form("AliEMCALgeo"));
250     TObjArray *mobj=dynamic_cast<TObjArray*>(emcalgeoCont.GetObject(runno,"EmcalMatrices"));
251     if (mobj) {
252       for(Int_t mod=0; mod < (geom->GetEMCGeometry())->GetNumberOfSuperModules(); mod++){
253         //AliInfo(Form("Misalignment matrix %d", mod));
254         geom->SetMisalMatrix((TGeoHMatrix*) mobj->At(mod),mod);
255       } 
256     }
257   }
258
259   fIsInit = kTRUE;
260 }
261
262 //________________________________________________________________________
263 void AliEmcalSetupTask::Terminate(Option_t *) 
264 {
265   // Called at the end.
266
267   if (fLocalOcdb.Length()>0) {
268     TString cmd(Form("rm -rf %s", fLocalOcdb.Data()));
269     gSystem->Exec(cmd);
270   }
271 }