]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGPP/TPC/AliTaskConfigOCDB.cxx
- Storage of multiplicity now controlled by a parameter
[u/mrichter/AliRoot.git] / PWGPP / TPC / AliTaskConfigOCDB.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 #include "AliTaskConfigOCDB.h"
17
18 #include <TChain.h>
19 #include <TFile.h>
20 #include <TGeoGlobalMagField.h>
21 #include "TGeoManager.h"
22 #include <TRegexp.h>
23  
24 #include "AliAnalysisManager.h"
25 #include "AliGeomManager.h"
26 #include "AliCDBManager.h"
27 #include "AliGRPManager.h"
28 #include "AliESDEvent.h"
29 #include "AliESDInputHandler.h"
30 #include "AliLog.h"
31
32 ClassImp(AliTaskConfigOCDB)
33
34 //______________________________________________________________________________
35 AliTaskConfigOCDB::AliTaskConfigOCDB():
36            AliAnalysisTask(),
37            fRun(0),
38            fOCDBstorage(),
39            fRunChanged(kFALSE),
40            fESDhandler(NULL),
41            fESD(NULL),
42            fGRPManager(NULL)
43 {
44 // Dummy constructor
45 }
46
47 //______________________________________________________________________________
48 AliTaskConfigOCDB::AliTaskConfigOCDB(const char* name, const char *storage, Int_t run)
49           :AliAnalysisTask(name, "configure OCDB field geom"),
50            fRun(run),
51            fOCDBstorage(storage),
52            fRunChanged(kFALSE),
53            fESDhandler(NULL),
54            fESD(NULL),
55            fGRPManager(NULL)
56 {
57 // Default constructor
58   AliCDBManager *cdb = AliCDBManager::Instance();
59   cdb->SetDefaultStorage(storage);
60   cdb->SetRun(run);
61   DefineInput (0, TChain::Class());
62 }
63
64 //______________________________________________________________________________
65 AliTaskConfigOCDB::~AliTaskConfigOCDB()
66 {
67 // Destructor
68   delete fGRPManager;
69 }  
70
71 //______________________________________________________________________________
72 void AliTaskConfigOCDB::LocalInit()
73 {
74 // Init CDB locally if run number is defined.
75 }
76   
77 //______________________________________________________________________________
78 void AliTaskConfigOCDB::ConnectInputData(Option_t* /*option*/)
79 {
80 // Connect the input data, create CDB manager.
81 }
82
83 //______________________________________________________________________________
84 void AliTaskConfigOCDB::InitGRP()
85 {
86 // Initialize geometry and mag. field
87   if (!fGRPManager) {
88   // magnetic field
89     if (!TGeoGlobalMagField::Instance()->GetField()) {
90       printf("AliCDBconnect: #### Loading field map...\n");
91       fGRPManager = new AliGRPManager();
92       if(!fGRPManager->ReadGRPEntry()) { 
93         AliError("Cannot get GRP entry"); 
94       }
95       if( !fGRPManager->SetMagField() ) { 
96         AliError("Problem with magnetic field setup"); 
97       }
98     }
99
100     // geometry
101     if (!gGeoManager) {
102       printf("AliCDBconnect: #### Loading geometry...\n");
103       AliGeomManager::LoadGeometry();
104       if( !AliGeomManager::ApplyAlignObjsFromCDB("GRP ITS TPC TRD") ) {
105         AliError("Problem with align objects"); 
106       }
107     }  
108   }  
109 }
110
111 //______________________________________________________________________________
112 void AliTaskConfigOCDB::CreateOutputObjects()
113 {
114 // Init CDB locally if run number is defined.
115   AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
116   if (!mgr) AliFatal("No analysis manager");
117   fESDhandler = dynamic_cast<AliESDInputHandler *>(mgr->GetInputEventHandler());
118     
119   if (!fESDhandler) {
120      AliFatal("No ESD input event handler connected");
121      return;
122   }   
123   // Try to get event number before the first event is read (this has precedence
124   // over existing fRun)
125   TTree* inputTree = mgr->GetTree();
126   if (!inputTree) { AliError("no input tree"); return; }
127   TFile* inputFile = inputTree->GetCurrentFile();
128   if (!inputFile) { AliError("no input file"); return; }
129   TString inputFileName(inputFile->GetName());
130   Int_t run = guessRunNumber(inputFileName);
131   mgr->SetRunFromPath(run);
132
133   if (!run && !fRun) {
134      AliError("AliTaskConfigOCDB: Run not set - no CDB connection");
135      return;
136   }
137   // Create CDB manager
138   AliCDBManager *cdb = AliCDBManager::Instance();
139   // If CDB is unlocked, set the def storage
140   if (!cdb->GetLock())
141   {
142     cdb->SetDefaultStorage(fOCDBstorage);
143   }
144
145   if (run && (run != fRun)) {
146      fRunChanged = kTRUE;
147      fRun = run;
148   } else {
149      fRunChanged = kFALSE;
150   }
151   // Set run
152   if (fRunChanged || !fGRPManager) {
153      printf("AliCDBconnect: #### Setting run to: %d\n", fRun);
154      cdb->SetRun(fRun);
155      // Initialize GRP manager only once
156      if (fRun) InitGRP();
157   }   
158 }
159
160 //______________________________________________________________________________
161 Bool_t AliTaskConfigOCDB::Notify()
162 {
163 // Init CDB locally if run number is defined.
164   CreateOutputObjects();
165   return kTRUE;
166 }
167
168 //______________________________________________________________________________
169 void AliTaskConfigOCDB::Exec(Option_t* /*option*/)
170 {
171 //
172 // Execute all supplied analysis of one event. Notify run change via RunChanged().
173   fESD = fESDhandler->GetEvent();
174   // Intercept when the run number changed
175   if (fRun != fESD->GetRunNumber()) {
176     fRunChanged = kTRUE;
177     fRun = fESD->GetRunNumber();
178     CreateOutputObjects();
179   }
180 }
181
182 //______________________________________________________________________________
183 void AliTaskConfigOCDB::Terminate(Option_t *)
184 {
185   // Initialize CDB also in Terminate
186   //   CreateOutputObjects();
187 }
188
189 Int_t AliTaskConfigOCDB::guessRunNumber(TString path)
190 {
191   //guess the runnumber from datapath
192   //works also on the LEGO train where the data path looks like this:
193   //workdir/testdata/__alice__data__2010__LHC10b__000114924__ESDs__pass2_root_archive_AliESDs_2/10000114924018.100/root_archive.zip
194   TObjArray* a = path.Tokenize("/_");
195   TRegexp r("^000[0-9][0-9][0-9][0-9][0-9][0-9]$");
196   TString sub;
197   for (Int_t i=0; i<a->GetEntries();i++)
198   {
199     TObjString* subobj = (TObjString*)a->At(i);
200     TString subtmp = subobj->GetString();
201     if (subtmp.Contains(r)) 
202     {
203       sub=subtmp;
204       break;
205     }
206   }
207   Int_t runNumber=sub.Atoi();
208   AliInfo(Form("guessed run: %i\n",runNumber));
209   a->Delete();
210   delete a;
211   return runNumber;
212 }