propagator and setup task
[u/mrichter/AliRoot.git] / PWG4 / UserTasks / EmcalTasks / AliEmcalSetupTask.cxx
1 // $Id$
2 //
3 // Task to setup emcal related global objects
4 //
5 //
6
7 #include <TClonesArray.h>
8 #include <TGeoManager.h>
9 #include <TGeoGlobalMagField.h>
10 #include "AliAnalysisManager.h"
11 #include "AliCDBManager.h"
12 #include "AliEMCALGeometry.h"
13 #include "AliESDEvent.h"
14 #include "AliEmcalSetupTask.h"
15 #include "AliGeomManager.h"
16 #include "AliOADBContainer.h"
17
18 ClassImp(AliEmcalSetupTask)
19
20 //________________________________________________________________________
21 AliEmcalSetupTask::AliEmcalSetupTask() : 
22   AliAnalysisTaskSE(),
23   fOcdbPath(),
24   fOadbPath("$ALICE_ROOT/OADB/EMCAL"),
25   fEsdEv(0),
26   fIsInit(kFALSE)
27 {
28   // Constructor.
29 }
30
31 //________________________________________________________________________
32 AliEmcalSetupTask::AliEmcalSetupTask(const char *name) : 
33   AliAnalysisTaskSE(name),
34   fOcdbPath(),
35   fOadbPath("$ALICE_ROOT/OADB/EMCAL"),
36   fEsdEv(0),
37   fIsInit(kFALSE)
38 {
39   // Constructor.
40   fBranchNames = "ESD:AliESDHeader.,AliESDRun.";
41 }
42
43 //________________________________________________________________________
44 AliEmcalSetupTask::~AliEmcalSetupTask()
45 {
46   // Destructor.
47 }
48
49 //________________________________________________________________________
50 void AliEmcalSetupTask::UserExec(Option_t *) 
51 {
52   // Main loop, called for each event.
53
54   if (fIsInit)
55     return;
56
57   fEsdEv = dynamic_cast<AliESDEvent*>(InputEvent());
58   if (!fEsdEv) {
59     AliError("Task works only on ESD events, returning");
60     return;
61   }
62
63   AliAnalysisManager *am = AliAnalysisManager::GetAnalysisManager();
64   if (!am) {
65     AliError("Manager zero, returning");
66     return;
67   }
68   am->LoadBranch("AliESDRun.");
69   am->LoadBranch("AliESDHeader.");
70
71   Int_t runno = fEsdEv->GetRunNumber();
72   Bool_t is2010 = kTRUE;
73   if (runno>139517) {
74     is2010 = kFALSE;
75   }
76
77   TString geoname("EMCAL_FIRSTYEARV1");
78   if (!is2010)
79     geoname = "EMCAL_COMPLETEV1";
80   AliEMCALGeometry *geom = AliEMCALGeometry::GetInstance(geoname);
81   if (!geom) {
82     AliFatal(Form("Can not create geometry: %s",geoname.Data()));
83     return;
84   }
85
86   AliCDBManager *man = 0;
87   if (fOcdbPath.Length()>0) {
88     AliInfo(Form("Setting up OCDB"));
89     man = AliCDBManager::Instance();
90     man->SetDefaultStorage(fOcdbPath);
91     man->SetRun(runno);
92   }
93
94   TGeoManager *geo = AliGeomManager::GetGeometry();
95   if (!geo) {
96     TString fname("geometry_2010.root");
97     if (!is2010)
98       fname = "geometry_2011.root";
99     if (gSystem->AccessPathName(fname)!=0)
100       fname = "geometry.root";
101     if (gSystem->AccessPathName(fname)==0) {
102       AliInfo(Form("Loading geometry from %s", fname.Data()));
103       AliGeomManager::LoadGeometry(fname);
104     } else if (man) {
105       AliInfo(Form("Loading geometry from OCDB"));
106       AliGeomManager::LoadGeometry();
107     }
108   }
109   if (geo) {
110     AliGeomManager::ApplyAlignObjsFromCDB("EMCAL");
111     AliInfo(Form("Locking geometry"));
112     geo->LockGeometry();
113   }
114
115   if (!TGeoGlobalMagField::Instance()->GetField()) { // construct field map
116     AliInfo("Constructing field map from ESD run info");
117     fEsdEv->InitMagneticField();
118   }
119
120   if (fOadbPath.Length()>0) {
121     AliOADBContainer emcalgeoCont(Form("emcal"));
122     emcalgeoCont.InitFromFile(Form("%s/EMCALlocal2master.root",fOadbPath.Data()),
123                               Form("AliEMCALgeo"));
124     TObjArray *mobj=dynamic_cast<TObjArray*>(emcalgeoCont.GetObject(runno,"EmcalMatrices"));
125     if (mobj) {
126       for(Int_t mod=0; mod < (geom->GetEMCGeometry())->GetNumberOfSuperModules(); mod++){
127         //AliInfo(Form("Misalignment matrix %d", mod));
128         geom->SetMisalMatrix((TGeoHMatrix*) mobj->At(mod),mod);
129       } 
130     }
131   }
132
133   fIsInit = kTRUE;
134 }