fading out dependent helper (AliPWG0depHelper)
[u/mrichter/AliRoot.git] / PWG0 / AliPWG0Helper.cxx
CommitLineData
04a7657f 1/* $Id$ */
2
3#include <AliPWG0Helper.h>
4
5#include <TParticle.h>
6#include <TParticlePDG.h>
847489f7 7#include <TH1.h>
25db2d85 8#include <TH3.h>
116083b1 9#include <TList.h>
10
11#include <AliHeader.h>
12#include <AliStack.h>
13#include <AliLog.h>
04a7657f 14
15#include <AliLog.h>
16#include <AliESD.h>
17#include <AliESDVertex.h>
18
116083b1 19#include <AliGenEventHeader.h>
20#include <AliGenPythiaEventHeader.h>
21#include <AliGenCocktailEventHeader.h>
22
04a7657f 23//____________________________________________________________________
24ClassImp(AliPWG0Helper)
25
26//____________________________________________________________________
116083b1 27Bool_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//____________________________________________________________________
36Bool_t AliPWG0Helper::IsEventTriggered(ULong64_t triggerMask, Trigger trigger)
04a7657f 37{
38 // check if the event was triggered
39 //
5c495d37 40 // this function needs the branch fTriggerMask
41 //
04a7657f 42 // MB should be
43 // ITS_SPD_GFO_L0 : 32
44 // VZERO_OR_LEFT : 1
45 // VZERO_OR_RIGHT : 2
46
e9c3977b 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 }
04a7657f 62
63 return kFALSE;
64}
65
66//____________________________________________________________________
116083b1 67Bool_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//____________________________________________________________________
76Bool_t AliPWG0Helper::IsVertexReconstructed(const AliESDVertex* vtxESD)
04a7657f 77{
78 // checks if the vertex is reasonable
5c495d37 79 //
80 // this function needs the branches fSPDVertex*
81
116083b1 82 if (!vtxESD)
83 return kFALSE;
04a7657f 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
9e952c39 97 // check Ncontributors, if <0 it means error *gna*
98
04a7657f 99 return kTRUE;
100}
101
102//____________________________________________________________________
7584d357 103Bool_t AliPWG0Helper::IsPrimaryCharged(TParticle* aParticle, Int_t aTotalPrimaries, Bool_t adebug)
04a7657f 104{
105 //
25db2d85 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 //
04a7657f 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 {
7584d357 115 if (adebug)
25db2d85 116 printf("Dropping particle because it has a daughter among the primaries.\n");
04a7657f 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 {
7584d357 125 if (adebug)
25db2d85 126 printf("Dropping particle because it is a quark or gluon.\n");
04a7657f 127 return kFALSE;
128 }
129
130 if (strcmp(aParticle->GetName(),"XXX") == 0)
131 {
7584d357 132 if (adebug)
25db2d85 133 printf("WARNING: There is a particle named XXX.\n");
04a7657f 134 return kFALSE;
135 }
136
137 TParticlePDG* pdgPart = aParticle->GetPDG();
138
139 if (strcmp(pdgPart->ParticleClass(),"Unknown") == 0)
140 {
7584d357 141 if (adebug)
25db2d85 142 printf("WARNING: There is a particle with an unknown particle class (pdg code %d).\n", pdgCode);
04a7657f 143 return kFALSE;
144 }
145
146 if (pdgPart->Charge() == 0)
147 {
7584d357 148 if (adebug)
25db2d85 149 printf("Dropping particle because it is not charged.\n");
04a7657f 150 return kFALSE;
151 }
152
153 return kTRUE;
154}
847489f7 155
156//____________________________________________________________________
29771dc8 157void AliPWG0Helper::CreateProjections(TH3* hist, Bool_t save)
847489f7 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());
29771dc8 165 if (save)
166 proj->Write();
847489f7 167
168 proj = hist->Project3D("zx");
169 proj->SetXTitle(hist->GetXaxis()->GetTitle());
170 proj->SetYTitle(hist->GetZaxis()->GetTitle());
29771dc8 171 if (save)
172 proj->Write();
847489f7 173
174 proj = hist->Project3D("zy");
175 proj->SetXTitle(hist->GetYaxis()->GetTitle());
176 proj->SetYTitle(hist->GetZaxis()->GetTitle());
29771dc8 177 if (save)
178 proj->Write();
847489f7 179}
1afae8ff 180
181//____________________________________________________________________
29771dc8 182void AliPWG0Helper::CreateDividedProjections(TH3* hist, TH3* hist2, const char* axis, Bool_t putErrors, Bool_t save)
1afae8ff 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 {
29771dc8 190 CreateDividedProjections(hist, hist2, "yx", putErrors, save);
191 CreateDividedProjections(hist, hist2, "zx", putErrors, save);
192 CreateDividedProjections(hist, hist2, "zy", putErrors, save);
1afae8ff 193
194 return;
195 }
196
197 TH1* proj = hist->Project3D(axis);
0ab29cfa 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]));
1afae8ff 206
207 TH1* proj2 = hist2->Project3D(axis);
0ab29cfa 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]));
1afae8ff 215
216 TH1* division = dynamic_cast<TH1*> (proj->Clone(Form("%s_div_%s", proj->GetName(), proj2->GetName())));
29771dc8 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()));
9e952c39 219 division->Divide(proj, proj2, 1, 1, "B");
29771dc8 220 division->SetTitle(Form("%s divided %s", proj->GetTitle(), proj2->GetTitle()));
0ab29cfa 221
222 if (putErrors)
223 {
224 division->Sumw2();
225 if (division->GetDimension() == 1)
226 {
227 Int_t nBins = division->GetNbinsX();
29771dc8 228 for (Int_t i = 1; i <= nBins; ++i)
0ab29cfa 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();
29771dc8 236 for (Int_t i = 1; i <= nBinsX; ++i)
237 for (Int_t j = 1; j <= nBinsY; ++j)
0ab29cfa 238 if (proj2->GetBinContent(i, j) != 0)
239 division->SetBinError(i, j, TMath::Sqrt(proj->GetBinContent(i, j)) / proj2->GetBinContent(i, j));
240 }
241 }
29771dc8 242
243 if (save)
244 {
245 proj->Write();
246 proj2->Write();
247 division->Write();
248 }
1afae8ff 249}
250
92d2d8ad 251//____________________________________________________________________
25db2d85 252const char* AliPWG0Helper::GetAxisTitle(TH3* hist, const char axis)
92d2d8ad 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}
116083b1 265
266//____________________________________________________________________
267Int_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//____________________________________________________________________
308TParticle* 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//____________________________________________________________________
323Int_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}