]> git.uio.no Git - u/mrichter/AliRoot.git/blame - PWGPP/TPC/AliTaskConfigOCDB.cxx
guess the run number from the input file path
[u/mrichter/AliRoot.git] / PWGPP / TPC / AliTaskConfigOCDB.cxx
CommitLineData
4bd2b776 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"
e07b0141 22#include <TRegexp.h>
4bd2b776 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
32ClassImp(AliTaskConfigOCDB)
33
34//______________________________________________________________________________
35AliTaskConfigOCDB::AliTaskConfigOCDB():
36 AliAnalysisTask(),
37 fRun(0),
38 fRunChanged(kFALSE),
39 fESDhandler(NULL),
40 fESD(NULL),
41 fGRPManager(NULL)
42{
43// Dummy constructor
44}
45
46//______________________________________________________________________________
47AliTaskConfigOCDB::AliTaskConfigOCDB(const char* name, const char *storage, Int_t run)
48 :AliAnalysisTask(name, "configure OCDB field geom"),
49 fRun(run),
50 fRunChanged(kFALSE),
51 fESDhandler(NULL),
52 fESD(NULL),
53 fGRPManager(NULL)
54{
55// Default constructor
56 AliCDBManager *cdb = AliCDBManager::Instance();
57 cdb->SetDefaultStorage(storage);
58 cdb->SetRun(run);
59 DefineInput (0, TChain::Class());
60}
61
62//______________________________________________________________________________
63AliTaskConfigOCDB::~AliTaskConfigOCDB()
64{
65// Destructor
66 delete fGRPManager;
67}
68
69//______________________________________________________________________________
70void AliTaskConfigOCDB::LocalInit()
71{
72// Init CDB locally if run number is defined.
73}
74
75//______________________________________________________________________________
76void AliTaskConfigOCDB::ConnectInputData(Option_t* /*option*/)
77{
78// Connect the input data, create CDB manager.
79}
80
81//______________________________________________________________________________
82void AliTaskConfigOCDB::InitGRP()
83{
84// Initialize geometry and mag. field
85 if (!fGRPManager) {
86 // magnetic field
87 if (!TGeoGlobalMagField::Instance()->GetField()) {
88 printf("AliCDBconnect: #### Loading field map...\n");
89 fGRPManager = new AliGRPManager();
90 if(!fGRPManager->ReadGRPEntry()) {
91 AliError("Cannot get GRP entry");
92 }
93 if( !fGRPManager->SetMagField() ) {
94 AliError("Problem with magnetic field setup");
95 }
96 }
97
98 // geometry
99 if (!gGeoManager) {
100 printf("AliCDBconnect: #### Loading geometry...\n");
101 AliGeomManager::LoadGeometry();
102 if( !AliGeomManager::ApplyAlignObjsFromCDB("GRP ITS TPC TRD") ) {
103 AliError("Problem with align objects");
104 }
105 }
106 }
107}
108
109//______________________________________________________________________________
110void AliTaskConfigOCDB::CreateOutputObjects()
111{
112// Init CDB locally if run number is defined.
113 AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
114 if (!mgr) AliFatal("No analysis manager");
115 fESDhandler = dynamic_cast<AliESDInputHandler *>(mgr->GetInputEventHandler());
116
117 if (!fESDhandler) {
118 AliFatal("No ESD input event handler connected");
119 return;
120 }
121 // Try to get event number before the first event is read (this has precedence
122 // over existing fRun)
4c0fc8e0 123 TTree* inputTree = mgr->GetTree();
124 if (!inputTree) { AliError("no input tree"); return; }
125 TFile* inputFile = inputTree->GetCurrentFile();
126 if (!inputFile) { AliError("no input file"); return; }
127 TString inputFileName(inputFile->GetName());
128 Int_t run = guessRunNumber(inputFileName);
129 mgr->SetRunFromPath(run);
130
4bd2b776 131 if (!run && !fRun) {
132 AliError("AliTaskConfigOCDB: Run not set - no CDB connection");
133 return;
134 }
135 // Create CDB manager
136 AliCDBManager *cdb = AliCDBManager::Instance();
137 // If CDB is already locked, return
138 if (cdb->GetLock()) return;
139 // SetDefault storage. Specific storages must be set by TaskCDBconnectSupply::Init()
140 // cdb->SetDefaultStorage("local://$ALICE_ROOT/OCDB");
141// if (!cdb->GetRaw()) {
142// cdb->SetDefaultStorage("raw://");
143// }
144 if (run && (run != fRun)) {
145 fRunChanged = kTRUE;
146 fRun = run;
147 } else {
148 fRunChanged = kFALSE;
149 }
150 // Set run
151 if (fRunChanged || !fGRPManager) {
152 printf("AliCDBconnect: #### Setting run to: %d\n", fRun);
153 cdb->SetRun(fRun);
154 // Initialize GRP manager only once
155 if (fRun) InitGRP();
156 }
157}
158
159//______________________________________________________________________________
160Bool_t AliTaskConfigOCDB::Notify()
161{
162// Init CDB locally if run number is defined.
163 CreateOutputObjects();
164 return kTRUE;
165}
166
167//______________________________________________________________________________
168void AliTaskConfigOCDB::Exec(Option_t* /*option*/)
169{
170//
171// Execute all supplied analysis of one event. Notify run change via RunChanged().
172 fESD = fESDhandler->GetEvent();
173 // Intercept when the run number changed
174 if (fRun != fESD->GetRunNumber()) {
175 fRunChanged = kTRUE;
176 fRun = fESD->GetRunNumber();
177 CreateOutputObjects();
178 }
179}
180
181//______________________________________________________________________________
182void AliTaskConfigOCDB::Terminate(Option_t *)
183{
e07b0141 184 // Initialize CDB also in Terminate
185 // CreateOutputObjects();
4bd2b776 186}
e07b0141 187
188Int_t AliTaskConfigOCDB::guessRunNumber(TString path)
189{
190 //guess the runnumber from datapath
191 //works also on the LEGO train where the data path looks like this:
192 //workdir/testdata/__alice__data__2010__LHC10b__000114924__ESDs__pass2_root_archive_AliESDs_2/10000114924018.100/root_archive.zip
193 TObjArray* a = path.Tokenize("/_");
194 TRegexp r("^000[0-9][0-9][0-9][0-9][0-9][0-9]$");
195 TString sub;
196 for (Int_t i=0; i<a->GetEntries();i++)
197 {
198 TObjString* subobj = (TObjString*)a->At(i);
199 TString subtmp = subobj->GetString();
200 if (subtmp.Contains(r))
201 {
202 sub=subtmp;
203 break;
204 }
205 }
206 Int_t runNumber=sub.Atoi();
207 AliInfo(Form("guessed run: %i\n",runNumber));
208 a->Delete();
209 delete a;
210 return runNumber;
211}