]> git.uio.no Git - u/mrichter/AliRoot.git/blame - PWG/EMCAL/AliEmcalSetupTask.cxx
Merge branch 'feature-movesplit'
[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"),
6b6a028a 32 fNoOCDB(kFALSE),
012dab22 33 fIsInit(kFALSE),
810787c3 34 fLocalOcdb(),
35 fLocalOcdbStor()
6ea93b32 36{
37 // Constructor.
38}
39
40//________________________________________________________________________
41AliEmcalSetupTask::AliEmcalSetupTask(const char *name) :
42 AliAnalysisTaskSE(name),
012dab22 43 fOcdbPath("uselocal"),
6ea93b32 44 fOadbPath("$ALICE_ROOT/OADB/EMCAL"),
4b4fc849 45 fGeoPath("$ALICE_ROOT/OADB/EMCAL"),
e8481053 46 fObjs("GRP ITS TPC TRD EMCAL"),
6b6a028a 47 fNoOCDB(kFALSE),
012dab22 48 fIsInit(kFALSE),
810787c3 49 fLocalOcdb(),
50 fLocalOcdbStor()
6ea93b32 51{
52 // Constructor.
53 fBranchNames = "ESD:AliESDHeader.,AliESDRun.";
54}
55
56//________________________________________________________________________
57AliEmcalSetupTask::~AliEmcalSetupTask()
58{
59 // Destructor.
60}
61
810787c3 62//________________________________________________________________________
63void AliEmcalSetupTask::ConnectInputData(Option_t *option)
64{
65 // Connect input data
66
67 AliAnalysisTaskSE::ConnectInputData(option);
68
69 if (fOcdbPath.Length()==0)
70 return;
71
3f0ec366 72 AliCDBManager *man = AliCDBManager::Instance();
73 if (man->IsDefaultStorageSet())
74 return;
75
810787c3 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
6ea93b32 114//________________________________________________________________________
115void AliEmcalSetupTask::UserExec(Option_t *)
116{
117 // Main loop, called for each event.
118
119 if (fIsInit)
120 return;
121
6ea93b32 122 AliAnalysisManager *am = AliAnalysisManager::GetAnalysisManager();
123 if (!am) {
124 AliError("Manager zero, returning");
125 return;
126 }
012dab22 127
6ea93b32 128 am->LoadBranch("AliESDRun.");
129 am->LoadBranch("AliESDHeader.");
130
dec5f256 131 Int_t runno = InputEvent()->GetRunNumber();
810787c3 132 Setup(runno);
133}
134
135//________________________________________________________________________
136void AliEmcalSetupTask::Setup(Int_t runno)
137{
138 // Setup everything
139
810787c3 140 // Setup AliEMCALGeometry corresponding to year
b0aacf89 141 TString geoname("EMCAL_COMPLETE12SMV1");
142 Int_t year = 2013;
c080ac89 143 if (runno>0 && runno<=139517) {
a1a56aea 144 year = 2010;
145 geoname = "EMCAL_FIRSTYEARV1";
4b4fc849 146 } else if ((runno>139517) && (runno<=170593)) {
64a8dac7 147 year = 2011;
148 geoname = "EMCAL_COMPLETEV1";
4b4fc849 149 } else if ((runno>170593) && (runno<=193766)) {
150 year = 2012;
d9b3f983 151 geoname = "EMCAL_COMPLETE12SMV1";
de47dde0 152 } else if ((runno>193766) && (runno <= 199161)) {
153 geoname = "EMCAL_COMPLETE12SMV1";
154 year = 2013;
b0aacf89 155 } else if (runno>199161) { //MV: is this the last run of run 1?
156 geoname = "EMCAL_COMPLETE12SMV1_DCAL_8SM";
157 year = 2015;
6ea93b32 158 }
6ea93b32 159 AliEMCALGeometry *geom = AliEMCALGeometry::GetInstance(geoname);
160 if (!geom) {
161 AliFatal(Form("Can not create geometry: %s",geoname.Data()));
162 return;
163 }
164
de47dde0 165 if (runno<0) // Run number 0 can occur for MC
c080ac89 166 return;
167
012dab22 168 // Setup CDB manager
6ea93b32 169 AliCDBManager *man = 0;
6b6a028a 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;
012dab22 194 fLocalOcdb = tmpdir;
810787c3 195 fLocalOcdb += Form("%d%d%d",gRandom->Integer(999999999),gRandom->Integer(999999999),gRandom->Integer(999999999));
6b6a028a 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()));
012dab22 218 }
012dab22 219 }
eb0d359d 220 }
6ea93b32 221 }
6b6a028a 222
012dab22 223 // Load geometry from OCDB
eb0d359d 224 if (man) {
012dab22
CL
225 if (man->GetRun()!=runno)
226 man->SetRun(runno);
af901268 227 AliInfo(Form("Loading grp data from OCDB for run %d", runno));
eb0d359d
CL
228 AliGRPManager GRPManager;
229 GRPManager.ReadGRPEntry();
230 GRPManager.SetMagField();
231 AliInfo(Form("Loading geometry from OCDB"));
232 AliGeomManager::LoadGeometry();
e8481053
CL
233 if (!fObjs.IsNull())
234 AliGeomManager::ApplyAlignObjsFromCDB(fObjs);
eb0d359d 235 }
6ea93b32 236
012dab22 237 // Load geometry from file (does not use misalignment of ITS/TPC!)
6ea93b32 238 TGeoManager *geo = AliGeomManager::GetGeometry();
012dab22 239 if (!geo && fGeoPath.Length()>0) {
64a8dac7 240 TString fname(gSystem->ExpandPathName(Form("%s/geometry_%d.root", fGeoPath.Data(), year)));
6ea93b32 241 if (gSystem->AccessPathName(fname)==0) {
012dab22 242 AliInfo(Form("Loading geometry from file %s (should be avoided!)", fname.Data()));
6ea93b32 243 AliGeomManager::LoadGeometry(fname);
ba4cb34f 244 geo = AliGeomManager::GetGeometry();
6ea93b32 245 }
6ea93b32 246 }
012dab22
CL
247
248 // Lock geometry
eb0d359d
CL
249 if (geo) {
250 AliInfo(Form("Locking geometry"));
251 geo->LockGeometry();
252 }
6ea93b32 253
012dab22
CL
254 // Construct field map
255 if (!TGeoGlobalMagField::Instance()->GetField()) {
ddc5a41b 256 InputEvent()->InitMagneticField();
6ea93b32 257 }
258
012dab22 259 // Apply mis-alignment matrices from OADB
6ea93b32 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}
012dab22
CL
275
276//________________________________________________________________________
277void 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}