]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGLF/FORWARD/analysis2/AliForwardQATask.cxx
Clean-up of code to conform with the style used
[u/mrichter/AliRoot.git] / PWGLF / FORWARD / analysis2 / AliForwardQATask.cxx
1 // 
2 // Calculate the multiplicity in the forward regions event-by-event 
3 // 
4 // Inputs: 
5 //   - AliESDEvent 
6 //
7 // Outputs: 
8 //   - AliAODForwardMult 
9 // 
10 // Histograms 
11 //   
12 // Corrections used 
13 //
14 #include "AliForwardQATask.h"
15 #include "AliForwardUtil.h"
16 #include "AliTriggerAnalysis.h"
17 #include "AliPhysicsSelection.h"
18 #include "AliLog.h"
19 #include "AliESDEvent.h"
20 #include "AliAODHandler.h"
21 #include "AliMultiplicity.h"
22 #include "AliInputEventHandler.h"
23 #include "AliForwardCorrectionManager.h"
24 #include "AliAnalysisManager.h"
25 #include "AliAODForwardMult.h"
26 #include <TH1.h>
27 #include <TDirectory.h>
28 #include <TTree.h>
29 #include <TROOT.h>
30 #include <TStopwatch.h>
31
32 //====================================================================
33 AliForwardQATask::AliForwardQATask()
34   : AliAnalysisTaskSE(),
35     fEnableLowFlux(false), 
36     fFirstEvent(true),
37     fCorrManager(0),
38     fESDFMD(),
39     fHistos(),
40     fEventInspector(),
41     fEnergyFitter(),
42     fSharingFilter(),
43     fDensityCalculator(),
44     fList(0),
45     fDebug(0)
46 {
47   // 
48   // Constructor
49   //
50 }
51
52 //____________________________________________________________________
53 AliForwardQATask::AliForwardQATask(const char* name)
54   : AliAnalysisTaskSE(name),
55     fEnableLowFlux(false), 
56     fFirstEvent(true),
57     fCorrManager(0),
58     fESDFMD(),
59     fHistos(),
60     fEventInspector("event"),
61     fEnergyFitter("energy"),
62     fSharingFilter("sharing"), 
63     fDensityCalculator("density"),
64     fList(0),
65     fDebug(0)
66 {
67   // 
68   // Constructor 
69   // 
70   // Parameters:
71   //    name Name of task 
72   //
73   DefineOutput(1, TList::Class());
74   DefineOutput(2, TList::Class());
75   fCorrManager = &AliForwardCorrectionManager::Instance(); 
76   fEnergyFitter.SetNParticles(1); // Just find the 1st peak 
77   fEnergyFitter.SetDoMakeObject(false); 
78   fEnergyFitter.SetUseIncreasingBins(true);
79   fEnergyFitter.SetDoFits(kTRUE);
80   fEnergyFitter.SetLowCut(0.4);
81   fEnergyFitter.SetFitRangeBinWidth(4);
82   fEnergyFitter.SetMinEntries(1000);
83 }
84
85 //____________________________________________________________________
86 AliForwardQATask::AliForwardQATask(const AliForwardQATask& o)
87   : AliAnalysisTaskSE(o),
88     fEnableLowFlux(o.fEnableLowFlux), 
89     fFirstEvent(o.fFirstEvent),
90     fCorrManager(o.fCorrManager),
91     fESDFMD(o.fESDFMD),
92     fHistos(o.fHistos),
93     fEventInspector(o.fEventInspector),
94     fEnergyFitter(o.fEnergyFitter),
95     fSharingFilter(o.fSharingFilter),
96     fDensityCalculator(o.fDensityCalculator),
97     fList(o.fList),
98     fDebug(o.fDebug) 
99 {
100   // 
101   // Copy constructor 
102   // 
103   // Parameters:
104   //    o Object to copy from 
105   //
106   DefineOutput(1, TList::Class());
107   DefineOutput(2, TList::Class());
108 }
109
110 //____________________________________________________________________
111 AliForwardQATask&
112 AliForwardQATask::operator=(const AliForwardQATask& o)
113 {
114   // 
115   // Assignment operator 
116   // 
117   // Parameters:
118   //    o Object to assign from 
119   // 
120   // Return:
121   //    Reference to this object 
122   //
123   if (&o == this) return *this;
124   AliAnalysisTaskSE::operator=(o);
125
126   fEnableLowFlux     = o.fEnableLowFlux;
127   fFirstEvent        = o.fFirstEvent;
128   fCorrManager       = o.fCorrManager;
129   fEventInspector    = o.fEventInspector;
130   fEnergyFitter      = o.fEnergyFitter;
131   fSharingFilter     = o.fSharingFilter;
132   fDensityCalculator = o.fDensityCalculator;
133   fHistos            = o.fHistos;
134   fList              = o.fList;
135   fDebug             = o.fDebug;
136
137   return *this;
138 }
139
140 //____________________________________________________________________
141 void
142 AliForwardQATask::SetDebug(Int_t dbg)
143 {
144   // 
145   // Set debug level 
146   // 
147   // Parameters:
148   //    dbg Debug level
149   //
150   fDebug = dbg;
151   fEventInspector.SetDebug(dbg);
152   fEnergyFitter.SetDebug(dbg);
153   fSharingFilter.SetDebug(dbg);
154   fDensityCalculator.SetDebug(dbg);
155 }
156
157 //____________________________________________________________________
158 Bool_t 
159 AliForwardQATask::CheckCorrections(UInt_t what) const
160 {
161   // 
162   // Check if all needed corrections are there and accounted for.  If not,
163   // do a Fatal exit 
164   // 
165   // Parameters:
166   //    what Which corrections is needed
167   // 
168   // Return:
169   //    true if all present, false otherwise
170   //  
171
172   AliForwardCorrectionManager& fcm = AliForwardCorrectionManager::Instance();
173   // Check that we have the energy loss fits, needed by 
174   //   AliFMDSharingFilter 
175   //   AliFMDDensityCalculator 
176   if (what & AliForwardCorrectionManager::kELossFits && !fcm.GetELossFit()) { 
177     AliWarning("No energy loss fits");
178     return false;
179   }
180   return true;
181 }
182
183 //____________________________________________________________________
184 Bool_t
185 AliForwardQATask::ReadCorrections(const TAxis*& pe, 
186                                   const TAxis*& pv, 
187                                   Bool_t        mc)
188 {
189   //
190   // Read corrections
191   //
192   //
193   UInt_t what = AliForwardCorrectionManager::kAll;
194   what ^= AliForwardCorrectionManager::kDoubleHit;
195   what ^= AliForwardCorrectionManager::kVertexBias;
196   what ^= AliForwardCorrectionManager::kAcceptance;
197   what ^= AliForwardCorrectionManager::kMergingEfficiency;
198
199   AliForwardCorrectionManager& fcm = AliForwardCorrectionManager::Instance();
200   if (!fcm.Init(GetEventInspector().GetCollisionSystem(),
201                 GetEventInspector().GetEnergy(),
202                 GetEventInspector().GetField(),
203                 mc,
204                 what)) return false;
205   if (!CheckCorrections(what)) {
206     return false;
207   }
208
209   // Sett our persistency pointer 
210   // fCorrManager = &fcm;
211
212   // Get the eta axis from the secondary maps - if read in
213   if (!pe) {
214     pe = fcm.GetEtaAxis();
215     if (!pe) AliFatal("No eta axis defined");
216   }
217   // Get the vertex axis from the secondary maps - if read in
218   if (!pv) {
219     pv = fcm.GetVertexAxis();
220     if (!pv) AliFatal("No vertex axis defined");
221   }
222
223   return true;
224 }
225
226 //____________________________________________________________________
227 AliESDEvent*
228 AliForwardQATask::GetESDEvent()
229 {
230   //
231   // Get the ESD event. IF this is the first event, initialise
232   //
233   if (IsZombie()) return 0;
234   AliESDEvent* esd = dynamic_cast<AliESDEvent*>(InputEvent());
235   if (!esd) {
236     AliWarning("No ESD event found for input event");
237     return 0;
238   }
239
240   // On the first event, initialize the parameters
241   if (fFirstEvent && esd->GetESDRun()) {
242     GetEventInspector().ReadRunDetails(esd);
243
244     AliInfoF("Initializing with parameters from the ESD:\n"
245              "         AliESDEvent::GetBeamEnergy()   ->%f\n"
246              "         AliESDEvent::GetBeamType()     ->%s\n"
247              "         AliESDEvent::GetCurrentL3()    ->%f\n"
248              "         AliESDEvent::GetMagneticField()->%f\n"
249              "         AliESDEvent::GetRunNumber()    ->%d",
250              esd->GetBeamEnergy(),
251              esd->GetBeamType(),
252              esd->GetCurrentL3(),
253              esd->GetMagneticField(),
254              esd->GetRunNumber());
255
256
257     if (!InitializeSubs()) {
258       AliWarning("Initialisation of sub algorithms failed!");
259       SetZombie(true);
260       esd = 0;
261       return 0;
262     }
263     AliInfoF("Clearing first event flag from %s to false", 
264              fFirstEvent ? "true" : "false");
265     fFirstEvent = false;
266   }
267   return esd;
268 }
269 //____________________________________________________________________
270 Bool_t
271 AliForwardQATask::InitializeSubs()
272 {
273   // 
274   // Initialise the sub objects and stuff.  Called on first event 
275   // 
276   //
277   const TAxis* pe = 0;
278   const TAxis* pv = 0;
279
280   if (!ReadCorrections(pe,pv))  { 
281     AliWarning("Using default binning");
282     pv = new TAxis(10,-10, 10);
283     pe = new TAxis(240,-6,6);
284   }
285
286   fHistos.Init(*pe);
287
288   fEventInspector.Init(*pv);
289   fEnergyFitter.Init(*pe);
290   fSharingFilter.Init();
291   fDensityCalculator.Init(*pe);
292
293   this->Print();
294
295   return true;
296 }
297
298 //____________________________________________________________________
299 void
300 AliForwardQATask::UserCreateOutputObjects()
301 {
302   // 
303   // Create output objects 
304   // 
305   //
306   fList = new TList;
307   fList->SetOwner();
308   
309   fEventInspector.DefineOutput(fList);
310   fEnergyFitter.DefineOutput(fList);
311   fSharingFilter.DefineOutput(fList);
312   fDensityCalculator.DefineOutput(fList);
313
314   PostData(1, fList);
315 }
316 //____________________________________________________________________
317 void
318 AliForwardQATask::UserExec(Option_t*)
319 {
320   // 
321   // Process each event 
322   // 
323   // Parameters:
324   //    option Not used
325   //  
326
327   // static Int_t cnt = 0;
328   // cnt++;
329   // Get the input data 
330   AliESDEvent* esd = GetESDEvent();
331   if (!esd) { 
332     AliWarning("Got no ESD event");
333     return;
334   }
335   if (fFirstEvent) { 
336     // If the first event flag wasn't cleared in the above call to
337     // GetESDEvent, we should not do anything, since nothing has been
338     // initialised yet, so we opt out here (with a warning) 
339     AliWarning("Nothing has been initialized yet, opt'ing out");
340     return;
341   }
342
343   // Clear stuff 
344   fHistos.Clear();
345   fESDFMD.Clear();
346   
347   Bool_t   lowFlux   = kFALSE;
348   UInt_t   triggers  = 0;
349   UShort_t ivz       = 0;
350   Double_t vz        = 0;
351   Double_t cent      = -1;
352   UShort_t nClusters = 0;
353   UInt_t   found     = fEventInspector.Process(esd, triggers, lowFlux, 
354                                                ivz, vz, cent, nClusters);
355   
356   if (found & AliFMDEventInspector::kNoEvent)    return;
357   if (found & AliFMDEventInspector::kNoTriggers) return;
358   if (found & AliFMDEventInspector::kNoSPD)      return;
359   if (found & AliFMDEventInspector::kNoFMD)      return;
360   if (found & AliFMDEventInspector::kNoVertex)   return;
361   if (triggers & AliAODForwardMult::kPileUp)     return;
362   if (found & AliFMDEventInspector::kBadVertex)  return;
363
364   // We we do not want to use low flux specific code, we disable it here. 
365   if (!fEnableLowFlux) lowFlux = false;
366
367   // Get FMD data 
368   AliESDFMD* esdFMD = esd->GetFMDData();
369   
370   // Run the energy loss fitter 
371   if (!fEnergyFitter.Accumulate(*esdFMD, cent, 
372                                 triggers & AliAODForwardMult::kEmpty)) {
373     AliWarning("Energy fitter failed");
374     return;
375   }
376   
377   //  // Apply the sharing filter (or hit merging or clustering if you like)
378   if (!fSharingFilter.Filter(*esdFMD, lowFlux, fESDFMD, vz)) { 
379     AliWarning("Sharing filter failed!");
380     return;
381   }
382  
383   // Calculate the inclusive charged particle density 
384   if (!fDensityCalculator.Calculate(fESDFMD, fHistos, ivz, lowFlux)) { 
385     // if (!fDensityCalculator.Calculate(*esdFMD, fHistos, ivz, lowFlux)) { 
386     AliWarning("Density calculator failed!");
387     return;
388   }
389   
390   PostData(1, fList);
391 }
392
393 //____________________________________________________________________
394 void
395 AliForwardQATask::Terminate(Option_t*)
396 {
397   // 
398   // End of job
399   // 
400   // Parameters:
401   //    option Not used 
402   //
403   if (fDebug) AliInfo("In Forwards terminate");
404   TStopwatch swt;
405   swt.Start();
406
407   TList* list = dynamic_cast<TList*>(GetOutputData(1));
408   if (!list) {
409     AliError(Form("No output list defined (%p)", GetOutputData(1)));
410     if (GetOutputData(1)) GetOutputData(1)->Print();
411     return;
412   }
413   
414   // Get our histograms from the container 
415   TH1I* hEventsTr    = 0;//static_cast<TH1I*>(list->FindObject("nEventsTr"));
416   TH1I* hEventsTrVtx = 0;//static_cast<TH1I*>(list->FindObject("nEventsTrVtx"));
417   TH1I* hTriggers    = 0;
418   if (!fEventInspector.FetchHistograms(list, hEventsTr, 
419                                        hEventsTrVtx, hTriggers)) { 
420     AliError(Form("Didn't get histograms from event selector "
421                   "(hEventsTr=%p,hEventsTrVtx=%p)", 
422                   hEventsTr, hEventsTrVtx));
423     return;
424   }
425
426   TStopwatch swf;
427   swf.Start();
428   fEnergyFitter.Fit(list);
429   swf.Stop();
430   AliInfoF("Fitting took %d real-time seconds, and %f CPU seconds", 
431            Int_t(swf.RealTime()), swf.CpuTime());
432
433   fSharingFilter.ScaleHistograms(list,Int_t(hEventsTr->Integral()));
434   fDensityCalculator.ScaleHistograms(list,Int_t(hEventsTrVtx->Integral()));
435
436   // Make a deep copy and post that as output 2 
437   TList* list2 = static_cast<TList*>(list->Clone(Form("%sResults", 
438                                                       list->GetName())));
439   if (fDebug) AliInfoF("Posting post processing results to %s", 
440                        list2->GetName());
441   list2->SetOwner();
442   PostData(2, list2);
443
444   swt.Stop();
445   AliInfoF("Terminate took %d real-time seconds, and %f CPU seconds", 
446            Int_t(swt.RealTime()), swt.CpuTime());
447
448 }
449
450 //____________________________________________________________________
451 void
452 AliForwardQATask::Print(Option_t* option) const
453 {
454   // 
455   // Print information 
456   // 
457   // Parameters:
458   //    option Not used
459   //
460   
461   std::cout << ClassName() << ": " << GetName() << "\n" 
462             << "  Enable low flux code:   " << (fEnableLowFlux ? "yes" : "no") 
463             << "\n"
464             << "  Off-line trigger mask:  0x" 
465             << std::hex     << std::setfill('0') 
466             << std::setw (8) << fOfflineTriggerMask 
467             << std::dec     << std::setfill (' ') << std::endl;
468   gROOT->IncreaseDirLevel();
469   if (fCorrManager) fCorrManager->Print();
470   else  
471     std::cout << "  Correction manager not set yet" << std::endl;
472   GetEventInspector()   .Print(option);
473   GetEnergyFitter()     .Print(option);
474   GetSharingFilter()    .Print(option);
475   gROOT->DecreaseDirLevel();
476 }
477
478 //
479 // EOF
480 //