]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG0/AliPWG0Helper.cxx
fading out dependent helper (AliPWG0depHelper)
[u/mrichter/AliRoot.git] / PWG0 / AliPWG0Helper.cxx
1 /* $Id$ */
2
3 #include <AliPWG0Helper.h>
4
5 #include <TParticle.h>
6 #include <TParticlePDG.h>
7 #include <TH1.h>
8 #include <TH3.h>
9 #include <TList.h>
10
11 #include <AliHeader.h>
12 #include <AliStack.h>
13 #include <AliLog.h>
14
15 #include <AliLog.h>
16 #include <AliESD.h>
17 #include <AliESDVertex.h>
18
19 #include <AliGenEventHeader.h>
20 #include <AliGenPythiaEventHeader.h>
21 #include <AliGenCocktailEventHeader.h>
22
23 //____________________________________________________________________
24 ClassImp(AliPWG0Helper)
25
26 //____________________________________________________________________
27 Bool_t AliPWG0Helper::IsEventTriggered(const AliESD* aEsd, Trigger trigger)
28 {
29   // see function with ULong64_t argument
30
31   ULong64_t triggerMask = aEsd->GetTriggerMask();
32   return IsEventTriggered(triggerMask, trigger);
33 }
34
35 //____________________________________________________________________
36 Bool_t AliPWG0Helper::IsEventTriggered(ULong64_t triggerMask, Trigger trigger)
37 {
38   // check if the event was triggered
39   //
40   // this function needs the branch fTriggerMask
41   //
42   // MB should be
43   // ITS_SPD_GFO_L0  : 32
44   // VZERO_OR_LEFT   : 1
45   // VZERO_OR_RIGHT  : 2
46
47   switch (trigger)
48   {
49     case kMB1:
50     {
51       if (triggerMask&32 || ((triggerMask&1) || (triggerMask&2)))
52         return kTRUE;
53       break;
54     }
55     case kMB2:
56     {
57       if (triggerMask&32 && ((triggerMask&1) || (triggerMask&2)))
58         return kTRUE;
59       break;
60     }
61   }
62
63   return kFALSE;
64 }
65
66 //____________________________________________________________________
67 Bool_t AliPWG0Helper::IsVertexReconstructed(const AliESD* aEsd)
68 {
69   // see function with AliESDVertex argument
70
71   const AliESDVertex* vtxESD = aEsd->GetVertex();
72   return IsVertexReconstructed(vtxESD);
73 }
74
75 //____________________________________________________________________
76 Bool_t AliPWG0Helper::IsVertexReconstructed(const AliESDVertex* vtxESD)
77 {
78   // checks if the vertex is reasonable
79   //
80   // this function needs the branches fSPDVertex*
81
82   if (!vtxESD)
83     return kFALSE;
84
85   // the vertex should be reconstructed
86   if (strcmp(vtxESD->GetName(), "default")==0)
87     return kFALSE;
88
89   Double_t vtx_res[3];
90   vtx_res[0] = vtxESD->GetXRes();
91   vtx_res[1] = vtxESD->GetYRes();
92   vtx_res[2] = vtxESD->GetZRes();
93
94   if (vtx_res[2]==0 || vtx_res[2]>0.1)
95     return kFALSE;
96
97   // check Ncontributors, if <0 it means error *gna*
98
99   return kTRUE;
100 }
101
102 //____________________________________________________________________
103 Bool_t AliPWG0Helper::IsPrimaryCharged(TParticle* aParticle, Int_t aTotalPrimaries, Bool_t adebug)
104 {
105   //
106   // this function checks if a particle from the event generator (i.e. among the nPrim particles in the stack)
107   // shall be counted as a primary particle
108   //
109   // This function or a equivalent should be available in some common place of AliRoot
110   //
111
112   // if the particle has a daughter primary, we do not want to count it
113   if (aParticle->GetFirstDaughter() != -1 && aParticle->GetFirstDaughter() < aTotalPrimaries)
114   {
115     if (adebug)
116       printf("Dropping particle because it has a daughter among the primaries.\n");
117     return kFALSE;
118   }
119
120   Int_t pdgCode = TMath::Abs(aParticle->GetPdgCode());
121
122   // skip quarks and gluon
123   if (pdgCode <= 10 || pdgCode == 21)
124   {
125     if (adebug)
126       printf("Dropping particle because it is a quark or gluon.\n");
127     return kFALSE;
128   }
129
130   if (strcmp(aParticle->GetName(),"XXX") == 0)
131   {
132     if (adebug)
133       printf("WARNING: There is a particle named XXX.\n");
134     return kFALSE;
135   }
136
137   TParticlePDG* pdgPart = aParticle->GetPDG();
138
139   if (strcmp(pdgPart->ParticleClass(),"Unknown") == 0)
140   {
141     if (adebug)
142       printf("WARNING: There is a particle with an unknown particle class (pdg code %d).\n", pdgCode);
143     return kFALSE;
144   }
145
146   if (pdgPart->Charge() == 0)
147   {
148     if (adebug)
149       printf("Dropping particle because it is not charged.\n");
150     return kFALSE;
151   }
152
153   return kTRUE;
154 }
155
156 //____________________________________________________________________
157 void AliPWG0Helper::CreateProjections(TH3* hist, Bool_t save)
158 {
159   // create projections of 3d hists to all 2d combinations
160   // the histograms are not returned, just use them from memory or use this to create them in a file
161
162   TH1* proj = hist->Project3D("yx");
163   proj->SetXTitle(hist->GetXaxis()->GetTitle());
164   proj->SetYTitle(hist->GetYaxis()->GetTitle());
165   if (save)
166     proj->Write();
167
168   proj = hist->Project3D("zx");
169   proj->SetXTitle(hist->GetXaxis()->GetTitle());
170   proj->SetYTitle(hist->GetZaxis()->GetTitle());
171   if (save)
172     proj->Write();
173
174   proj = hist->Project3D("zy");
175   proj->SetXTitle(hist->GetYaxis()->GetTitle());
176   proj->SetYTitle(hist->GetZaxis()->GetTitle());
177   if (save)
178     proj->Write();
179 }
180
181 //____________________________________________________________________
182 void AliPWG0Helper::CreateDividedProjections(TH3* hist, TH3* hist2, const char* axis, Bool_t putErrors, Bool_t save)
183 {
184   // create projections of the 3d hists divides them
185   // axis decides to which plane, if axis is 0 to all planes
186   // the histograms are not returned, just use them from memory or use this to create them in a file
187
188   if (axis == 0)
189   {
190     CreateDividedProjections(hist, hist2, "yx", putErrors, save);
191     CreateDividedProjections(hist, hist2, "zx", putErrors, save);
192     CreateDividedProjections(hist, hist2, "zy", putErrors, save);
193
194     return;
195   }
196
197   TH1* proj = hist->Project3D(axis);
198
199   if (strlen(axis) == 2)
200   {
201     proj->SetYTitle(GetAxisTitle(hist, axis[0]));
202     proj->SetXTitle(GetAxisTitle(hist, axis[1]));
203   }
204   else if (strlen(axis) == 1)
205     proj->SetXTitle(GetAxisTitle(hist, axis[0]));
206
207   TH1* proj2 = hist2->Project3D(axis);
208   if (strlen(axis) == 2)
209   {
210     proj2->SetYTitle(GetAxisTitle(hist2, axis[0]));
211     proj2->SetXTitle(GetAxisTitle(hist2, axis[1]));
212   }
213   else if (strlen(axis) == 1)
214     proj2->SetXTitle(GetAxisTitle(hist2, axis[0]));
215
216   TH1* division = dynamic_cast<TH1*> (proj->Clone(Form("%s_div_%s", proj->GetName(), proj2->GetName())));
217   //printf("doing axis: %s, x axis has %d %d bins, min %f %f max %f %f\n", axis, division->GetNbinsX(), proj2->GetNbinsX(), division->GetXaxis()->GetBinLowEdge(1), proj2->GetXaxis()->GetBinLowEdge(1), division->GetXaxis()->GetBinUpEdge(division->GetNbinsX()), proj2->GetXaxis()->GetBinUpEdge(proj2->GetNbinsX()));
218   //printf("doing axis: %s, y axis has %d %d bins, min %f %f max %f %f\n", axis, division->GetNbinsY(), proj2->GetNbinsY(), division->GetYaxis()->GetBinLowEdge(1), proj2->GetYaxis()->GetBinLowEdge(1), division->GetYaxis()->GetBinUpEdge(division->GetNbinsY()), proj2->GetYaxis()->GetBinUpEdge(proj2->GetNbinsY()));
219   division->Divide(proj, proj2, 1, 1, "B");
220   division->SetTitle(Form("%s divided %s", proj->GetTitle(), proj2->GetTitle()));
221
222   if (putErrors)
223   {
224     division->Sumw2();
225     if (division->GetDimension() == 1)
226     {
227       Int_t nBins = division->GetNbinsX();
228       for (Int_t i = 1; i <= nBins; ++i)
229         if (proj2->GetBinContent(i) != 0)
230           division->SetBinError(i, TMath::Sqrt(proj->GetBinContent(i)) / proj2->GetBinContent(i));
231     }
232     else if (division->GetDimension() == 2)
233     {
234       Int_t nBinsX = division->GetNbinsX();
235       Int_t nBinsY = division->GetNbinsY();
236       for (Int_t i = 1; i <= nBinsX; ++i)
237         for (Int_t j = 1; j <= nBinsY; ++j)
238           if (proj2->GetBinContent(i, j) != 0)
239             division->SetBinError(i, j, TMath::Sqrt(proj->GetBinContent(i, j)) / proj2->GetBinContent(i, j));
240     }
241   }
242
243   if (save)
244   {
245     proj->Write();
246     proj2->Write();
247     division->Write();
248   }
249 }
250
251 //____________________________________________________________________
252 const char* AliPWG0Helper::GetAxisTitle(TH3* hist, const char axis)
253 {
254   // returns the title of the axis given in axis (x, y, z)
255
256   if (axis == 'x')
257     return hist->GetXaxis()->GetTitle();
258   else if (axis == 'y')
259     return hist->GetYaxis()->GetTitle();
260   else if (axis == 'z')
261     return hist->GetZaxis()->GetTitle();
262
263   return 0;
264 }
265
266 //____________________________________________________________________
267 Int_t AliPWG0Helper::GetPythiaEventProcessType(AliHeader* aHeader, Bool_t adebug) {
268   //
269   // get the process type of the event.
270   //
271
272   // can only read pythia headers, either directly or from cocktalil header
273   AliGenPythiaEventHeader* pythiaGenHeader = dynamic_cast<AliGenPythiaEventHeader*>(aHeader->GenEventHeader());
274
275   if (!pythiaGenHeader) {
276
277     AliGenCocktailEventHeader* genCocktailHeader = dynamic_cast<AliGenCocktailEventHeader*>(aHeader->GenEventHeader());
278     if (!genCocktailHeader) {
279       printf("AliPWG0Helper::GetProcessType : Unknown header type (not Pythia or Cocktail). \n");
280       return -1;
281     }
282
283     TList* headerList = genCocktailHeader->GetHeaders();
284     if (!headerList) {
285       return -1;
286     }
287
288     for (Int_t i=0; i<headerList->GetEntries(); i++) {
289       pythiaGenHeader = dynamic_cast<AliGenPythiaEventHeader*>(headerList->At(i));
290       if (pythiaGenHeader)
291         break;
292     }
293
294     if (!pythiaGenHeader) {
295       printf("AliPWG0Helper::GetProcessType : Could not find Pythia header. \n");
296       return -1;
297     }
298   }
299
300   if (adebug) {
301     printf("AliPWG0Helper::GetProcessType : Pythia process type found: %d \n",pythiaGenHeader->ProcessType());
302   }
303
304   return pythiaGenHeader->ProcessType();
305 }
306
307 //____________________________________________________________________
308 TParticle* AliPWG0Helper::FindPrimaryMother(AliStack* stack, Int_t label)
309 {
310   //
311   // Finds the first mother among the primary particles of the particle identified by <label>,
312   // i.e. the primary that "caused" this particle
313   //
314
315   Int_t motherLabel = FindPrimaryMotherLabel(stack, label);
316   if (motherLabel < 0)
317     return 0;
318
319   return stack->Particle(motherLabel);
320 }
321
322 //____________________________________________________________________
323 Int_t AliPWG0Helper::FindPrimaryMotherLabel(AliStack* stack, Int_t label)
324 {
325   //
326   // Finds the first mother among the primary particles of the particle identified by <label>,
327   // i.e. the primary that "caused" this particle
328   //
329   // returns its label
330   //
331
332   Int_t nPrim  = stack->GetNprimary();
333
334   while (label >= nPrim)
335   {
336     //printf("Particle %d (pdg %d) is not a primary. Let's check its mother %d\n", label, mother->GetPdgCode(), mother->GetMother(0));
337
338     TParticle* particle = stack->Particle(label);
339     if (!particle)
340     {
341       AliDebugGeneral("FindPrimaryMother", AliLog::kError, Form("UNEXPECTED: particle with label %d not found in stack.", label));
342       return -1;
343     }
344  
345     // find mother
346     if (particle->GetMother(0) < 0)
347     {
348       AliDebugGeneral("FindPrimaryMother", AliLog::kError, Form("UNEXPECTED: Could not find mother of secondary particle %d.", label));
349       return -1;
350     }
351
352     label = particle->GetMother(0);
353   }
354
355   return label;
356 }