AliPhysics  d565ceb (d565ceb)
AliTaskCDBconnect.cxx
Go to the documentation of this file.
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 "AliTaskCDBconnect.h"
17 
18 #include <TChain.h>
19 #include <TFile.h>
20 #include <TRegexp.h>
21 #include <TGeoGlobalMagField.h>
22 #include "TGeoManager.h"
23 
24 #include "AliAnalysisManager.h"
25 #include "AliGeomManager.h"
26 #include "AliCDBManager.h"
27 #include "AliGRPManager.h"
28 #include "AliVEvent.h"
29 #include "AliInputEventHandler.h"
30 #include "AliLog.h"
31 
32 ClassImp(AliTaskCDBconnect)
33 
34 //______________________________________________________________________________
37  fFallBackToRaw(kFALSE),
38  fRun(0),
39  fLock(0),
40  fStorage(),
41  fSpecCDBUri(),
42  fGRPManager(NULL)
43 {
44  // Dummy constructor
45  fSpecCDBUri.SetOwner();
46 }
47 
48 //______________________________________________________________________________
49 AliTaskCDBconnect::AliTaskCDBconnect(const char* name, const char *storage, Int_t run, Bool_t fallback)
50  :AliAnalysisTask(name, "ESD analysis tender car"),
51  fFallBackToRaw(fallback),
52  fRun(run),
53  fLock(0),
54  fStorage(storage),
55  fSpecCDBUri(),
56  fGRPManager(NULL)
57 {
58  // Default constructor
59  fSpecCDBUri.SetOwner();
60  DefineInput (0, TChain::Class());
61  if (run>0) InitGRP();
62 }
63 
64 //______________________________________________________________________________
66 {
67  // Destructor
68  delete fGRPManager;
69 }
70 
71 //______________________________________________________________________________
73 {
74  // Initialize geometry and mag. field
75  AliCDBManager *cdb = AliCDBManager::Instance();
76 
77  if (!cdb->IsDefaultStorageSet()) {
78  //
79  // automatic setting of year
80  Int_t year = -1;
81  if (fRun<139674) year = 2010;
82  else if (fRun<170718) year = 2011;
83  else if (fRun<194479) year = 2012;
84  else if (fRun<199999) year = 2013;
85  else if (fRun<208504) year = 2014;
86  else if (fRun<247170) year = 2015;
87  else if (fRun<267255) year = 2016;
88  else if (fRun<282901) year = 2017;
89  else {
90  year = 2018;
91  TDatime today;
92  if (today.GetYear()!=year) AliErrorF("Adjust CDB connect, we are now in %d!",today.GetYear());
93  }
94  //
95  Bool_t useCVMFS = kFALSE;
96  TString inpStor = fStorage.Strip(TString::kTrailing,'/');
97  if (inpStor == "cvmfs:") {
98  fStorage = Form("local:///cvmfs/alice.cern.ch/calibration/data/%4d/OCDB",year);
99  useCVMFS = kTRUE;
100  }
101  else if (inpStor.BeginsWith("local:///cvmfs") && inpStor.EndsWith("/OCDB")) {
102  TString tmp = inpStor;
103  tmp.ReplaceAll("local://","");
104  TString strYold = tmp(TRegexp("/[0-9][0-9][0-9][0-9]/OCDB"));
105  TString strYnew = Form("/%4d/OCDB",year);
106  if (strYold.IsNull()) tmp += strYnew;
107  else tmp.ReplaceAll(strYold,strYnew);
108  useCVMFS = kTRUE;
109  fStorage = Form("local://%s", tmp.Data());
110  }
111  // check if cvfms is linked
112  if (useCVMFS) {
113  TString cvmfspath = fStorage;
114  cvmfspath = cvmfspath.ReplaceAll("local://", "");
115  if(gSystem->AccessPathName(cvmfspath.Data(),kFileExists)) {
116  if (fFallBackToRaw) {
117  AliErrorF("could not access %s, switching to raw://",fStorage.Data());
118  fStorage = "raw://";
119  }
120  else AliFatalF("could not access %s, fallback to raw:// disabled",fStorage.Data());
121  }
122  }
123  AliInfoF("Setting default storage to %s",fStorage.Data());
124  cdb->SetDefaultStorage(fStorage);
125  //
126  // set specific storages
127  for (Int_t i = 0; i < fSpecCDBUri.GetEntriesFast(); i++) {
128  TNamed* obj = (TNamed*)fSpecCDBUri[i];
129  if (!obj) continue;
130  UInt_t vsv = obj->GetUniqueID();
131  Int_t ver = int(vsv>>16)-1;
132  Int_t subver = int(vsv&0xffff)-1;
133  cdb->SetSpecificStorage(obj->GetName(), obj->GetTitle(), ver, subver);
134  }
135  }
136  if (cdb->GetRun()!=fRun) {
137  fLock = cdb->SetLock(kFALSE,fLock);
138  cdb->SetRun(fRun);
139  fLock = cdb->SetLock(kTRUE,fLock);
140  }
141  if (gSystem->AccessPathName("OCDB.root",kFileExists)==0) cdb->SetSnapshotMode("OCDB.root");
142  //
143  if (!fGRPManager) fGRPManager = new AliGRPManager();
144  AliInfo("AliCDBconnect: #### Loading GRP to init B-field...");
145  if(!fGRPManager->ReadGRPEntry()) AliFatal("Cannot get GRP entry");
146  if(!fGRPManager->SetMagField()) AliFatal("Problem with magnetic field setup");
147  //
148  // geometry
149  if (!gGeoManager) {
150  AliInfo("AliCDBconnect: #### Loading geometry...");
151  AliGeomManager::LoadGeometry("geometry.root");
152  if(!AliGeomManager::ApplyAlignObjsFromCDB("GRP ITS TPC TRD TOF EMCAL PHOS MUON")) AliWarning("Problem with align objects");
153  }
154 }
155 
156 //______________________________________________________________________________
158 {
159  // Init CDB locally if run number is defined.
160  //
161  // try to init before the analysis set
162  AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
163  if (!mgr) AliFatal("No analysis manager");
164  if (fRun>0 && !fGRPManager) {
165  // in the proof or plugin mode the initialization done in the constructor is not available
166  InitGRP();
167  }
168  else {
169  AliInfo("Run number is not available at this stage, InitGRP will be called in the execution loop");
170  }
171 }
172 
173 //______________________________________________________________________________
175 {
176  // Connect the input data, create CDB manager.
177  AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
178  if (!mgr) AliFatal("No analysis manager");
179  AliAnalysisTask::ConnectInputData(option);
180  Int_t run = AliAnalysisManager::GetAnalysisManager()->GetRunFromPath();
181  if (run<=0) {
182  AliWarning("AliTaskCDBconnect: Could not set run from path");
183  return;
184  }
185  if (fRun != run) {
186  fRun = run;
187  InitGRP();
188  }
189 }
190 
191 
192 //______________________________________________________________________________
194 {
195  // Execute all supplied analysis of one event. Notify run change via RunChanged().
196  AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
197  if (!mgr) AliFatal("No analysis manager");
198  AliInputEventHandler* inp = (AliInputEventHandler*)mgr->GetInputEventHandler();
199  if (!inp) AliFatal("No input event handler connected");
200  AliVEvent* ev = inp->GetEvent();
201  if (!ev) AliFatal("No event returned");
202  int run = ev->GetRunNumber();
203  // Intercept when the run number changed
204  if (fRun != run && run>0) {
205  fRun = run;
206  InitGRP();
207  }
208 }
209 
210 //______________________________________________________________________________
211 void AliTaskCDBconnect::SetSpecificStorage(const char* calibType, const char* dbString, Int_t version, Int_t subVersion)
212 {
213  // Set a specific storage
214  TNamed *nmpath = new TNamed(calibType,dbString);
215  if (version<0) version = -1;
216  if (subVersion<0) subVersion = -1;
217  nmpath->SetUniqueID((UInt_t(version+1)<<16)+UInt_t(subVersion+1));
218  fSpecCDBUri.AddLast(nmpath);
219 }
virtual void CreateOutputObjects()
TSystem * gSystem
virtual void Exec(Option_t *option)
int Int_t
Definition: External.C:63
unsigned int UInt_t
Definition: External.C:33
AliGRPManager * fGRPManager
void SetSpecificStorage(const char *calibType, const char *dbString, Int_t version=-1, Int_t subVersion=-1)
virtual void ConnectInputData(Option_t *option="")
const char Option_t
Definition: External.C:48
bool Bool_t
Definition: External.C:53