]> git.uio.no Git - u/mrichter/AliRoot.git/blame - PWG0/AliPWG0Helper.cxx
Correct character constantness
[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>
dd367a14 8#include <TH2.h>
25db2d85 9#include <TH3.h>
116083b1 10#include <TList.h>
9cd015f7 11#include <TTree.h>
12#include <TBranch.h>
13#include <TLeaf.h>
116083b1 14
15#include <AliHeader.h>
16#include <AliStack.h>
17#include <AliLog.h>
04a7657f 18
19#include <AliLog.h>
20#include <AliESD.h>
770a1f1d 21#include <AliESDEvent.h>
04a7657f 22#include <AliESDVertex.h>
85737496 23#include <AliVertexerTracks.h>
04a7657f 24
116083b1 25#include <AliGenEventHeader.h>
26#include <AliGenPythiaEventHeader.h>
27#include <AliGenCocktailEventHeader.h>
6b62a9c7 28#include <AliGenDPMjetEventHeader.h>
116083b1 29
04a7657f 30//____________________________________________________________________
31ClassImp(AliPWG0Helper)
32
a67484a6 33Int_t AliPWG0Helper::fgLastProcessType = -1;
34
04a7657f 35//____________________________________________________________________
116083b1 36Bool_t AliPWG0Helper::IsEventTriggered(const AliESD* aEsd, Trigger trigger)
37{
38 // see function with ULong64_t argument
39
40 ULong64_t triggerMask = aEsd->GetTriggerMask();
41 return IsEventTriggered(triggerMask, trigger);
42}
43
44//____________________________________________________________________
45Bool_t AliPWG0Helper::IsEventTriggered(ULong64_t triggerMask, Trigger trigger)
04a7657f 46{
47 // check if the event was triggered
48 //
5c495d37 49 // this function needs the branch fTriggerMask
dd367a14 50
dd367a14 51 // definitions from p-p.cfg
52 ULong64_t spdFO = (1 << 14);
53 ULong64_t v0left = (1 << 11);
54 ULong64_t v0right = (1 << 12);
04a7657f 55
e9c3977b 56 switch (trigger)
57 {
58 case kMB1:
59 {
dd367a14 60 if (triggerMask & spdFO || ((triggerMask & v0left) || (triggerMask & v0right)))
e9c3977b 61 return kTRUE;
62 break;
63 }
64 case kMB2:
65 {
dd367a14 66 if (triggerMask & spdFO && ((triggerMask & v0left) || (triggerMask & v0right)))
e9c3977b 67 return kTRUE;
68 break;
69 }
1c15d51a 70 case kSPDFASTOR:
71 {
72 if (triggerMask & spdFO)
73 return kTRUE;
74 break;
75 }
e9c3977b 76 }
04a7657f 77
78 return kFALSE;
79}
80
51f6de65 81//____________________________________________________________________
a6e0ebfe 82Bool_t AliPWG0Helper::TestVertex(const AliESDVertex* vertex, AnalysisMode analysisMode, Bool_t debug)
51f6de65 83{
84 // Checks if a vertex meets the needed quality criteria
85
86 Float_t requiredZResolution = -1;
87 if (analysisMode == kSPD || analysisMode == kTPCITS)
88 {
89 requiredZResolution = 0.1;
90 }
91 else if (analysisMode == kTPC)
92 requiredZResolution = 10.;
93
94 // check resolution
95 Double_t zRes = vertex->GetZRes();
96
97 if (zRes > requiredZResolution) {
98 if (debug)
99 Printf("AliPWG0Helper::TestVertex: Resolution too poor %f (required: %f", zRes, requiredZResolution);
100 return kFALSE;
101 }
102
103 return kTRUE;
104}
105
770a1f1d 106//____________________________________________________________________
85737496 107const AliESDVertex* AliPWG0Helper::GetVertex(AliESDEvent* aEsd, AnalysisMode analysisMode, Bool_t debug,Bool_t bRedoTPC)
770a1f1d 108{
109 // Get the vertex from the ESD and returns it if the vertex is valid
110 //
111 // Second argument decides which vertex is used (this selects
112 // also the quality criteria that are applied)
113
114 const AliESDVertex* vertex = 0;
770a1f1d 115 if (analysisMode == kSPD || analysisMode == kTPCITS)
116 {
d1f50534 117 vertex = aEsd->GetPrimaryVertexSPD();
ebf31fda 118 if (debug)
119 Printf("AliPWG0Helper::GetVertex: Returning SPD vertex");
770a1f1d 120 }
51f6de65 121 else if (analysisMode == kTPC)
770a1f1d 122 {
85737496 123 if(bRedoTPC){
51f6de65 124 Double_t kBz = aEsd->GetMagneticField();
85737496 125 AliVertexerTracks vertexer(kBz);
51f6de65 126 vertexer.SetTPCMode();
85737496 127 AliESDVertex *vTPC = vertexer.FindPrimaryVertex(aEsd);
128 aEsd->SetPrimaryVertexTPC(vTPC);
129 for (Int_t i=0; i<aEsd->GetNumberOfTracks(); i++) {
130 AliESDtrack *t = aEsd->GetTrack(i);
131 t->RelateToVertexTPC(vTPC, kBz, kVeryBig);
51f6de65 132 }
85737496 133 delete vTPC;
134 }
135
d1f50534 136 vertex = aEsd->GetPrimaryVertexTPC();
ebf31fda 137 if (debug)
138 Printf("AliPWG0Helper::GetVertex: Returning vertex from tracks");
770a1f1d 139 }
140 else
141 Printf("AliPWG0Helper::GetVertex: ERROR: Invalid second argument %d", analysisMode);
142
ebf31fda 143 if (!vertex) {
144 if (debug)
145 Printf("AliPWG0Helper::GetVertex: No vertex found in ESD");
770a1f1d 146 return 0;
ebf31fda 147 }
770a1f1d 148
149 // check Ncontributors
ebf31fda 150 if (vertex->GetNContributors() <= 0) {
85737496 151 if (debug){
152 Printf("AliPWG0Helper::GetVertex: NContributors() <= 0: %d",vertex->GetNContributors());
153 Printf("AliPWG0Helper::GetVertex: NIndices(): %d",vertex->GetNIndices());
51f6de65 154 vertex->Print();
85737496 155 }
770a1f1d 156 return 0;
ebf31fda 157 }
770a1f1d 158
159 // check resolution
160 Double_t zRes = vertex->GetZRes();
51f6de65 161 if (zRes == 0) {
162 Printf("AliPWG0Helper::GetVertex: UNEXPECTED: resolution is 0.");
770a1f1d 163 return 0;
ebf31fda 164 }
165
166 if (debug)
85737496 167 {
168 Printf("AliPWG0Helper::GetVertex: Returning valid vertex: %s", vertex->GetTitle());
169 vertex->Print();
170 }
770a1f1d 171
172 return vertex;
173}
174
04a7657f 175//____________________________________________________________________
7584d357 176Bool_t AliPWG0Helper::IsPrimaryCharged(TParticle* aParticle, Int_t aTotalPrimaries, Bool_t adebug)
04a7657f 177{
178 //
25db2d85 179 // this function checks if a particle from the event generator (i.e. among the nPrim particles in the stack)
180 // shall be counted as a primary particle
181 //
04a7657f 182 // This function or a equivalent should be available in some common place of AliRoot
183 //
dd367a14 184 // WARNING: Call this function only for particles that are among the particles from the event generator!
185 // --> stack->Particle(id) with id < stack->GetNprimary()
04a7657f 186
187 // if the particle has a daughter primary, we do not want to count it
188 if (aParticle->GetFirstDaughter() != -1 && aParticle->GetFirstDaughter() < aTotalPrimaries)
189 {
7584d357 190 if (adebug)
25db2d85 191 printf("Dropping particle because it has a daughter among the primaries.\n");
04a7657f 192 return kFALSE;
193 }
194
195 Int_t pdgCode = TMath::Abs(aParticle->GetPdgCode());
85737496 196
04a7657f 197
198 // skip quarks and gluon
199 if (pdgCode <= 10 || pdgCode == 21)
200 {
7584d357 201 if (adebug)
25db2d85 202 printf("Dropping particle because it is a quark or gluon.\n");
04a7657f 203 return kFALSE;
204 }
205
85737496 206 Int_t status = aParticle->GetStatusCode();
207 // skip non final state particles..
208 if(status!=1){
209 if (adebug)
210 printf("Dropping particle because it is not a final state particle.\n");
211 return kFALSE;
212 }
213
04a7657f 214 if (strcmp(aParticle->GetName(),"XXX") == 0)
215 {
1c15d51a 216 Printf("WARNING: There is a particle named XXX (pdg code %d).", pdgCode);
04a7657f 217 return kFALSE;
218 }
219
220 TParticlePDG* pdgPart = aParticle->GetPDG();
221
222 if (strcmp(pdgPart->ParticleClass(),"Unknown") == 0)
223 {
1c15d51a 224 Printf("WARNING: There is a particle with an unknown particle class (pdg code %d).", pdgCode);
04a7657f 225 return kFALSE;
226 }
227
228 if (pdgPart->Charge() == 0)
229 {
7584d357 230 if (adebug)
25db2d85 231 printf("Dropping particle because it is not charged.\n");
04a7657f 232 return kFALSE;
233 }
234
235 return kTRUE;
236}
847489f7 237
238//____________________________________________________________________
29771dc8 239void AliPWG0Helper::CreateProjections(TH3* hist, Bool_t save)
847489f7 240{
241 // create projections of 3d hists to all 2d combinations
242 // the histograms are not returned, just use them from memory or use this to create them in a file
243
244 TH1* proj = hist->Project3D("yx");
245 proj->SetXTitle(hist->GetXaxis()->GetTitle());
246 proj->SetYTitle(hist->GetYaxis()->GetTitle());
29771dc8 247 if (save)
248 proj->Write();
847489f7 249
250 proj = hist->Project3D("zx");
251 proj->SetXTitle(hist->GetXaxis()->GetTitle());
252 proj->SetYTitle(hist->GetZaxis()->GetTitle());
29771dc8 253 if (save)
254 proj->Write();
847489f7 255
256 proj = hist->Project3D("zy");
257 proj->SetXTitle(hist->GetYaxis()->GetTitle());
258 proj->SetYTitle(hist->GetZaxis()->GetTitle());
29771dc8 259 if (save)
260 proj->Write();
847489f7 261}
1afae8ff 262
263//____________________________________________________________________
29771dc8 264void AliPWG0Helper::CreateDividedProjections(TH3* hist, TH3* hist2, const char* axis, Bool_t putErrors, Bool_t save)
1afae8ff 265{
266 // create projections of the 3d hists divides them
267 // axis decides to which plane, if axis is 0 to all planes
268 // the histograms are not returned, just use them from memory or use this to create them in a file
269
270 if (axis == 0)
271 {
29771dc8 272 CreateDividedProjections(hist, hist2, "yx", putErrors, save);
273 CreateDividedProjections(hist, hist2, "zx", putErrors, save);
274 CreateDividedProjections(hist, hist2, "zy", putErrors, save);
1afae8ff 275
276 return;
277 }
278
279 TH1* proj = hist->Project3D(axis);
0ab29cfa 280
281 if (strlen(axis) == 2)
282 {
283 proj->SetYTitle(GetAxisTitle(hist, axis[0]));
284 proj->SetXTitle(GetAxisTitle(hist, axis[1]));
285 }
286 else if (strlen(axis) == 1)
287 proj->SetXTitle(GetAxisTitle(hist, axis[0]));
1afae8ff 288
289 TH1* proj2 = hist2->Project3D(axis);
0ab29cfa 290 if (strlen(axis) == 2)
291 {
292 proj2->SetYTitle(GetAxisTitle(hist2, axis[0]));
293 proj2->SetXTitle(GetAxisTitle(hist2, axis[1]));
294 }
295 else if (strlen(axis) == 1)
296 proj2->SetXTitle(GetAxisTitle(hist2, axis[0]));
1afae8ff 297
298 TH1* division = dynamic_cast<TH1*> (proj->Clone(Form("%s_div_%s", proj->GetName(), proj2->GetName())));
29771dc8 299 //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()));
300 //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 301 division->Divide(proj, proj2, 1, 1, "B");
29771dc8 302 division->SetTitle(Form("%s divided %s", proj->GetTitle(), proj2->GetTitle()));
0ab29cfa 303
304 if (putErrors)
305 {
306 division->Sumw2();
307 if (division->GetDimension() == 1)
308 {
309 Int_t nBins = division->GetNbinsX();
29771dc8 310 for (Int_t i = 1; i <= nBins; ++i)
0ab29cfa 311 if (proj2->GetBinContent(i) != 0)
312 division->SetBinError(i, TMath::Sqrt(proj->GetBinContent(i)) / proj2->GetBinContent(i));
313 }
314 else if (division->GetDimension() == 2)
315 {
316 Int_t nBinsX = division->GetNbinsX();
317 Int_t nBinsY = division->GetNbinsY();
29771dc8 318 for (Int_t i = 1; i <= nBinsX; ++i)
319 for (Int_t j = 1; j <= nBinsY; ++j)
0ab29cfa 320 if (proj2->GetBinContent(i, j) != 0)
321 division->SetBinError(i, j, TMath::Sqrt(proj->GetBinContent(i, j)) / proj2->GetBinContent(i, j));
322 }
323 }
29771dc8 324
325 if (save)
326 {
327 proj->Write();
328 proj2->Write();
329 division->Write();
330 }
1afae8ff 331}
332
92d2d8ad 333//____________________________________________________________________
25db2d85 334const char* AliPWG0Helper::GetAxisTitle(TH3* hist, const char axis)
92d2d8ad 335{
336 // returns the title of the axis given in axis (x, y, z)
337
338 if (axis == 'x')
339 return hist->GetXaxis()->GetTitle();
340 else if (axis == 'y')
341 return hist->GetYaxis()->GetTitle();
342 else if (axis == 'z')
343 return hist->GetZaxis()->GetTitle();
344
345 return 0;
346}
116083b1 347
6b62a9c7 348
1c15d51a 349AliPWG0Helper::MCProcessType AliPWG0Helper::GetPythiaEventProcessType(AliGenEventHeader* aHeader, Bool_t adebug) {
6b62a9c7 350
351 AliGenPythiaEventHeader* pythiaGenHeader = dynamic_cast<AliGenPythiaEventHeader*>(aHeader);
352
353 if (!pythiaGenHeader) {
354 printf("AliPWG0Helper::GetProcessType : Unknown gen Header type). \n");
355 return kInvalidProcess;
356 }
357
358
359 Int_t pythiaType = pythiaGenHeader->ProcessType();
a67484a6 360 fgLastProcessType = pythiaType;
6b62a9c7 361 MCProcessType globalType = kInvalidProcess;
362
363
364 if (adebug) {
365 printf("AliPWG0Helper::GetProcessType : Pythia process type found: %d \n",pythiaType);
366 }
367
368
369 if(pythiaType==92||pythiaType==93){
370 globalType = kSD;
371 }
372 else if(pythiaType==94){
373 globalType = kDD;
374 }
375 //else if(pythiaType != 91){ // also exclude elastic to be sure... CKB??
376 else {
377 globalType = kND;
378 }
379 return globalType;
380}
381
382
1c15d51a 383AliPWG0Helper::MCProcessType AliPWG0Helper::GetDPMjetEventProcessType(AliGenEventHeader* aHeader, Bool_t adebug) {
116083b1 384 //
385 // get the process type of the event.
386 //
387
388 // can only read pythia headers, either directly or from cocktalil header
6b62a9c7 389 AliGenDPMjetEventHeader* dpmJetGenHeader = dynamic_cast<AliGenDPMjetEventHeader*>(aHeader);
390
391 if (!dpmJetGenHeader) {
392 printf("AliPWG0Helper::GetDPMjetProcessType : Unknown header type (not DPMjet or). \n");
393 return kInvalidProcess;
394 }
395
396 Int_t dpmJetType = dpmJetGenHeader->ProcessType();
a67484a6 397 fgLastProcessType = dpmJetType;
6b62a9c7 398 MCProcessType globalType = kInvalidProcess;
399
400
401 if (adebug) {
402 printf("AliPWG0Helper::GetDPMJetProcessType : DPMJet process type found: %d \n",dpmJetType);
403 }
404
405
406 if(dpmJetType == 1){ // this is explicitly inelastic
407 globalType = kND;
408 }
409 else if(dpmJetType==5||dpmJetType==6){
410 globalType = kSD;
411 }
412 else if(dpmJetType==7||dpmJetType==4){// DD and double pomeron
413 globalType = kDD;
414 }
415 return globalType;
416}
417
418
1c15d51a 419AliPWG0Helper::MCProcessType AliPWG0Helper::GetEventProcessType(AliHeader* aHeader, Bool_t adebug) {
6b62a9c7 420 //
421 // get the process type of the event.
422 //
423
424
425 // Check for simple headers first
426
116083b1 427 AliGenPythiaEventHeader* pythiaGenHeader = dynamic_cast<AliGenPythiaEventHeader*>(aHeader->GenEventHeader());
6b62a9c7 428 if (pythiaGenHeader) {
429 return GetPythiaEventProcessType(pythiaGenHeader,adebug);
430 }
116083b1 431
6b62a9c7 432 AliGenDPMjetEventHeader* dpmJetGenHeader = dynamic_cast<AliGenDPMjetEventHeader*>(aHeader->GenEventHeader());
433 if (dpmJetGenHeader) {
434 return GetDPMjetEventProcessType(dpmJetGenHeader,adebug);
435 }
436
116083b1 437
6b62a9c7 438 // check for cocktail
116083b1 439
6b62a9c7 440 AliGenCocktailEventHeader* genCocktailHeader = dynamic_cast<AliGenCocktailEventHeader*>(aHeader->GenEventHeader());
441 if (!genCocktailHeader) {
442 printf("AliPWG0Helper::GetProcessType : Unknown header type (not Pythia or Cocktail). \n");
443 return kInvalidProcess;
444 }
116083b1 445
6b62a9c7 446 TList* headerList = genCocktailHeader->GetHeaders();
447 if (!headerList) {
448 return kInvalidProcess;
449 }
450
451 for (Int_t i=0; i<headerList->GetEntries(); i++) {
452
453 pythiaGenHeader = dynamic_cast<AliGenPythiaEventHeader*>(headerList->At(i));
454 if (pythiaGenHeader) {
455 return GetPythiaEventProcessType(pythiaGenHeader,adebug);
116083b1 456 }
457
6b62a9c7 458 dpmJetGenHeader = dynamic_cast<AliGenDPMjetEventHeader*>(headerList->At(i));
459 if (dpmJetGenHeader) {
460 return GetDPMjetEventProcessType(dpmJetGenHeader,adebug);
116083b1 461 }
462 }
6b62a9c7 463 return kInvalidProcess;
464}
116083b1 465
116083b1 466
116083b1 467
468//____________________________________________________________________
469TParticle* AliPWG0Helper::FindPrimaryMother(AliStack* stack, Int_t label)
470{
471 //
472 // Finds the first mother among the primary particles of the particle identified by <label>,
473 // i.e. the primary that "caused" this particle
474 //
475
476 Int_t motherLabel = FindPrimaryMotherLabel(stack, label);
477 if (motherLabel < 0)
478 return 0;
479
480 return stack->Particle(motherLabel);
481}
482
483//____________________________________________________________________
484Int_t AliPWG0Helper::FindPrimaryMotherLabel(AliStack* stack, Int_t label)
485{
486 //
487 // Finds the first mother among the primary particles of the particle identified by <label>,
488 // i.e. the primary that "caused" this particle
489 //
490 // returns its label
491 //
492
493 Int_t nPrim = stack->GetNprimary();
494
495 while (label >= nPrim)
496 {
497 //printf("Particle %d (pdg %d) is not a primary. Let's check its mother %d\n", label, mother->GetPdgCode(), mother->GetMother(0));
498
499 TParticle* particle = stack->Particle(label);
500 if (!particle)
501 {
502 AliDebugGeneral("FindPrimaryMother", AliLog::kError, Form("UNEXPECTED: particle with label %d not found in stack.", label));
503 return -1;
504 }
505
506 // find mother
507 if (particle->GetMother(0) < 0)
508 {
509 AliDebugGeneral("FindPrimaryMother", AliLog::kError, Form("UNEXPECTED: Could not find mother of secondary particle %d.", label));
510 return -1;
511 }
512
513 label = particle->GetMother(0);
514 }
515
516 return label;
517}
9cd015f7 518
dd367a14 519//____________________________________________________________________
520void AliPWG0Helper::NormalizeToBinWidth(TH1* hist)
521{
522 //
523 // normalizes a 1-d histogram to its bin width
524 //
525
526 for (Int_t i=1; i<=hist->GetNbinsX(); ++i)
527 {
528 hist->SetBinContent(i, hist->GetBinContent(i) / hist->GetBinWidth(i));
529 hist->SetBinError(i, hist->GetBinError(i) / hist->GetBinWidth(i));
530 }
531}
532
533//____________________________________________________________________
534void AliPWG0Helper::NormalizeToBinWidth(TH2* hist)
535{
536 //
537 // normalizes a 2-d histogram to its bin width (x width * y width)
538 //
539
540 for (Int_t i=1; i<=hist->GetNbinsX(); ++i)
541 for (Int_t j=1; j<=hist->GetNbinsY(); ++j)
542 {
543 Double_t factor = hist->GetXaxis()->GetBinWidth(i) * hist->GetYaxis()->GetBinWidth(j);
544 hist->SetBinContent(i, j, hist->GetBinContent(i, j) / factor);
545 hist->SetBinError(i, j, hist->GetBinError(i, j) / factor);
546 }
547}
d1f50534 548
549//____________________________________________________________________
550void AliPWG0Helper::PrintConf(AnalysisMode analysisMode, Trigger trigger)
551{
552 //
553 // Prints the given configuration
554 //
555
556 TString str(">>>> Running with ");
557
558 switch (analysisMode)
559 {
773f84dc 560 case kInvalid: str += "invalid setting"; break;
d1f50534 561 case kSPD : str += "SPD-only"; break;
562 case kTPC : str += "TPC-only"; break;
563 case kTPCITS : str += "Global tracking"; break;
564 }
565
566 str += " and trigger ";
567
568 switch (trigger)
569 {
570 case kMB1 : str += "MB1"; break;
571 case kMB2 : str += "MB2"; break;
1c15d51a 572 case kSPDFASTOR : str += "SPD FASTOR"; break;
d1f50534 573 }
574
575 str += " <<<<";
576
577 Printf("%s", str.Data());
578}
579