]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGLF/FORWARD/analysis2/AliForwardQATask.cxx
Multiple fixes:
[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   DGUARD(fDebug,2,"Get the ESD event");
234   if (IsZombie()) {
235     DMSG(fDebug,3,"We're a Zombie - bailing out");
236     return 0;
237   }
238   AliESDEvent* esd = dynamic_cast<AliESDEvent*>(InputEvent());
239   if (!esd) {
240     AliWarning("No ESD event found for input event");
241     return 0;
242   }
243
244   // On the first event, initialize the parameters
245   if (fFirstEvent && esd->GetESDRun()) {
246     GetEventInspector().ReadRunDetails(esd);
247
248     AliInfoF("Initializing with parameters from the ESD:\n"
249              "         AliESDEvent::GetBeamEnergy()   ->%f\n"
250              "         AliESDEvent::GetBeamType()     ->%s\n"
251              "         AliESDEvent::GetCurrentL3()    ->%f\n"
252              "         AliESDEvent::GetMagneticField()->%f\n"
253              "         AliESDEvent::GetRunNumber()    ->%d",
254              esd->GetBeamEnergy(),
255              esd->GetBeamType(),
256              esd->GetCurrentL3(),
257              esd->GetMagneticField(),
258              esd->GetRunNumber());
259
260
261     if (!InitializeSubs()) {
262       AliWarning("Initialisation of sub algorithms failed!");
263       SetZombie(true);
264       esd = 0;
265       return 0;
266     }
267     AliInfoF("Clearing first event flag from %s to false", 
268              fFirstEvent ? "true" : "false");
269     fFirstEvent = false;
270   }
271   return esd;
272 }
273 //____________________________________________________________________
274 Bool_t
275 AliForwardQATask::InitializeSubs()
276 {
277   // 
278   // Initialise the sub objects and stuff.  Called on first event 
279   // 
280   //
281   const TAxis* pe = 0;
282   const TAxis* pv = 0;
283
284   if (!ReadCorrections(pe,pv))  { 
285     AliWarning("Using default binning");
286     pv = new TAxis(10,-10, 10);
287     pe = new TAxis(240,-6,6);
288   }
289
290   fHistos.Init(*pe);
291
292   fEventInspector.Init(*pv);
293   fEnergyFitter.Init(*pe);
294   fSharingFilter.Init(*pe);
295   fDensityCalculator.Init(*pe);
296
297   this->Print();
298
299   return true;
300 }
301
302 //____________________________________________________________________
303 void
304 AliForwardQATask::UserCreateOutputObjects()
305 {
306   // 
307   // Create output objects 
308   // 
309   //
310   fList = new TList;
311   fList->SetOwner();
312   
313   fEventInspector.DefineOutput(fList);
314   fEnergyFitter.DefineOutput(fList);
315   fSharingFilter.DefineOutput(fList);
316   fDensityCalculator.DefineOutput(fList);
317
318   PostData(1, fList);
319 }
320 //____________________________________________________________________
321 void
322 AliForwardQATask::UserExec(Option_t*)
323 {
324   // 
325   // Process each event 
326   // 
327   // Parameters:
328   //    option Not used
329   //  
330   DGUARD(fDebug,1,"Process the input event");
331
332   // static Int_t cnt = 0;
333   // cnt++;
334   // Get the input data 
335   AliESDEvent* esd = GetESDEvent();
336   if (!esd) { 
337     AliWarning("Got no ESD event");
338     return;
339   }
340   if (fFirstEvent) { 
341     // If the first event flag wasn't cleared in the above call to
342     // GetESDEvent, we should not do anything, since nothing has been
343     // initialised yet, so we opt out here (with a warning) 
344     AliWarning("Nothing has been initialized yet, opt'ing out");
345     return;
346   }
347
348   // Clear stuff 
349   fHistos.Clear();
350   fESDFMD.Clear();
351   
352   Bool_t   lowFlux   = kFALSE;
353   UInt_t   triggers  = 0;
354   UShort_t ivz       = 0;
355   Double_t vz        = 0;
356   Double_t cent      = -1;
357   UShort_t nClusters = 0;
358   UInt_t   found     = fEventInspector.Process(esd, triggers, lowFlux, 
359                                                ivz, vz, cent, nClusters);
360   
361   Bool_t ok = true;
362   if (found & AliFMDEventInspector::kNoEvent)    ok = false;
363   if (found & AliFMDEventInspector::kNoTriggers) ok = false;
364   if (found & AliFMDEventInspector::kNoSPD)      ok = false;
365   if (found & AliFMDEventInspector::kNoFMD)      ok = false;
366   if (found & AliFMDEventInspector::kNoVertex)   ok = false;
367   if (triggers & AliAODForwardMult::kPileUp)     ok = false;
368   if (found & AliFMDEventInspector::kBadVertex)  ok = false;
369   if (!ok) { 
370     DMSG(fDebug,2,"Event failed selection: %s", 
371          AliFMDEventInspector::CodeString(found));
372     return;
373   }
374   DMSG(fDebug,2,"Event triggers: %s", AliAODForwardMult::GetTriggerString(triggers));
375
376   // We we do not want to use low flux specific code, we disable it here. 
377   if (!fEnableLowFlux) lowFlux = false;
378
379   // Get FMD data 
380   AliESDFMD* esdFMD = esd->GetFMDData();
381   
382   // Run the energy loss fitter 
383   if (!fEnergyFitter.Accumulate(*esdFMD, cent, 
384                                 triggers & AliAODForwardMult::kEmpty)) {
385     AliWarning("Energy fitter failed");
386     return;
387   }
388   
389   //  // Apply the sharing filter (or hit merging or clustering if you like)
390   if (!fSharingFilter.Filter(*esdFMD, lowFlux, fESDFMD, vz)) { 
391     AliWarning("Sharing filter failed!");
392     return;
393   }
394  
395   // Calculate the inclusive charged particle density 
396   if (!fDensityCalculator.Calculate(fESDFMD, fHistos, ivz, lowFlux)) { 
397     // if (!fDensityCalculator.Calculate(*esdFMD, fHistos, ivz, lowFlux)) { 
398     AliWarning("Density calculator failed!");
399     return;
400   }
401   
402   PostData(1, fList);
403 }
404
405 //____________________________________________________________________
406 void
407 AliForwardQATask::Terminate(Option_t*)
408 {
409   // 
410   // End of job
411   // 
412   // Parameters:
413   //    option Not used 
414   //
415   if (fDebug) AliInfo("In Forwards terminate");
416   TStopwatch swt;
417   swt.Start();
418
419   TList* list = dynamic_cast<TList*>(GetOutputData(1));
420   if (!list) {
421     AliError(Form("No output list defined (%p)", GetOutputData(1)));
422     if (GetOutputData(1)) GetOutputData(1)->Print();
423     return;
424   }
425   
426   // Get our histograms from the container 
427   TH1I* hEventsTr    = 0;//static_cast<TH1I*>(list->FindObject("nEventsTr"));
428   TH1I* hEventsTrVtx = 0;//static_cast<TH1I*>(list->FindObject("nEventsTrVtx"));
429   TH1I* hTriggers    = 0;
430   if (!fEventInspector.FetchHistograms(list, hEventsTr, 
431                                        hEventsTrVtx, hTriggers)) { 
432     AliError(Form("Didn't get histograms from event selector "
433                   "(hEventsTr=%p,hEventsTrVtx=%p)", 
434                   hEventsTr, hEventsTrVtx));
435     return;
436   }
437
438   TStopwatch swf;
439   swf.Start();
440   fEnergyFitter.Fit(list);
441   swf.Stop();
442   AliInfoF("Fitting took %d real-time seconds, and %f CPU seconds", 
443            Int_t(swf.RealTime()), swf.CpuTime());
444
445   fSharingFilter.ScaleHistograms(list,Int_t(hEventsTr->Integral()));
446   fDensityCalculator.ScaleHistograms(list,Int_t(hEventsTrVtx->Integral()));
447
448   // Make a deep copy and post that as output 2 
449   TList* list2 = static_cast<TList*>(list->Clone(Form("%sResults", 
450                                                       list->GetName())));
451   if (fDebug) AliInfoF("Posting post processing results to %s", 
452                        list2->GetName());
453   list2->SetOwner();
454   PostData(2, list2);
455
456   swt.Stop();
457   AliInfoF("Terminate took %d real-time seconds, and %f CPU seconds", 
458            Int_t(swt.RealTime()), swt.CpuTime());
459
460 }
461
462 //____________________________________________________________________
463 void
464 AliForwardQATask::Print(Option_t* option) const
465 {
466   // 
467   // Print information 
468   // 
469   // Parameters:
470   //    option Not used
471   //
472   
473   std::cout << ClassName() << ": " << GetName() << "\n" 
474             << "  Enable low flux code:   " << (fEnableLowFlux ? "yes" : "no") 
475             << "\n"
476             << "  Off-line trigger mask:  0x" 
477             << std::hex     << std::setfill('0') 
478             << std::setw (8) << fOfflineTriggerMask 
479             << std::dec     << std::setfill (' ') << std::endl;
480   gROOT->IncreaseDirLevel();
481   if (fCorrManager) fCorrManager->Print();
482   else  
483     std::cout << "  Correction manager not set yet" << std::endl;
484   GetEventInspector()   .Print(option);
485   GetEnergyFitter()     .Print(option);
486   GetSharingFilter()    .Print(option);
487   gROOT->DecreaseDirLevel();
488 }
489
490 //
491 // EOF
492 //