]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGLF/FORWARD/analysis2/AliBaseESDTask.cxx
Merge branch 'feature-movesplit'
[u/mrichter/AliRoot.git] / PWGLF / FORWARD / analysis2 / AliBaseESDTask.cxx
1 #include "AliBaseESDTask.h"
2 #include "AliFMDEventInspector.h"
3 #include "AliForwardCorrectionManager.h"
4 #include "AliForwardUtil.h"
5 #include "AliFMDCorrELossFit.h"
6 #include <AliAnalysisManager.h>
7 #include <AliAODHandler.h>
8 #include <AliLog.h>
9 #include <AliESDEvent.h>
10 #include <TROOT.h>
11 #include <TSystem.h>
12 #include <TInterpreter.h>
13 #include <iostream>
14 #include <iomanip>
15
16 //____________________________________________________________________
17 AliBaseESDTask::AliBaseESDTask()
18   : AliAnalysisTaskSE(), 
19     fFirstEvent(true),
20     fList(0),
21     fResults(0),
22     fNeededCorrections(0),
23     fExtraCorrections(0),
24     fCloneList(false),
25     fCorrManager(0)
26 {}
27
28 //____________________________________________________________________
29 AliBaseESDTask::AliBaseESDTask(const char* name, const char* title,
30                                AliCorrectionManagerBase* manager)
31   : AliAnalysisTaskSE(name), 
32     fFirstEvent(true),
33     fList(0),
34     fResults(0),
35     fNeededCorrections(0),
36     fExtraCorrections(0),
37     fCloneList(false),
38     fCorrManager(0)
39 {
40   // The line below doesn't actually do the job - when we're
41   // constructing, the derived class ins't set yet and this explicitly
42   // points to an object of _this_ class.
43   // SetTitle(title && title[0] != '\0' ? title : this->ClassName());
44   SetTitle(title && title[0] != '\0' ? title : "");
45   fCorrManager = manager;
46   // if (!manager) 
47   //   AliFatal("Must pass in a valid correction manager object!");
48   fBranchNames = 
49     "ESD:AliESDRun.,AliESDHeader.,AliMultiplicity.,"
50     "AliESDFMD.,SPDVertex.,PrimaryVertex.";
51
52   DefineOutput(1, TList::Class());
53   DefineOutput(2, TList::Class());
54 }
55 //____________________________________________________________________
56 Bool_t
57 AliBaseESDTask::Connect(const char* sumFile, 
58                         const char* resFile,
59                         Bool_t      old)
60 {
61   AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
62   if (!mgr) {
63     Error("AddTaskForwardMult", "No analysis manager to connect to.");
64     return false;
65   }   
66
67   // Add to the manager 
68   mgr->AddTask(this);
69   
70   // Create and connect output containers 
71   TString sumOut;
72   TString resOut;
73   if      (sumFile && sumFile[0] != '\0') sumOut = sumFile;
74   if      (resFile && resFile[0] != '\0') resOut = resFile;
75   else if (sumFile && sumFile[0] != '\0') resOut = sumFile;
76   // If the string is null or 'default' connect to standard output file 
77   if (sumOut.IsNull() || sumOut.EqualTo("default", TString::kIgnoreCase)) 
78     sumOut = AliAnalysisManager::GetCommonFileName();
79   // If the string is null or 'default' connect to standard output file 
80   if (resOut.IsNull() || resOut.EqualTo("default", TString::kIgnoreCase)) 
81     resOut = AliAnalysisManager::GetCommonFileName();
82
83   // Always connect input 
84   mgr->ConnectInput(this, 0, mgr->GetCommonInputContainer());
85
86   // Connect sum list unless the output 'none' is specified
87   if (!sumOut.EqualTo("none", TString::kIgnoreCase)) {
88     TString sumName(Form("%s%s", old ? "Forward" : GetName(), old ? "" : "Sums"));
89     AliAnalysisDataContainer* sumCon = 
90       mgr->CreateContainer(sumName, TList::Class(), 
91                            AliAnalysisManager::kOutputContainer, sumOut);
92     mgr->ConnectOutput(this, 1, sumCon);
93   }
94   // Connect the result list unless the output 'none' is specified
95   if (!resOut.EqualTo("none", TString::kIgnoreCase)) {
96     TString resName(Form("%sResults", GetName()));
97     AliAnalysisDataContainer* resCon = 
98       mgr->CreateContainer(resName, TList::Class(), 
99                            AliAnalysisManager::kParamContainer, resOut);
100     mgr->ConnectOutput(this, 2, resCon);
101   }
102   
103   return true;
104 }
105
106 //____________________________________________________________________
107 TAxis*
108 AliBaseESDTask::DefaultEtaAxis() const
109 {
110   static TAxis* a = new TAxis(200, -4, 6);
111   return a;
112 }
113 //____________________________________________________________________
114 TAxis*
115 AliBaseESDTask::DefaultVertexAxis() const
116 {
117   static TAxis* a = AliForwardUtil::MakeFullIpZAxis(20);
118   return a;
119 }
120 //____________________________________________________________________
121 void
122 AliBaseESDTask::SetDebug(Int_t dbg)
123 {
124   // 
125   // Set debug level 
126   // 
127   // Parameters:
128   //    dbg debug level
129   //
130   GetEventInspector().SetDebug(dbg);
131 }
132
133
134 //____________________________________________________________________
135 Bool_t 
136 AliBaseESDTask::Configure(const char* macro)
137 {
138   // --- Configure the task ------------------------------------------
139   TString macroPath(gROOT->GetMacroPath());
140   if (!macroPath.Contains("$(ALICE_ROOT)/PWGLF/FORWARD/analysis2")) { 
141     macroPath.Append(":$(ALICE_ROOT)/PWGLF/FORWARD/analysis2");
142     gROOT->SetMacroPath(macroPath);
143   }
144   TString mac(macro);
145   if (mac.EqualTo("-default-")) mac = DefaultConfig();
146   const char* config = gSystem->Which(gROOT->GetMacroPath(), mac.Data());
147   if (!config) {
148     AliWarningF("%s not found in %s", mac.Data(), gROOT->GetMacroPath());
149     return false;
150   }
151   if (fTitle.IsNull()) fTitle = this->ClassName();
152
153   AliInfoF("Loading configuration of '%s' from %s",  ClassName(), config);
154   gROOT->Macro(Form("%s((%s*)%p)", config, GetTitle(), this));
155
156   Info("Configure", "Unloading configuration script");
157   gInterpreter->UnloadFile(config);
158
159   delete config;
160  
161  return true;
162 }
163
164 //____________________________________________________________________
165 void 
166 AliBaseESDTask::LocalInit() 
167
168   fFirstEvent = true; 
169   DGUARD(fDebug,1,"Doing local initialization");
170   Setup(); 
171 }
172
173 //____________________________________________________________________
174 void
175 AliBaseESDTask::UserCreateOutputObjects()
176 {
177   // 
178   // Create output objects 
179   // 
180   //
181   DGUARD(fDebug,1,"Create user ouput");
182   fList = new TList;
183   fList->SetName(Form("%sSums", GetName()));
184   fList->SetOwner();
185   
186   AliAnalysisManager* am = AliAnalysisManager::GetAnalysisManager();
187   AliAODHandler*      ah = 
188     dynamic_cast<AliAODHandler*>(am->GetOutputEventHandler());
189   //if (!ah) AliFatal("No AOD output handler set in analysis manager");
190   if (ah)  CreateBranches(ah);
191    
192   GetEventInspector().CreateOutputObjects(fList);
193
194   if (!Book()) AliFatalF("Failed to book output objects for %s", GetName());
195
196   // gSystem->Exec("root-config --version --prefix");
197   PostData(1, fList);
198 }
199
200 //____________________________________________________________________
201 void
202 AliBaseESDTask::UserExec(Option_t*)
203 {
204   // Call pre-event setup 
205   PreEvent();
206
207   // Read in selected branches 
208   LoadBranches();
209
210   // Get the input data 
211   AliESDEvent* esd = GetESDEvent();
212   if (!esd) return;
213
214   // Call the user code with our event passed in 
215   Event(*esd);
216   // if (!Event(*esd)) {
217   //   AliWarningF("Failed to process the event for %s", GetName());
218   //   return;
219   // }
220
221   // Post data 
222   PostData(1, fList);
223
224   // Call post-event processing 
225   PostEvent();
226 }
227
228 //____________________________________________________________________
229 void
230 AliBaseESDTask::Terminate(Option_t*)
231 {
232   TList* list = dynamic_cast<TList*>(GetOutputData(1));
233   if (!list) {
234     AliError(Form("No output list defined (%p)", GetOutputData(1)));
235     if (GetOutputData(1)) GetOutputData(1)->Print();
236     return;
237   }
238
239   // Assign to our internal variable for use by sub-classes 
240   fList = list;
241
242   // Create our output container 
243   TString resName(Form("%sResults", GetName()));
244   if (fCloneList)
245     fResults = static_cast<TList*>(fList->Clone(resName));
246   else {
247     fResults = new TList;
248     fResults->SetName(resName);
249   }
250   fResults->SetOwner();
251
252   // Now call user defined routines 
253   if (!Finalize()) {
254     AliErrorF("Failed to finalize this task (%s)", GetName());
255     return;
256   }
257
258   PostData(2, fResults);
259 }
260
261 //____________________________________________________________________
262 Bool_t 
263 AliBaseESDTask::PreData(const TAxis&, const TAxis&) 
264
265   return true; 
266 }
267
268
269 //____________________________________________________________________
270 Bool_t 
271 AliBaseESDTask::CheckCorrections(UInt_t what) const
272 {
273   // 
274   // Check if all needed corrections are there and accounted for.  If not,
275   // do a Fatal exit 
276   // 
277   // Parameters:
278   //    what Which corrections is needed
279   // 
280   // Return:
281   //    true if all present, false otherwise
282   //  
283   DGUARD(fDebug,1,"Checking corrections 0x%x", what);
284   if (what == 0) return true;
285
286   AliCorrectionManagerBase* cm = GetManager();
287   if (!cm) {
288     AliErrorF("Check corrections=0x%x not null, "
289               "but no correction manager defined!", 
290               what);
291     return false;
292   }
293   Bool_t ret = cm->CheckCorrections(what);
294   return ret;
295 }
296 //____________________________________________________________________
297 Bool_t
298 AliBaseESDTask::ReadCorrections(const TAxis*& pe, 
299                                const TAxis*& pv, 
300                                Bool_t        mc,
301                                Bool_t        sat)
302 {
303   //
304   // Read corrections
305   //
306   //
307   UInt_t what = fNeededCorrections|fExtraCorrections;
308   
309   DGUARD(fDebug,1,"Read corrections 0x%x", what);
310
311   AliCorrectionManagerBase* cm = GetManager();
312   if (!cm && fNeededCorrections) {
313     AliErrorF("Needed/extra corrections=0x%x/0x%x not null, "
314               "but no correction manager defined!", 
315               fNeededCorrections, fExtraCorrections);
316     return false;
317   }
318   if (!cm || !what) {
319     // In case we have no needed corrections, we can return here 
320     if (!pe) pe = DefaultEtaAxis();
321     if (!pv) pv = DefaultVertexAxis();
322     return true;
323   }
324   cm->EnableCorrections(what);
325   if (!cm->InitCorrections(GetEventInspector().GetRunNumber(),
326                            GetEventInspector().GetCollisionSystem(),
327                            GetEventInspector().GetEnergy(),
328                            GetEventInspector().GetField(),
329                            mc,
330                            sat,
331                            false)) { 
332     AliWarning("Failed to read in some corrections, making task zombie");
333     return false;
334   }
335   if (!CheckCorrections(fNeededCorrections)) return false;
336
337   // Sett our persistency pointer 
338   // fCorrManager = &fcm;
339
340   // Get the eta axis from the secondary maps - if read in
341   if (!pe) {
342     pe = cm->GetEtaAxis();
343     if (!pe) pe = DefaultEtaAxis();
344   }
345   // Get the vertex axis from the secondary maps - if read in
346   if (!pv) {
347     pv = cm->GetVertexAxis();
348     if (!pv) pv = DefaultVertexAxis();
349   }
350
351   return true;
352 }
353 //____________________________________________________________________
354 AliESDEvent*
355 AliBaseESDTask::GetESDEvent()
356 {
357   //
358   // Get the ESD event. IF this is the first event, initialise
359   //
360   DGUARD(fDebug,1,"Get the ESD event");
361
362   // If we're marked as a zombie, do nothing and return a null
363   if (IsZombie()) return 0;
364
365   // Try to get the ESD event 
366   AliESDEvent* esd = dynamic_cast<AliESDEvent*>(InputEvent());
367   if (!esd) {
368     AliWarning("No ESD event found for input event");
369     return 0;
370   }
371
372   // --- Load the data -----------------------------------------------
373   LoadBranches();
374
375   if (!fFirstEvent || !esd->GetESDRun()) return esd;
376
377   // On the first event, initialize the parameters
378   GetEventInspector().SetMC(MCEvent());
379   GetEventInspector().ReadRunDetails(esd);
380   
381   AliInfoF("Initializing with parameters from the ESD:\n"
382            "         AliESDEvent::GetBeamEnergy()   ->%f\n"
383            "         AliESDEvent::GetBeamType()     ->%s\n"
384            "         AliESDEvent::GetCurrentL3()    ->%f\n"
385            "         AliESDEvent::GetMagneticField()->%f\n"
386            "         AliESDEvent::GetRunNumber()    ->%d",
387            esd->GetBeamEnergy(),
388            esd->GetBeamType(),
389            esd->GetCurrentL3(),
390            esd->GetMagneticField(),
391            esd->GetRunNumber());
392
393   PreCorrections(esd);
394
395   fFirstEvent = false;
396   
397   const   TAxis* pe = 0;
398   const   TAxis* pv = 0;
399   Bool_t  mc        = IsMC();
400   Bool_t  sat       = false;
401   Bool_t  ret       = ReadCorrections(pe, pv, mc, sat);
402   if (!ret) {
403     AliError("Failed to read corrections, making this a zombie");
404     SetZombie(true);
405     return 0;
406   }
407   Printf("Vertex axis: %p   Eta axis: %p", pv, pe);
408   if (!pv) AliFatal("No vertex axis defined");
409   if (!pe) AliFatal("No eta axis defined");
410
411   // Initialize the event inspector 
412   GetEventInspector().SetupForData(*pv);
413   
414   // Initialize the remaining stuff 
415   if (!PreData(*pv, *pe)) {
416     AliError("Failed to initialize sub-algorithms, making this a zombie");
417     SetZombie(true);
418     return 0;
419   }
420   
421   this->Print("R");
422
423   return esd;
424 }
425
426 //____________________________________________________________________
427 void
428 AliBaseESDTask::MarkEventForStore() const
429 {
430   // Make sure the AOD tree is filled 
431   DGUARD(fDebug,3,"Mark AOD event for storage");
432   AliAnalysisManager* am = AliAnalysisManager::GetAnalysisManager();
433   AliAODHandler*      ah = 
434     dynamic_cast<AliAODHandler*>(am->GetOutputEventHandler());
435   if (ah) ah->SetFillAOD(kTRUE);
436 }
437 #define PF(N,V,...)                                     \
438   AliForwardUtil::PrintField(N,V, ## __VA_ARGS__)
439 #define PFB(N,FLAG)                             \
440   do {                                                                  \
441     AliForwardUtil::PrintName(N);                                       \
442     std::cout << std::boolalpha << (FLAG) << std::noboolalpha << std::endl; \
443   } while(false)
444 #define PFV(N,VALUE)                                    \
445   do {                                                  \
446     AliForwardUtil::PrintName(N);                       \
447     std::cout << (VALUE) << std::endl; } while(false)
448
449 //____________________________________________________________________
450 void
451 AliBaseESDTask::Print(Option_t* option) const
452 {
453   // 
454   // Print information 
455   // 
456   // Parameters:
457   //    option Not used
458   //  
459   std::cout << std::setfill('=') << std::setw(75) << "=" 
460             << std::setfill(' ') << std::endl;
461   AliForwardUtil::PrintTask(*this);
462   gROOT->IncreaseDirLevel();
463   PF("Off-line trigger mask", "0x%0x", fOfflineTriggerMask);
464   if (GetManager()) GetManager()->Print(option);
465   else  PF("No correction manager","");
466
467   GetEventInspector().Print(option);
468   gROOT->DecreaseDirLevel();
469 }
470 //
471 // EOF
472 //