]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG/EMCAL/AliEmcalSetupTask.cxx
added geo path and updated macro to be able to specify input
[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 "AliAODEvent.h"
12 #include "AliAnalysisManager.h"
13 #include "AliCDBManager.h"
14 #include "AliEMCALGeometry.h"
15 #include "AliESDEvent.h"
16 #include "AliGeomManager.h"
17 #include "AliMagF.h"
18 #include "AliOADBContainer.h"
19
20 ClassImp(AliEmcalSetupTask)
21
22 //________________________________________________________________________
23 AliEmcalSetupTask::AliEmcalSetupTask() : 
24   AliAnalysisTaskSE(),
25   fOcdbPath(),
26   fOadbPath("$ALICE_ROOT/OADB/EMCAL"),
27   fGeoPath("."),
28   fIsInit(kFALSE)
29 {
30   // Constructor.
31 }
32
33 //________________________________________________________________________
34 AliEmcalSetupTask::AliEmcalSetupTask(const char *name) : 
35   AliAnalysisTaskSE(name),
36   fOcdbPath(),
37   fOadbPath("$ALICE_ROOT/OADB/EMCAL"),
38   fGeoPath("$ALICE_ROOT/OADB/EMCAL"),
39   fIsInit(kFALSE)
40 {
41   // Constructor.
42   fBranchNames = "ESD:AliESDHeader.,AliESDRun.";
43 }
44
45 //________________________________________________________________________
46 AliEmcalSetupTask::~AliEmcalSetupTask()
47 {
48   // Destructor.
49 }
50
51 //________________________________________________________________________
52 void AliEmcalSetupTask::UserExec(Option_t *) 
53 {
54   // Main loop, called for each event.
55
56   if (fIsInit)
57     return;
58
59   AliAnalysisManager *am = AliAnalysisManager::GetAnalysisManager();
60   if (!am) {
61     AliError("Manager zero, returning");
62     return;
63   }
64   am->LoadBranch("AliESDRun.");
65   am->LoadBranch("AliESDHeader.");
66
67   Int_t runno = InputEvent()->GetRunNumber();
68   TString geoname("EMCAL_COMPLETE12SMV1");
69   Int_t year = 2013;
70   if (runno<=139517) {
71     year = 2010;
72     geoname = "EMCAL_FIRSTYEARV1";
73   } else if ((runno>139517) && (runno<=170593)) {
74     year = 2011;
75     geoname = "EMCAL_COMPLETEV1";
76   } else if ((runno>170593) && (runno<=193766)) {
77     year = 2012;
78   }
79
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(gSystem->ExpandPathName(Form("%s/geometry_%d.root", fGeoPath.Data(), year)));
97     if (gSystem->AccessPathName(fname)==0) {
98       AliInfo(Form("Loading geometry from %s", fname.Data()));
99       AliGeomManager::LoadGeometry(fname);
100     } else if (man) {
101       AliInfo(Form("Loading geometry from OCDB"));
102       AliGeomManager::LoadGeometry();
103     }
104   }
105   if (geo) {
106     AliGeomManager::ApplyAlignObjsFromCDB("EMCAL");
107     AliInfo(Form("Locking geometry"));
108     geo->LockGeometry();
109   }
110
111   if (!TGeoGlobalMagField::Instance()->GetField()) { // construct field map
112     AliESDEvent *esdEv = dynamic_cast<AliESDEvent*>(InputEvent());
113     if (esdEv) {
114       AliInfo("Constructing field map from ESD run info");
115       esdEv->InitMagneticField();
116     } else {
117       AliAODEvent *aodEv = dynamic_cast<AliAODEvent*>(InputEvent());
118       if (aodEv) {
119         Double_t curSol = 30000*aodEv->GetMagneticField()/5.00668;
120         Double_t curDip = 6000 *aodEv->GetMuonMagFieldScale();
121         AliMagF *field  = AliMagF::CreateFieldMap(curSol,curDip);
122         TGeoGlobalMagField::Instance()->SetField(field);
123       }
124     }
125   }
126
127   if (fOadbPath.Length()>0) {
128     AliOADBContainer emcalgeoCont(Form("emcal"));
129     emcalgeoCont.InitFromFile(Form("%s/EMCALlocal2master.root",fOadbPath.Data()),
130                               Form("AliEMCALgeo"));
131     TObjArray *mobj=dynamic_cast<TObjArray*>(emcalgeoCont.GetObject(runno,"EmcalMatrices"));
132     if (mobj) {
133       for(Int_t mod=0; mod < (geom->GetEMCGeometry())->GetNumberOfSuperModules(); mod++){
134         //AliInfo(Form("Misalignment matrix %d", mod));
135         geom->SetMisalMatrix((TGeoHMatrix*) mobj->At(mod),mod);
136       } 
137     }
138   }
139
140   fIsInit = kTRUE;
141 }