]> 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
22 ClassImp(AliEmcalSetupTask)
23
24 //________________________________________________________________________
25 AliEmcalSetupTask::AliEmcalSetupTask() : 
26   AliAnalysisTaskSE(),
27   fOcdbPath("uselocal"),
28   fOadbPath("$ALICE_ROOT/OADB/EMCAL"),
29   fGeoPath("$ALICE_ROOT/OADB/EMCAL"),
30   fObjs("GRP ITS TPC TRD EMCAL"),
31   fIsInit(kFALSE),
32   fLocalOcdb()
33 {
34   // Constructor.
35 }
36
37 //________________________________________________________________________
38 AliEmcalSetupTask::AliEmcalSetupTask(const char *name) : 
39   AliAnalysisTaskSE(name),
40   fOcdbPath("uselocal"),
41   fOadbPath("$ALICE_ROOT/OADB/EMCAL"),
42   fGeoPath("$ALICE_ROOT/OADB/EMCAL"),
43   fObjs("GRP ITS TPC TRD EMCAL"),
44   fIsInit(kFALSE),
45   fLocalOcdb()
46 {
47   // Constructor.
48   fBranchNames = "ESD:AliESDHeader.,AliESDRun.";
49 }
50
51 //________________________________________________________________________
52 AliEmcalSetupTask::~AliEmcalSetupTask()
53 {
54   // Destructor.
55 }
56
57 //________________________________________________________________________
58 void AliEmcalSetupTask::UserExec(Option_t *) 
59 {
60   // Main loop, called for each event.
61
62   if (fIsInit)
63     return;
64
65   AliAnalysisManager *am = AliAnalysisManager::GetAnalysisManager();
66   if (!am) {
67     AliError("Manager zero, returning");
68     return;
69   }
70
71   am->LoadBranch("AliESDRun.");
72   am->LoadBranch("AliESDHeader.");
73
74   // Setup AliEMCALGeometry corresponding to year
75   Int_t runno = InputEvent()->GetRunNumber();
76   TString geoname("EMCAL_COMPLETE12SMV1");
77   Int_t year = 2013;
78   if (runno<=139517) {
79     year = 2010;
80     geoname = "EMCAL_FIRSTYEARV1";
81   } else if ((runno>139517) && (runno<=170593)) {
82     year = 2011;
83     geoname = "EMCAL_COMPLETEV1";
84   } else if ((runno>170593) && (runno<=193766)) {
85     year = 2012;
86   }
87   AliEMCALGeometry *geom = AliEMCALGeometry::GetInstance(geoname);
88   if (!geom) {
89     AliFatal(Form("Can not create geometry: %s",geoname.Data()));
90     return;
91   }
92
93   // Setup CDB manager
94   AliCDBManager *man = 0;
95   man = AliCDBManager::Instance();
96   if (man->IsDefaultStorageSet()) {
97     AliInfo(Form("Default OCDB storage already set"));
98   } else {
99     if (fOcdbPath.Length()==0) {
100       man = 0; // do not use OCDB
101     } else if (fOcdbPath != "uselocal") {
102       AliInfo(Form("Setting up OCDB to point to %s",fOcdbPath.Data()));
103       man->SetDefaultStorage(fOcdbPath);
104     } else { // use local copy of OCDB
105       TString tmpdir=gSystem->WorkingDirectory();
106       if (gSystem->AccessPathName(tmpdir))
107         tmpdir = "/tmp";
108       tmpdir+="/";
109       tmpdir+=gSystem->GetUid();
110       tmpdir+="-";
111       TDatime t;
112       tmpdir+=t.Get();
113       tmpdir+="-";
114       Int_t counter = 0;
115       fLocalOcdb = tmpdir;
116       fLocalOcdb += Form("%d%d%d%",gRandom->Integer(999999999),gRandom->Integer(999999999),gRandom->Integer(999999999));
117       while (!gSystem->AccessPathName(fLocalOcdb)) {
118         fLocalOcdb = tmpdir;
119         fLocalOcdb += Form("%d%d%d%",gRandom->Integer(999999999),gRandom->Integer(999999999),gRandom->Integer(999999999));
120         counter++;
121         if (counter>100) {
122           AliFatal(Form("Could not create local directory for OCDB at %s",tmpdir.Data()));
123         }
124       }
125       gSystem->MakeDirectory(fLocalOcdb);
126       TString filename(Form("$ALICE_ROOT/PWG/EMCAL/data/%d.dat",year));
127       TString cmd(Form("cd %s && tar -xf %s",fLocalOcdb.Data(),filename.Data()));
128       Int_t ret = gSystem->Exec(cmd);
129       if (ret==0) {
130         TString locdb("local://");
131         locdb+=fLocalOcdb;
132         locdb+="/";
133         locdb+=year;
134         AliInfo(Form("Setting up local OCDB at %s",locdb.Data()));
135         man->SetDefaultStorage(locdb);
136       } else {
137         AliFatal(Form("Could not set up local OCDB at %s",fLocalOcdb.Data()));
138       }
139     }
140   }
141   
142   // Load geometry from OCDB 
143   if (man) {
144     if (man->GetRun()!=runno)
145       man->SetRun(runno);
146     AliInfo(Form("Loading grp data from OCDB for run %d", runno));
147     AliGRPManager GRPManager;
148     GRPManager.ReadGRPEntry();
149     GRPManager.SetMagField();
150     AliInfo(Form("Loading geometry from OCDB"));
151     AliGeomManager::LoadGeometry();
152     if (!fObjs.IsNull())
153       AliGeomManager::ApplyAlignObjsFromCDB(fObjs);
154   }
155
156   // Load geometry from file (does not use misalignment of ITS/TPC!)
157   TGeoManager *geo = AliGeomManager::GetGeometry();
158   if (!geo && fGeoPath.Length()>0) {
159     TString fname(gSystem->ExpandPathName(Form("%s/geometry_%d.root", fGeoPath.Data(), year)));
160     if (gSystem->AccessPathName(fname)==0) {
161       AliInfo(Form("Loading geometry from file %s (should be avoided!)", fname.Data()));
162       AliGeomManager::LoadGeometry(fname);
163       geo = AliGeomManager::GetGeometry();
164     }
165   }
166
167   // Lock geometry
168   if (geo) {
169     AliInfo(Form("Locking geometry"));
170     geo->LockGeometry();
171   }
172
173   // Construct field map
174   if (!TGeoGlobalMagField::Instance()->GetField()) { 
175     InputEvent()->InitMagneticField();
176   }
177
178   // Apply mis-alignment matrices from OADB
179   if (fOadbPath.Length()>0) {
180     AliOADBContainer emcalgeoCont(Form("emcal"));
181     emcalgeoCont.InitFromFile(Form("%s/EMCALlocal2master.root",fOadbPath.Data()),
182                               Form("AliEMCALgeo"));
183     TObjArray *mobj=dynamic_cast<TObjArray*>(emcalgeoCont.GetObject(runno,"EmcalMatrices"));
184     if (mobj) {
185       for(Int_t mod=0; mod < (geom->GetEMCGeometry())->GetNumberOfSuperModules(); mod++){
186         //AliInfo(Form("Misalignment matrix %d", mod));
187         geom->SetMisalMatrix((TGeoHMatrix*) mobj->At(mod),mod);
188       } 
189     }
190   }
191
192   fIsInit = kTRUE;
193 }
194
195 //________________________________________________________________________
196 void AliEmcalSetupTask::Terminate(Option_t *) 
197 {
198   // Called at the end.
199
200   if (fLocalOcdb.Length()>0) {
201     TString cmd(Form("rm -rf %s", fLocalOcdb.Data()));
202     gSystem->Exec(cmd);
203   }
204 }