]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG/EMCAL/AliEmcalSetupTask.cxx
protections against failures in deleting event content
[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("."),
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_FIRSTYEARV1");
69   Int_t year = 2010;
70   if (runno>139517) {
71     year = 2011;
72     geoname = "EMCAL_COMPLETEV1";
73   } 
74   if (runno>170593) {
75     year = 2012;
76     geoname = "EMCAL_COMPLETE12SMV1";
77   }
78
79   AliEMCALGeometry *geom = AliEMCALGeometry::GetInstance(geoname);
80   if (!geom) {
81     AliFatal(Form("Can not create geometry: %s",geoname.Data()));
82     return;
83   }
84
85   AliCDBManager *man = 0;
86   if (fOcdbPath.Length()>0) {
87     AliInfo(Form("Setting up OCDB"));
88     man = AliCDBManager::Instance();
89     man->SetDefaultStorage(fOcdbPath);
90     man->SetRun(runno);
91   }
92
93   TGeoManager *geo = AliGeomManager::GetGeometry();
94   if (!geo) {
95     TString fname(gSystem->ExpandPathName(Form("%s/geometry_%d.root", fGeoPath.Data(), year)));
96     if (gSystem->AccessPathName(fname)==0) {
97       AliInfo(Form("Loading geometry from %s", fname.Data()));
98       AliGeomManager::LoadGeometry(fname);
99     } else if (man) {
100       AliInfo(Form("Loading geometry from OCDB"));
101       AliGeomManager::LoadGeometry();
102     }
103   }
104   if (geo) {
105     AliGeomManager::ApplyAlignObjsFromCDB("EMCAL");
106     AliInfo(Form("Locking geometry"));
107     geo->LockGeometry();
108   }
109
110   if (!TGeoGlobalMagField::Instance()->GetField()) { // construct field map
111     AliESDEvent *esdEv = dynamic_cast<AliESDEvent*>(InputEvent());
112     if (esdEv) {
113       AliInfo("Constructing field map from ESD run info");
114       esdEv->InitMagneticField();
115     } else {
116       AliAODEvent *aodEv = dynamic_cast<AliAODEvent*>(InputEvent());
117       if (aodEv) {
118         Double_t curSol = 30000*aodEv->GetMagneticField()/5.00668;
119         Double_t curDip = 6000 *aodEv->GetMuonMagFieldScale();
120         AliMagF *field  = AliMagF::CreateFieldMap(curSol,curDip);
121         TGeoGlobalMagField::Instance()->SetField(field);
122       }
123     }
124   }
125
126   if (fOadbPath.Length()>0) {
127     AliOADBContainer emcalgeoCont(Form("emcal"));
128     emcalgeoCont.InitFromFile(Form("%s/EMCALlocal2master.root",fOadbPath.Data()),
129                               Form("AliEMCALgeo"));
130     TObjArray *mobj=dynamic_cast<TObjArray*>(emcalgeoCont.GetObject(runno,"EmcalMatrices"));
131     if (mobj) {
132       for(Int_t mod=0; mod < (geom->GetEMCGeometry())->GetNumberOfSuperModules(); mod++){
133         //AliInfo(Form("Misalignment matrix %d", mod));
134         geom->SetMisalMatrix((TGeoHMatrix*) mobj->At(mod),mod);
135       } 
136     }
137   }
138
139   fIsInit = kTRUE;
140 }