]> git.uio.no Git - u/mrichter/AliRoot.git/blame - PWG/EMCAL/AliEmcalSetupTask.cxx
change order of bookkeeping events
[u/mrichter/AliRoot.git] / PWG / EMCAL / AliEmcalSetupTask.cxx
CommitLineData
6ea93b32 1// $Id$
2//
980821ba 3// Task to setup emcal related global objects.
6ea93b32 4//
cd231d42 5// Author: C.Loizides
6ea93b32 6
b933ccf0 7#include "AliEmcalSetupTask.h"
6ea93b32 8#include <TClonesArray.h>
6ea93b32 9#include <TGeoGlobalMagField.h>
dec5f256 10#include <TGeoManager.h>
012dab22 11#include <TRandom.h>
dec5f256 12#include "AliAODEvent.h"
6ea93b32 13#include "AliAnalysisManager.h"
14#include "AliCDBManager.h"
15#include "AliEMCALGeometry.h"
16#include "AliESDEvent.h"
eb0d359d 17#include "AliGRPManager.h"
6ea93b32 18#include "AliGeomManager.h"
dec5f256 19#include "AliMagF.h"
6ea93b32 20#include "AliOADBContainer.h"
810787c3 21#include "AliTender.h"
6ea93b32 22
23ClassImp(AliEmcalSetupTask)
24
25//________________________________________________________________________
26AliEmcalSetupTask::AliEmcalSetupTask() :
27 AliAnalysisTaskSE(),
012dab22 28 fOcdbPath("uselocal"),
6ea93b32 29 fOadbPath("$ALICE_ROOT/OADB/EMCAL"),
e8481053
CL
30 fGeoPath("$ALICE_ROOT/OADB/EMCAL"),
31 fObjs("GRP ITS TPC TRD EMCAL"),
012dab22 32 fIsInit(kFALSE),
810787c3 33 fLocalOcdb(),
34 fLocalOcdbStor()
6ea93b32 35{
36 // Constructor.
37}
38
39//________________________________________________________________________
40AliEmcalSetupTask::AliEmcalSetupTask(const char *name) :
41 AliAnalysisTaskSE(name),
012dab22 42 fOcdbPath("uselocal"),
6ea93b32 43 fOadbPath("$ALICE_ROOT/OADB/EMCAL"),
4b4fc849 44 fGeoPath("$ALICE_ROOT/OADB/EMCAL"),
e8481053 45 fObjs("GRP ITS TPC TRD EMCAL"),
012dab22 46 fIsInit(kFALSE),
810787c3 47 fLocalOcdb(),
48 fLocalOcdbStor()
6ea93b32 49{
50 // Constructor.
51 fBranchNames = "ESD:AliESDHeader.,AliESDRun.";
52}
53
54//________________________________________________________________________
55AliEmcalSetupTask::~AliEmcalSetupTask()
56{
57 // Destructor.
58}
59
810787c3 60//________________________________________________________________________
61void AliEmcalSetupTask::ConnectInputData(Option_t *option)
62{
63 // Connect input data
64
65 AliAnalysisTaskSE::ConnectInputData(option);
66
67 if (fOcdbPath.Length()==0)
68 return;
69
3f0ec366 70 AliCDBManager *man = AliCDBManager::Instance();
71 if (man->IsDefaultStorageSet())
72 return;
73
810787c3 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
6ea93b32 112//________________________________________________________________________
113void AliEmcalSetupTask::UserExec(Option_t *)
114{
115 // Main loop, called for each event.
116
117 if (fIsInit)
118 return;
119
6ea93b32 120 AliAnalysisManager *am = AliAnalysisManager::GetAnalysisManager();
121 if (!am) {
122 AliError("Manager zero, returning");
123 return;
124 }
012dab22 125
6ea93b32 126 am->LoadBranch("AliESDRun.");
127 am->LoadBranch("AliESDHeader.");
128
dec5f256 129 Int_t runno = InputEvent()->GetRunNumber();
810787c3 130 Setup(runno);
131}
132
133//________________________________________________________________________
134void AliEmcalSetupTask::Setup(Int_t runno)
135{
136 // Setup everything
137
810787c3 138 // Setup AliEMCALGeometry corresponding to year
a1a56aea 139 TString geoname("EMCAL_COMPLETE12SMV1");
4b4fc849 140 Int_t year = 2013;
c080ac89 141 if (runno>0 && runno<=139517) {
a1a56aea 142 year = 2010;
143 geoname = "EMCAL_FIRSTYEARV1";
4b4fc849 144 } else if ((runno>139517) && (runno<=170593)) {
64a8dac7 145 year = 2011;
146 geoname = "EMCAL_COMPLETEV1";
4b4fc849 147 } else if ((runno>170593) && (runno<=193766)) {
148 year = 2012;
6ea93b32 149 }
6ea93b32 150 AliEMCALGeometry *geom = AliEMCALGeometry::GetInstance(geoname);
151 if (!geom) {
152 AliFatal(Form("Can not create geometry: %s",geoname.Data()));
153 return;
154 }
155
c080ac89 156 if (runno<=0)
157 return;
158
012dab22 159 // Setup CDB manager
6ea93b32 160 AliCDBManager *man = 0;
eb0d359d 161 man = AliCDBManager::Instance();
012dab22
CL
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()));
eb97986c 169 man->SetDefaultStorage(fOcdbPath);
012dab22
CL
170 } else { // use local copy of OCDB
171 TString tmpdir=gSystem->WorkingDirectory();
af901268 172 if (gSystem->AccessPathName(tmpdir))
173 tmpdir = "/tmp";
012dab22
CL
174 tmpdir+="/";
175 tmpdir+=gSystem->GetUid();
176 tmpdir+="-";
177 TDatime t;
178 tmpdir+=t.Get();
179 tmpdir+="-";
012dab22 180 Int_t counter = 0;
af901268 181 fLocalOcdb = tmpdir;
810787c3 182 fLocalOcdb += Form("%d%d%d",gRandom->Integer(999999999),gRandom->Integer(999999999),gRandom->Integer(999999999));
af901268 183 while (!gSystem->AccessPathName(fLocalOcdb)) {
012dab22 184 fLocalOcdb = tmpdir;
810787c3 185 fLocalOcdb += Form("%d%d%d",gRandom->Integer(999999999),gRandom->Integer(999999999),gRandom->Integer(999999999));
012dab22
CL
186 counter++;
187 if (counter>100) {
188 AliFatal(Form("Could not create local directory for OCDB at %s",tmpdir.Data()));
189 }
190 }
af901268 191 gSystem->MakeDirectory(fLocalOcdb);
012dab22
CL
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);
810787c3 202 fLocalOcdbStor = locdb;
012dab22
CL
203 } else {
204 AliFatal(Form("Could not set up local OCDB at %s",fLocalOcdb.Data()));
205 }
eb0d359d 206 }
6ea93b32 207 }
eb0d359d 208
012dab22 209 // Load geometry from OCDB
eb0d359d 210 if (man) {
012dab22
CL
211 if (man->GetRun()!=runno)
212 man->SetRun(runno);
af901268 213 AliInfo(Form("Loading grp data from OCDB for run %d", runno));
eb0d359d
CL
214 AliGRPManager GRPManager;
215 GRPManager.ReadGRPEntry();
216 GRPManager.SetMagField();
217 AliInfo(Form("Loading geometry from OCDB"));
218 AliGeomManager::LoadGeometry();
e8481053
CL
219 if (!fObjs.IsNull())
220 AliGeomManager::ApplyAlignObjsFromCDB(fObjs);
eb0d359d 221 }
6ea93b32 222
012dab22 223 // Load geometry from file (does not use misalignment of ITS/TPC!)
6ea93b32 224 TGeoManager *geo = AliGeomManager::GetGeometry();
012dab22 225 if (!geo && fGeoPath.Length()>0) {
64a8dac7 226 TString fname(gSystem->ExpandPathName(Form("%s/geometry_%d.root", fGeoPath.Data(), year)));
6ea93b32 227 if (gSystem->AccessPathName(fname)==0) {
012dab22 228 AliInfo(Form("Loading geometry from file %s (should be avoided!)", fname.Data()));
6ea93b32 229 AliGeomManager::LoadGeometry(fname);
ba4cb34f 230 geo = AliGeomManager::GetGeometry();
6ea93b32 231 }
6ea93b32 232 }
012dab22
CL
233
234 // Lock geometry
eb0d359d
CL
235 if (geo) {
236 AliInfo(Form("Locking geometry"));
237 geo->LockGeometry();
238 }
6ea93b32 239
012dab22
CL
240 // Construct field map
241 if (!TGeoGlobalMagField::Instance()->GetField()) {
ddc5a41b 242 InputEvent()->InitMagneticField();
6ea93b32 243 }
244
012dab22 245 // Apply mis-alignment matrices from OADB
6ea93b32 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}
012dab22
CL
261
262//________________________________________________________________________
263void 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}