Fixes
[u/mrichter/AliRoot.git] / PWG2 / FORWARD / analysis2 / AliForwarddNdetaTask.cxx
1 //====================================================================
2 #include "AliForwarddNdetaTask.h"
3 #include <TMath.h>
4 #include <TH2D.h>
5 #include <TH1D.h>
6 #include <THStack.h>
7 #include <TList.h>
8 #include <AliAnalysisManager.h>
9 #include <AliAODEvent.h>
10 #include <AliAODHandler.h>
11 #include <AliAODInputHandler.h>
12 #include "AliForwardUtil.h"
13 #include "AliAODForwardMult.h"
14
15 //____________________________________________________________________
16 AliForwarddNdetaTask::AliForwarddNdetaTask()
17   : AliBasedNdetaTask()
18 {
19   //
20   // Constructor 
21   // 
22 }
23
24 //____________________________________________________________________
25 AliForwarddNdetaTask::AliForwarddNdetaTask(const char* /* name */)
26   : AliBasedNdetaTask("Forward")
27 {
28   // 
29   // Constructor
30   // 
31   // Paramters
32   //   name    Name of task 
33 }
34
35 //____________________________________________________________________
36 AliForwarddNdetaTask::AliForwarddNdetaTask(const AliForwarddNdetaTask& o)
37   : AliBasedNdetaTask(o)
38 {
39   // 
40   // Copy constructor
41   // 
42 }
43
44 //____________________________________________________________________
45 AliBasedNdetaTask::CentralityBin*
46 AliForwarddNdetaTask::MakeCentralityBin(const char* name, Short_t l, Short_t h) 
47   const 
48 {
49   // 
50   // Make a new centrality bin
51   // 
52   // Parameters:
53   //    name   Histogram names
54   //    l      Lower cut
55   //    h      Upper cut
56   // 
57   // Return:
58   //    Newly allocated object (of our type)
59   //
60   return new AliForwarddNdetaTask::CentralityBin(name, l, h);
61 }
62
63 //____________________________________________________________________
64 void
65 AliForwarddNdetaTask::UserExec(Option_t* option)
66 {
67   // 
68   // Called at each event 
69   //
70   // This is called once in the master 
71   // 
72   // Parameters:
73   //    option Not used 
74   //
75   AliBasedNdetaTask::UserExec(option);
76   
77   AliAODEvent* aod = dynamic_cast<AliAODEvent*>(InputEvent());
78   if (!aod) {
79     AliError("Cannot get the AOD event");
80     return;
81   } 
82
83   TObject* obj = aod->FindListObject("Forward");
84   if (!obj) { 
85     AliWarning("No forward object found");
86     return;
87   }
88   AliAODForwardMult* forward = static_cast<AliAODForwardMult*>(obj);
89  
90   TObject* oPrimary   = aod->FindListObject("primary");
91   if (!oPrimary) return;
92   
93   TH2D* primary   = static_cast<TH2D*>(oPrimary);
94
95   // Loop over centrality bins 
96   TIter next(fListOfCentralities);
97   CentralityBin* bin = 0;
98   while ((bin = static_cast<CentralityBin*>(next()))) 
99     bin->ProcessPrimary(forward, fTriggerMask, fVtxMin, fVtxMax, primary);
100 }  
101   
102
103 //____________________________________________________________________
104 TH2D*
105 AliForwarddNdetaTask::GetHistogram(const AliAODEvent* aod, Bool_t mc)
106 {
107   // 
108   // Retrieve the histogram 
109   // 
110   // Parameters:
111   //    aod AOD event 
112   //    mc  Whether to get the MC histogram or not
113   // 
114   // Return:
115   //    Retrieved histogram or null
116   //
117   TObject* obj = 0;
118   if (mc) obj = aod->FindListObject("ForwardMC");
119   else    obj = aod->FindListObject("Forward");
120
121   // We should have a forward object at least 
122   if (!obj) {
123     if (!mc) AliWarning("No Forward object found AOD");
124     return 0;
125   }
126   AliAODForwardMult* forward = static_cast<AliAODForwardMult*>(obj);
127   return &(forward->GetHistogram());
128 }
129
130 //========================================================================
131 void
132 AliForwarddNdetaTask::CentralityBin::ProcessPrimary(const AliAODForwardMult* 
133                                                     forward, 
134                                                     Int_t triggerMask,
135                                                     Double_t /*vzMin*/, 
136                                                     Double_t /*vzMax*/, 
137                                                     const TH2D* primary)
138
139   // Check the centrality class unless this is the 'all' bin 
140   if (!IsAllBin()) { 
141     Double_t centrality = forward->GetCentrality();
142     if (centrality < fLow || centrality >= fHigh) return;
143   }
144
145   // Create sum histogram 
146   if (!fSumPrimary) { 
147     fSumPrimary = static_cast<TH2D*>(primary->Clone("truth"));
148     fSumPrimary->SetDirectory(0);
149     fSumPrimary->Reset();
150     fSums->Add(fSumPrimary);
151   }
152   
153   // translate real trigger mask to MC trigger mask
154   Int_t mask = AliAODForwardMult::kB;
155   if (triggerMask == AliAODForwardMult::kNSD)
156     mask = AliAODForwardMult::kMCNSD;
157
158   // Now use our normal check, but with the new mask, except 
159   if (!forward->CheckEvent(mask, -1000, -1000, 0, 0, 0)) return;
160
161   fSumPrimary->Add(primary);
162 }
163
164 //________________________________________________________________________
165 void
166 AliForwarddNdetaTask::CentralityBin::End(TList*      sums, 
167                                          TList*      results,
168                                          UShort_t    scheme,
169                                          const TH1*  shapeCorr, 
170                                          Double_t    trigEff,
171                                          Bool_t      symmetrice,
172                                          Int_t       rebin, 
173                                          Bool_t      rootProj,
174                                          Bool_t      corrEmpty, 
175                                          Bool_t      cutEdges,
176                                          Int_t       triggerMask,
177                                          Int_t       color,
178                                          Int_t       marker)
179 {
180   AliInfo(Form("In End of %s with corrEmpty=%d, cutEdges=%d, rootProj=%d", 
181                GetName(), corrEmpty, cutEdges, rootProj));
182   AliBasedNdetaTask::CentralityBin::End(sums, results, scheme, 
183                                         shapeCorr, trigEff, 
184                                         symmetrice, rebin, 
185                                         rootProj, corrEmpty, cutEdges,
186                                         triggerMask, color, marker);
187
188   fSumPrimary     = static_cast<TH2D*>(fSums->FindObject("truth"));
189
190   if (!fSumPrimary) { 
191     AliWarning("No MC truth histogram found");
192     fSums->ls();
193     return;
194   }
195   Int_t n = (triggerMask == AliAODForwardMult::kNSD ? 
196              Int_t(fTriggers->GetBinContent(AliAODForwardMult::kBinMCNSD)) : 
197              Int_t(fTriggers->GetBinContent(AliAODForwardMult::kBinAll)));
198   AliInfo(Form("Normalising MC truth to %d", n));
199
200   TH1D* dndetaTruth = fSumPrimary->ProjectionX("dndetaTruth",1,
201                                                fSumPrimary->GetNbinsY(),"e");
202   dndetaTruth->Scale(1./n, "width");
203
204   SetHistogramAttributes(dndetaTruth, color-2, marker+4, "Monte-Carlo truth");
205     
206   fOutput->Add(dndetaTruth);
207   fOutput->Add(Rebin(dndetaTruth, rebin, cutEdges));
208 }
209
210 //________________________________________________________________________
211 //
212 // EOF
213 //