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