]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGPP/AliTaskCDBconnect.cxx
guess the run number from the input file path
[u/mrichter/AliRoot.git] / PWGPP / AliTaskCDBconnect.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 "AliTaskCDBconnect.h"
17
18 #include <TChain.h>
19 #include <TFile.h>
20 #include <TGeoGlobalMagField.h>
21 #include "TGeoManager.h"
22  
23 #include "AliAnalysisManager.h"
24 #include "AliGeomManager.h"
25 #include "AliCDBManager.h"
26 #include "AliGRPManager.h"
27 #include "AliESDEvent.h"
28 #include "AliESDInputHandler.h"
29 #include "AliLog.h"
30
31 ClassImp(AliTaskCDBconnect)
32
33 //______________________________________________________________________________
34 AliTaskCDBconnect::AliTaskCDBconnect():
35            AliAnalysisTask(),
36            fRun(0),
37            fRunChanged(kFALSE),
38            fESDhandler(NULL),
39            fESD(NULL),
40            fGRPManager(NULL)
41 {
42 // Dummy constructor
43 }
44
45 //______________________________________________________________________________
46 AliTaskCDBconnect::AliTaskCDBconnect(const char* name, const char *storage, Int_t run)
47           :AliAnalysisTask(name, "ESD analysis tender car"),
48            fRun(run),
49            fRunChanged(kFALSE),
50            fESDhandler(NULL),
51            fESD(NULL),
52            fGRPManager(NULL)
53 {
54 // Default constructor
55   AliCDBManager *cdb = AliCDBManager::Instance();
56   cdb->SetDefaultStorage(storage);
57   cdb->SetRun(run);
58   DefineInput (0, TChain::Class());
59 }
60
61 //______________________________________________________________________________
62 AliTaskCDBconnect::~AliTaskCDBconnect()
63 {
64 // Destructor
65   delete fGRPManager;
66 }  
67
68 //______________________________________________________________________________
69 void AliTaskCDBconnect::LocalInit()
70 {
71 // Init CDB locally if run number is defined.
72 }
73   
74 //______________________________________________________________________________
75 void AliTaskCDBconnect::ConnectInputData(Option_t* /*option*/)
76 {
77 // Connect the input data, create CDB manager.
78 }
79
80 //______________________________________________________________________________
81 void AliTaskCDBconnect::InitGRP()
82 {
83 // Initialize geometry and mag. field
84   if (!fGRPManager) {
85   // magnetic field
86     if (!TGeoGlobalMagField::Instance()->GetField()) {
87       printf("AliCDBconnect: #### Loading field map...\n");
88       fGRPManager = new AliGRPManager();
89       if(!fGRPManager->ReadGRPEntry()) { 
90         AliError("Cannot get GRP entry"); 
91       }
92       if( !fGRPManager->SetMagField() ) { 
93         AliError("Problem with magnetic field setup"); 
94       }
95     }
96
97     // geometry
98     if (!gGeoManager) {
99       printf("AliCDBconnect: #### Loading geometry...\n");
100       AliGeomManager::LoadGeometry("geometry.root");
101       if( !AliGeomManager::ApplyAlignObjsFromCDB("GRP ITS TPC TRD") ) {
102         AliError("Problem with align objects"); 
103       }
104     }  
105   }  
106 }
107
108 //______________________________________________________________________________
109 void AliTaskCDBconnect::CreateOutputObjects()
110 {
111 // Init CDB locally if run number is defined.
112   AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
113   if (!mgr) AliFatal("No analysis manager");
114   fESDhandler = dynamic_cast<AliESDInputHandler *>(mgr->GetInputEventHandler());
115     
116   if (!fESDhandler) {
117      AliFatal("No ESD input event handler connected");
118      return;
119   }   
120   // Try to get event number before the first event is read (this has precedence
121   // over existing fRun)
122   Int_t run = mgr->GetRunFromPath();
123   if (!run && !fRun) {
124      AliError("AliTaskCDBconnect: Run not set - no CDB connection");
125      return;
126   }
127   // Create CDB manager
128   AliCDBManager *cdb = AliCDBManager::Instance();
129   // If CDB is already locked, return
130   if (cdb->GetLock()) return;
131   // SetDefault storage. Specific storages must be set by TaskCDBconnectSupply::Init()
132   //  cdb->SetDefaultStorage("local://$ALICE_ROOT/OCDB");
133 //  if (!cdb->GetRaw()) {
134 //     cdb->SetDefaultStorage("raw://");
135 //  }   
136   if (run && (run != fRun)) {
137      fRunChanged = kTRUE;
138      fRun = run;
139   } else {
140      fRunChanged = kFALSE;
141   }
142   // Set run
143   if (fRunChanged || !fGRPManager) {
144      printf("AliCDBconnect: #### Setting run to: %d\n", fRun);
145      cdb->SetRun(fRun);
146      // Initialize GRP manager only once
147      if (fRun) InitGRP();
148   }   
149 }
150
151 //______________________________________________________________________________
152 Bool_t AliTaskCDBconnect::Notify()
153 {
154 // Init CDB locally if run number is defined.
155   CreateOutputObjects();
156   return kTRUE;
157 }
158
159 //______________________________________________________________________________
160 void AliTaskCDBconnect::Exec(Option_t* /*option*/)
161 {
162 //
163 // Execute all supplied analysis of one event. Notify run change via RunChanged().
164   fESD = fESDhandler->GetEvent();
165   // Intercept when the run number changed
166   if (fRun != fESD->GetRunNumber()) {
167     fRunChanged = kTRUE;
168     fRun = fESD->GetRunNumber();
169     CreateOutputObjects();
170   }
171 }
172
173 //______________________________________________________________________________
174 void AliTaskCDBconnect::Terminate(Option_t *)
175 {
176 // Initialize CDB also in Terminate
177 //   CreateOutputObjects();
178 }