]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG0/AliPWG0Helper.cxx
merging post-CVS changes into SVN:
[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 <TH2.h>
9 #include <TH3.h>
10 #include <TList.h>
11 #include <TTree.h>
12 #include <TBranch.h>
13 #include <TLeaf.h>
14
15 #include <AliHeader.h>
16 #include <AliStack.h>
17 #include <AliLog.h>
18
19 #include <AliLog.h>
20 #include <AliESD.h>
21 #include <AliESDVertex.h>
22
23 #include <AliGenEventHeader.h>
24 #include <AliGenPythiaEventHeader.h>
25 #include <AliGenCocktailEventHeader.h>
26
27 //____________________________________________________________________
28 ClassImp(AliPWG0Helper)
29
30 //____________________________________________________________________
31 Bool_t AliPWG0Helper::IsEventTriggered(const AliESD* aEsd, Trigger trigger)
32 {
33   // see function with ULong64_t argument
34
35   ULong64_t triggerMask = aEsd->GetTriggerMask();
36   return IsEventTriggered(triggerMask, trigger);
37 }
38
39 //____________________________________________________________________
40 Bool_t AliPWG0Helper::IsEventTriggered(ULong64_t triggerMask, Trigger trigger)
41 {
42   // check if the event was triggered
43   //
44   // this function needs the branch fTriggerMask
45   
46
47   // definitions from p-p.cfg
48   ULong64_t spdFO = (1 << 14);
49   ULong64_t v0left = (1 << 11);
50   ULong64_t v0right = (1 << 12);
51
52   switch (trigger)
53   {
54     case kMB1:
55     {
56       if (triggerMask & spdFO || ((triggerMask & v0left) || (triggerMask & v0right)))
57         return kTRUE;
58       break;
59     }
60     case kMB2:
61     {
62       if (triggerMask & spdFO && ((triggerMask & v0left) || (triggerMask & v0right)))
63         return kTRUE;
64       break;
65     }
66   }
67
68   return kFALSE;
69 }
70
71 //____________________________________________________________________
72 Bool_t AliPWG0Helper::IsVertexReconstructed(const AliESD* aEsd)
73 {
74   // see function with AliESDVertex argument
75
76   const AliESDVertex* vtxESD = aEsd->GetVertex();
77   return IsVertexReconstructed(vtxESD);
78 }
79
80 //____________________________________________________________________
81 Bool_t AliPWG0Helper::IsVertexReconstructed(const AliESDVertex* vtxESD)
82 {
83   // checks if the vertex is reasonable
84   //
85   // this function needs the branches fSPDVertex*
86
87   if (!vtxESD)
88     return kFALSE;
89
90   // the vertex should be reconstructed
91   if (strcmp(vtxESD->GetName(), "default")==0)
92     return kFALSE;
93
94   // check Ncontributors
95   if (vtxESD->GetNContributors() <= 0)
96     return kFALSE;
97
98   Double_t vtx_res[3];
99   vtx_res[0] = vtxESD->GetXRes();
100   vtx_res[1] = vtxESD->GetYRes();
101   vtx_res[2] = vtxESD->GetZRes();
102
103   if (vtx_res[2]==0 || vtx_res[2]>0.1)
104     return kFALSE;
105
106   return kTRUE;
107 }
108
109 //____________________________________________________________________
110 Bool_t AliPWG0Helper::IsPrimaryCharged(TParticle* aParticle, Int_t aTotalPrimaries, Bool_t adebug)
111 {
112   //
113   // this function checks if a particle from the event generator (i.e. among the nPrim particles in the stack)
114   // shall be counted as a primary particle
115   //
116   // This function or a equivalent should be available in some common place of AliRoot
117   //
118   // WARNING: Call this function only for particles that are among the particles from the event generator!
119   // --> stack->Particle(id) with id < stack->GetNprimary()
120
121   // if the particle has a daughter primary, we do not want to count it
122   if (aParticle->GetFirstDaughter() != -1 && aParticle->GetFirstDaughter() < aTotalPrimaries)
123   {
124     if (adebug)
125       printf("Dropping particle because it has a daughter among the primaries.\n");
126     return kFALSE;
127   }
128
129   Int_t pdgCode = TMath::Abs(aParticle->GetPdgCode());
130
131   // skip quarks and gluon
132   if (pdgCode <= 10 || pdgCode == 21)
133   {
134     if (adebug)
135       printf("Dropping particle because it is a quark or gluon.\n");
136     return kFALSE;
137   }
138
139   if (strcmp(aParticle->GetName(),"XXX") == 0)
140   {
141     if (adebug)
142       printf("WARNING: There is a particle named XXX.\n");
143     return kFALSE;
144   }
145
146   TParticlePDG* pdgPart = aParticle->GetPDG();
147
148   if (strcmp(pdgPart->ParticleClass(),"Unknown") == 0)
149   {
150     if (adebug)
151       printf("WARNING: There is a particle with an unknown particle class (pdg code %d).\n", pdgCode);
152     return kFALSE;
153   }
154
155   if (pdgPart->Charge() == 0)
156   {
157     if (adebug)
158       printf("Dropping particle because it is not charged.\n");
159     return kFALSE;
160   }
161
162   return kTRUE;
163 }
164
165 //____________________________________________________________________
166 void AliPWG0Helper::CreateProjections(TH3* hist, Bool_t save)
167 {
168   // create projections of 3d hists to all 2d combinations
169   // the histograms are not returned, just use them from memory or use this to create them in a file
170
171   TH1* proj = hist->Project3D("yx");
172   proj->SetXTitle(hist->GetXaxis()->GetTitle());
173   proj->SetYTitle(hist->GetYaxis()->GetTitle());
174   if (save)
175     proj->Write();
176
177   proj = hist->Project3D("zx");
178   proj->SetXTitle(hist->GetXaxis()->GetTitle());
179   proj->SetYTitle(hist->GetZaxis()->GetTitle());
180   if (save)
181     proj->Write();
182
183   proj = hist->Project3D("zy");
184   proj->SetXTitle(hist->GetYaxis()->GetTitle());
185   proj->SetYTitle(hist->GetZaxis()->GetTitle());
186   if (save)
187     proj->Write();
188 }
189
190 //____________________________________________________________________
191 void AliPWG0Helper::CreateDividedProjections(TH3* hist, TH3* hist2, const char* axis, Bool_t putErrors, Bool_t save)
192 {
193   // create projections of the 3d hists divides them
194   // axis decides to which plane, if axis is 0 to all planes
195   // the histograms are not returned, just use them from memory or use this to create them in a file
196
197   if (axis == 0)
198   {
199     CreateDividedProjections(hist, hist2, "yx", putErrors, save);
200     CreateDividedProjections(hist, hist2, "zx", putErrors, save);
201     CreateDividedProjections(hist, hist2, "zy", putErrors, save);
202
203     return;
204   }
205
206   TH1* proj = hist->Project3D(axis);
207
208   if (strlen(axis) == 2)
209   {
210     proj->SetYTitle(GetAxisTitle(hist, axis[0]));
211     proj->SetXTitle(GetAxisTitle(hist, axis[1]));
212   }
213   else if (strlen(axis) == 1)
214     proj->SetXTitle(GetAxisTitle(hist, axis[0]));
215
216   TH1* proj2 = hist2->Project3D(axis);
217   if (strlen(axis) == 2)
218   {
219     proj2->SetYTitle(GetAxisTitle(hist2, axis[0]));
220     proj2->SetXTitle(GetAxisTitle(hist2, axis[1]));
221   }
222   else if (strlen(axis) == 1)
223     proj2->SetXTitle(GetAxisTitle(hist2, axis[0]));
224
225   TH1* division = dynamic_cast<TH1*> (proj->Clone(Form("%s_div_%s", proj->GetName(), proj2->GetName())));
226   //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()));
227   //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()));
228   division->Divide(proj, proj2, 1, 1, "B");
229   division->SetTitle(Form("%s divided %s", proj->GetTitle(), proj2->GetTitle()));
230
231   if (putErrors)
232   {
233     division->Sumw2();
234     if (division->GetDimension() == 1)
235     {
236       Int_t nBins = division->GetNbinsX();
237       for (Int_t i = 1; i <= nBins; ++i)
238         if (proj2->GetBinContent(i) != 0)
239           division->SetBinError(i, TMath::Sqrt(proj->GetBinContent(i)) / proj2->GetBinContent(i));
240     }
241     else if (division->GetDimension() == 2)
242     {
243       Int_t nBinsX = division->GetNbinsX();
244       Int_t nBinsY = division->GetNbinsY();
245       for (Int_t i = 1; i <= nBinsX; ++i)
246         for (Int_t j = 1; j <= nBinsY; ++j)
247           if (proj2->GetBinContent(i, j) != 0)
248             division->SetBinError(i, j, TMath::Sqrt(proj->GetBinContent(i, j)) / proj2->GetBinContent(i, j));
249     }
250   }
251
252   if (save)
253   {
254     proj->Write();
255     proj2->Write();
256     division->Write();
257   }
258 }
259
260 //____________________________________________________________________
261 const char* AliPWG0Helper::GetAxisTitle(TH3* hist, const char axis)
262 {
263   // returns the title of the axis given in axis (x, y, z)
264
265   if (axis == 'x')
266     return hist->GetXaxis()->GetTitle();
267   else if (axis == 'y')
268     return hist->GetYaxis()->GetTitle();
269   else if (axis == 'z')
270     return hist->GetZaxis()->GetTitle();
271
272   return 0;
273 }
274
275 //____________________________________________________________________
276 Int_t AliPWG0Helper::GetPythiaEventProcessType(AliHeader* aHeader, Bool_t adebug) {
277   //
278   // get the process type of the event.
279   //
280
281   // can only read pythia headers, either directly or from cocktalil header
282   AliGenPythiaEventHeader* pythiaGenHeader = dynamic_cast<AliGenPythiaEventHeader*>(aHeader->GenEventHeader());
283
284   if (!pythiaGenHeader) {
285
286     AliGenCocktailEventHeader* genCocktailHeader = dynamic_cast<AliGenCocktailEventHeader*>(aHeader->GenEventHeader());
287     if (!genCocktailHeader) {
288       printf("AliPWG0Helper::GetProcessType : Unknown header type (not Pythia or Cocktail). \n");
289       return -1;
290     }
291
292     TList* headerList = genCocktailHeader->GetHeaders();
293     if (!headerList) {
294       return -1;
295     }
296
297     for (Int_t i=0; i<headerList->GetEntries(); i++) {
298       pythiaGenHeader = dynamic_cast<AliGenPythiaEventHeader*>(headerList->At(i));
299       if (pythiaGenHeader)
300         break;
301     }
302
303     if (!pythiaGenHeader) {
304       printf("AliPWG0Helper::GetProcessType : Could not find Pythia header. \n");
305       return -1;
306     }
307   }
308
309   if (adebug) {
310     printf("AliPWG0Helper::GetProcessType : Pythia process type found: %d \n",pythiaGenHeader->ProcessType());
311   }
312
313   return pythiaGenHeader->ProcessType();
314 }
315
316 //____________________________________________________________________
317 TParticle* AliPWG0Helper::FindPrimaryMother(AliStack* stack, Int_t label)
318 {
319   //
320   // Finds the first mother among the primary particles of the particle identified by <label>,
321   // i.e. the primary that "caused" this particle
322   //
323
324   Int_t motherLabel = FindPrimaryMotherLabel(stack, label);
325   if (motherLabel < 0)
326     return 0;
327
328   return stack->Particle(motherLabel);
329 }
330
331 //____________________________________________________________________
332 Int_t AliPWG0Helper::FindPrimaryMotherLabel(AliStack* stack, Int_t label)
333 {
334   //
335   // Finds the first mother among the primary particles of the particle identified by <label>,
336   // i.e. the primary that "caused" this particle
337   //
338   // returns its label
339   //
340
341   Int_t nPrim  = stack->GetNprimary();
342
343   while (label >= nPrim)
344   {
345     //printf("Particle %d (pdg %d) is not a primary. Let's check its mother %d\n", label, mother->GetPdgCode(), mother->GetMother(0));
346
347     TParticle* particle = stack->Particle(label);
348     if (!particle)
349     {
350       AliDebugGeneral("FindPrimaryMother", AliLog::kError, Form("UNEXPECTED: particle with label %d not found in stack.", label));
351       return -1;
352     }
353  
354     // find mother
355     if (particle->GetMother(0) < 0)
356     {
357       AliDebugGeneral("FindPrimaryMother", AliLog::kError, Form("UNEXPECTED: Could not find mother of secondary particle %d.", label));
358       return -1;
359     }
360
361     label = particle->GetMother(0);
362   }
363
364   return label;
365 }
366
367 void AliPWG0Helper::SetBranchStatusRecursive(TTree* tree, char *bname, Bool_t status, Bool_t debug)
368 {
369   // Function  to switch on/off all data members of a top level branch
370   // this is needed for branches without a trailing dot ".", for those
371   // the root functionality with regular expressions does not work.
372   // Usage e.g.
373   // chain->SetBranchStatus("*", 0); 
374   // SetBranchStatusRecursive(chain,"SPDVertex",1);
375   // You need to give the full name of the top level branch zou want to access
376   //==========================================================================
377   // Author Christian.Klein-Boesing@cern.ch
378
379   if (!tree)
380     return;
381
382   TBranch *br = tree->GetBranch(bname);
383   if(!br) {
384     Printf("AliPWG0Helper::SetBranchStatusRecursive: Branch %s not found", bname);
385   }
386
387   TObjArray *leaves = tree->GetListOfLeaves();
388   Int_t nleaves = leaves->GetEntries();
389   TLeaf *leaf = 0;
390   TBranch *branch = 0;
391   TBranch *mother = 0;
392   for (Int_t i=0;i<nleaves;i++)  {
393     // the matched entry e.g. SPDVertex is its own Mother
394     leaf = (TLeaf*)leaves->UncheckedAt(i);
395     branch = (TBranch*)leaf->GetBranch();
396     mother = branch->GetMother();
397     if (mother==br){
398       if (debug)
399         Printf(">>>> AliPWG0Helper::SetBranchStatusRecursive: Setting status %s %s to %d", mother->GetName(), leaf->GetName(), status);
400       if (status) branch->ResetBit(kDoNotProcess);
401       else        branch->SetBit(kDoNotProcess);
402     }
403   }
404 }
405
406 //____________________________________________________________________
407 void AliPWG0Helper::NormalizeToBinWidth(TH1* hist)
408 {
409   //
410   // normalizes a 1-d histogram to its bin width
411   //
412
413   for (Int_t i=1; i<=hist->GetNbinsX(); ++i)
414   {
415     hist->SetBinContent(i, hist->GetBinContent(i) / hist->GetBinWidth(i));
416     hist->SetBinError(i, hist->GetBinError(i) / hist->GetBinWidth(i));
417   }
418 }
419
420 //____________________________________________________________________
421 void AliPWG0Helper::NormalizeToBinWidth(TH2* hist)
422 {
423   //
424   // normalizes a 2-d histogram to its bin width (x width * y width)
425   //
426
427   for (Int_t i=1; i<=hist->GetNbinsX(); ++i)
428     for (Int_t j=1; j<=hist->GetNbinsY(); ++j)
429     {
430       Double_t factor = hist->GetXaxis()->GetBinWidth(i) * hist->GetYaxis()->GetBinWidth(j);
431       hist->SetBinContent(i, j, hist->GetBinContent(i, j) / factor);
432       hist->SetBinError(i, j, hist->GetBinError(i, j) / factor);
433     }
434 }