3 // All the definitions needed for building a RSN histogram
5 // -- properties of resonance (mass, PDG code if needed)
6 // -- properties of daughters (assigned mass, charges)
7 // -- definition of output histogram
10 #include "Riostream.h"
17 #include "THnSparse.h"
19 #include "TClonesArray.h"
21 #include "AliRsnMiniParticle.h"
22 #include "AliRsnMiniPair.h"
23 #include "AliRsnMiniEvent.h"
24 #include "AliAODEvent.h"
27 #include "AliRsnCutSet.h"
28 #include "AliRsnMiniAxis.h"
29 #include "AliRsnMiniOutput.h"
30 #include "AliRsnMiniValue.h"
32 ClassImp(AliRsnMiniOutput)
34 //__________________________________________________________________________________________________
35 AliRsnMiniOutput::AliRsnMiniOutput() :
38 fComputation(kComputations),
43 fAxes("AliRsnMiniAxis", 0),
51 fCheckHistRange(kTRUE)
57 fCutID[0] = fCutID[1] = -1;
58 fDaughter[0] = fDaughter[1] = AliRsnDaughter::kUnknown;
59 fCharge[0] = fCharge[1] = 0;
62 //__________________________________________________________________________________________________
63 AliRsnMiniOutput::AliRsnMiniOutput(const char *name, EOutputType type, EComputation src) :
71 fAxes("AliRsnMiniAxis", 0),
79 fCheckHistRange(kTRUE)
85 fCutID[0] = fCutID[1] = -1;
86 fDaughter[0] = fDaughter[1] = AliRsnDaughter::kUnknown;
87 fCharge[0] = fCharge[1] = 0;
90 //__________________________________________________________________________________________________
91 AliRsnMiniOutput::AliRsnMiniOutput(const char *name, const char *outType, const char *compType) :
94 fComputation(kComputations),
99 fAxes("AliRsnMiniAxis", 0),
107 fCheckHistRange(kTRUE)
110 // Constructor, with a more user friendly implementation, where
111 // the user sets the type of output and computations through conventional strings:
114 // -- "HIST" --> common histogram (up to 3 dimensions)
115 // -- "SPARSE" --> sparse histogram
118 // -- "EVENT" --> event-only computations
119 // -- "PAIR" --> track pair computations (default)
120 // -- "MIX" --> event mixing (like track pair, but different events)
121 // -- "ROTATE1" --> rotated background (rotate first track)
122 // -- "ROTATE2" --> rotated background (rotate second track)
123 // -- "TRUE" --> true pairs (like track pair, but checking that come from same mother)
124 // -- "MOTHER" --> mother (loop on MC directly for mothers --> denominator of efficiency)
129 // understand output type
132 if (!input.CompareTo("HIST"))
133 fOutputType = kHistogram;
134 else if (!input.CompareTo("SPARSE"))
135 fOutputType = kHistogramSparse;
137 AliWarning(Form("String '%s' does not define a meaningful output type", outType));
139 // understand computation type
142 if (!input.CompareTo("EVENT"))
143 fComputation = kEventOnly;
144 else if (!input.CompareTo("PAIR"))
145 fComputation = kTrackPair;
146 else if (!input.CompareTo("MIX"))
147 fComputation = kTrackPairMix;
148 else if (!input.CompareTo("ROTATE1"))
149 fComputation = kTrackPairRotated1;
150 else if (!input.CompareTo("ROTATE2"))
151 fComputation = kTrackPairRotated2;
152 else if (!input.CompareTo("TRUE"))
153 fComputation = kTruePair;
154 else if (!input.CompareTo("MOTHER"))
155 fComputation = kMother;
157 AliWarning(Form("String '%s' does not define a meaningful computation type", compType));
159 fCutID[0] = fCutID[1] = -1;
160 fDaughter[0] = fDaughter[1] = AliRsnDaughter::kUnknown;
161 fCharge[0] = fCharge[1] = 0;
164 //__________________________________________________________________________________________________
165 AliRsnMiniOutput::AliRsnMiniOutput(const AliRsnMiniOutput ©) :
167 fOutputType(copy.fOutputType),
168 fComputation(copy.fComputation),
169 fMotherPDG(copy.fMotherPDG),
170 fMotherMass(copy.fMotherMass),
171 fPairCuts(copy.fPairCuts),
172 fOutputID(copy.fOutputID),
174 fComputed(copy.fComputed),
181 fCheckHistRange(copy.fCheckHistRange)
188 for (i = 0; i < 2; i++) {
189 fCutID[i] = copy.fCutID[i];
190 fDaughter[i] = copy.fDaughter[i];
191 fCharge[i] = copy.fCharge[i];
195 //__________________________________________________________________________________________________
196 AliRsnMiniOutput &AliRsnMiniOutput::operator=(const AliRsnMiniOutput ©)
199 // Assignment operator
203 fOutputType = copy.fOutputType;
204 fComputation = copy.fComputation;
205 fMotherPDG = copy.fMotherPDG;
206 fMotherMass = copy.fMotherMass;
207 fPairCuts = copy.fPairCuts;
208 fOutputID = copy.fOutputID;
210 fComputed = copy.fComputed;
214 for (i = 0; i < 2; i++) {
215 fCutID[i] = copy.fCutID[i];
216 fDaughter[i] = copy.fDaughter[i];
217 fCharge[i] = copy.fCharge[i];
222 fMaxNSisters = copy.fMaxNSisters;
223 fCheckP = copy.fCheckP;
224 fCheckHistRange = copy.fCheckHistRange;
230 //__________________________________________________________________________________________________
231 void AliRsnMiniOutput::AddAxis(Int_t i, Int_t nbins, Double_t min, Double_t max)
234 // Create a new axis reference
237 Int_t size = fAxes.GetEntries();
238 new (fAxes[size]) AliRsnMiniAxis(i, nbins, min, max);
241 //__________________________________________________________________________________________________
242 void AliRsnMiniOutput::AddAxis(Int_t i, Double_t min, Double_t max, Double_t step)
245 // Create a new axis reference
248 Int_t size = fAxes.GetEntries();
249 new (fAxes[size]) AliRsnMiniAxis(i, min, max, step);
252 //__________________________________________________________________________________________________
253 void AliRsnMiniOutput::AddAxis(Int_t i, Int_t nbins, Double_t *values)
256 // Create a new axis reference
259 Int_t size = fAxes.GetEntries();
260 new (fAxes[size]) AliRsnMiniAxis(i, nbins, values);
263 //__________________________________________________________________________________________________
264 Bool_t AliRsnMiniOutput::Init(const char *prefix, TList *list)
267 // Initialize properly the histogram and add it to the argument list
271 AliError("Required an output list");
276 Int_t size = fAxes.GetEntries();
278 AliWarning(Form("[%s] Cannot initialize histogram with less than 1 axis", GetName()));
282 switch (fOutputType) {
285 CreateHistogram(Form("%s_%s", prefix, GetName()));
287 AliInfo(Form("[%s] Added %d > 3 axes. Creating a sparse histogram", GetName(), size));
288 fOutputType = kHistogramSparse;
289 CreateHistogramSparse(Form("%s_%s", prefix, GetName()));
292 case kHistogramSparse:
293 CreateHistogramSparse(Form("%s_%s", prefix, GetName()));
296 AliError("Wrong output histogram definition");
301 //__________________________________________________________________________________________________
302 void AliRsnMiniOutput::CreateHistogram(const char *name)
305 // Initialize the 'default' TH1 output object.
306 // In case one of the expected axes is NULL, the initialization fails.
309 Int_t size = fAxes.GetEntries();
310 AliInfo(Form("Histogram name = '%s', with %d axes", name, size));
312 // we expect to have maximum 3 axes in this case
313 AliRsnMiniAxis *xAxis = 0x0, *yAxis = 0x0, *zAxis = 0x0;
314 if (size >= 1) xAxis = (AliRsnMiniAxis *)fAxes[0];
315 if (size >= 2) yAxis = (AliRsnMiniAxis *)fAxes[1];
316 if (size >= 3) zAxis = (AliRsnMiniAxis *)fAxes[2];
318 // create histogram depending on the number of axes
320 if (xAxis && yAxis && zAxis) {
321 h1 = new TH3F(name, "", xAxis->NBins(), xAxis->BinArray(), yAxis->NBins(), yAxis->BinArray(), zAxis->NBins(), zAxis->BinArray());
322 } else if (xAxis && yAxis) {
323 h1 = new TH2F(name, "", xAxis->NBins(), xAxis->BinArray(), yAxis->NBins(), yAxis->BinArray());
325 h1 = new TH1F(name, "", xAxis->NBins(), xAxis->BinArray());
327 AliError("No axis was initialized");
331 // switch the correct computation of errors
335 fOutputID = fList->IndexOf(h1);
339 //________________________________________________________________________________________
340 void AliRsnMiniOutput::CreateHistogramSparse(const char *name)
343 // Initialize the THnSparse output object.
344 // In case one of the expected axes is NULL, the initialization fails.
347 Int_t size = fAxes.GetEntries();
348 AliInfo(Form("Sparse histogram name = '%s', with %d axes", name, size));
350 // retrieve binnings and sizes of all axes
351 // since the check for null values is done in Init(),
352 // we assume that here they must all be well defined
353 Int_t i, *nbins = new Int_t[size];
354 for (i = 0; i < size; i++) {
355 AliRsnMiniAxis *axis = (AliRsnMiniAxis *)fAxes[i];
356 nbins[i] = axis->NBins();
359 // create fHSparseogram
360 THnSparseF *h1 = new THnSparseF(name, "", size, nbins);
362 // update the various axes using the definitions given in the array of axes here
363 for (i = 0; i < size; i++) {
364 AliRsnMiniAxis *axis = (AliRsnMiniAxis *)fAxes[i];
365 h1->GetAxis(i)->Set(nbins[i], axis->BinArray());
375 fOutputID = fList->IndexOf(h1);
379 //________________________________________________________________________________________
380 Bool_t AliRsnMiniOutput::FillEvent(AliRsnMiniEvent *event, TClonesArray *valueList)
383 // Compute values for event-based computations (does not use the pair)
386 // check computation type
387 if (fComputation != kEventOnly) {
388 AliError("This method can be called only for event-based computations");
393 ComputeValues(event, valueList);
398 //________________________________________________________________________________________
399 Bool_t AliRsnMiniOutput::FillMother(const AliRsnMiniPair *pair, AliRsnMiniEvent *event, TClonesArray *valueList)
402 // Compute values for mother-based computations
405 // check computation type
406 if (fComputation != kMother) {
407 AliError("This method can be called only for mother-based computations");
411 // copy passed pair info
414 // check pair against cuts
415 if (fPairCuts) if (!fPairCuts->IsSelected(&fPair)) return kFALSE;
418 ComputeValues(event, valueList);
423 //________________________________________________________________________________________
424 Int_t AliRsnMiniOutput::FillPair(AliRsnMiniEvent *event1, AliRsnMiniEvent *event2, TClonesArray *valueList, Bool_t refFirst)
427 // Loops on the passed mini-event, and for each pair of particles
428 // which satisfy the charge and cut requirements defined here, add an entry.
429 // Returns the number of successful fillings.
430 // Last argument tells if the reference event for event-based values is the first or the second.
433 // check computation type
434 Bool_t okComp = kFALSE;
435 if (fComputation == kTrackPair) okComp = kTRUE;
436 if (fComputation == kTrackPairMix) okComp = kTRUE;
437 if (fComputation == kTrackPairRotated1) okComp = kTRUE;
438 if (fComputation == kTrackPairRotated2) okComp = kTRUE;
439 if (fComputation == kTruePair) okComp = kTRUE;
441 AliError(Form("[%s] This method can be called only for pair-based computations", GetName()));
446 Int_t i1, i2, start, nadded = 0;
447 AliRsnMiniParticle *p1, *p2;
449 // it is necessary to know if criteria for the two daughters are the same
450 // and if the two events are the same or not (mixing)
451 //Bool_t sameCriteria = ((fCharge[0] == fCharge[1]) && (fCutID[0] == fCutID[1]));
452 Bool_t sameCriteria = ((fCharge[0] == fCharge[1]) && (fDaughter[0] == fDaughter[1]));
453 Bool_t sameEvent = (event1->ID() == event2->ID());
455 TString selList1 = "";
456 TString selList2 = "";
457 Int_t n1 = event1->CountParticles(fSel1, fCharge[0], fCutID[0]);
458 Int_t n2 = event2->CountParticles(fSel2, fCharge[1], fCutID[1]);
459 for (i1 = 0; i1 < n1; i1++) selList1.Append(Form("%d ", fSel1[i1]));
460 for (i2 = 0; i2 < n2; i2++) selList2.Append(Form("%d ", fSel2[i2]));
461 AliDebugClass(1, Form("[%10s] Part #1: [%s] -- evID %6d -- charge = %c -- cut ID = %d --> %4d tracks (%s)", GetName(), (event1 == event2 ? "def" : "mix"), event1->ID(), fCharge[0], fCutID[0], n1, selList1.Data()));
462 AliDebugClass(1, Form("[%10s] Part #2: [%s] -- evID %6d -- charge = %c -- cut ID = %d --> %4d tracks (%s)", GetName(), (event1 == event2 ? "def" : "mix"), event2->ID(), fCharge[1], fCutID[1], n2, selList2.Data()));
464 AliDebugClass(1, "No pairs to mix");
469 for (i1 = 0; i1 < n1; i1++) {
470 p1 = event1->GetParticle(fSel1[i1]);
471 //p1 = event1->GetParticle(i1);
472 //if (p1->Charge() != fCharge[0]) continue;
473 //if (!p1->HasCutBit(fCutID[0])) continue;
474 // define starting point for inner loop
475 // if daughter selection criteria (charge, cuts) are the same
476 // and the two events coincide, internal loop must start from
477 // the first track *after* current one;
478 // otherwise it starts from the beginning
479 start = ((sameEvent && sameCriteria) ? i1 + 1 : 0);
480 AliDebugClass(2, Form("Start point = %d", start));
482 for (i2 = start; i2 < n2; i2++) {
483 p2 = event2->GetParticle(fSel2[i2]);
484 //p2 = event2->GetParticle(i2);
485 //if (p2->Charge() != fCharge[1]) continue;
486 //if (!p2->HasCutBit(fCutID[1])) continue;
487 // avoid to mix a particle with itself
488 if (sameEvent && (p1->Index() == p2->Index())) {
489 AliDebugClass(2, "Skipping same index");
493 fPair.Fill(p1, p2, GetMass(0), GetMass(1), fMotherMass);
494 // do rotation if needed
495 if (fComputation == kTrackPairRotated1) fPair.InvertP(kTRUE);
496 if (fComputation == kTrackPairRotated2) fPair.InvertP(kFALSE);
497 // if required, check that this is a true pair
498 if (fComputation == kTruePair) {
499 if (fPair.Mother() < 0) {
501 } else if (fPair.MotherPDG() != fMotherPDG) {
504 Bool_t decayMatch = kFALSE;
505 if (p1->PDGAbs() == AliRsnDaughter::SpeciesPDG(fDaughter[0]) && p2->PDGAbs() == AliRsnDaughter::SpeciesPDG(fDaughter[1]))
507 if (p2->PDGAbs() == AliRsnDaughter::SpeciesPDG(fDaughter[0]) && p1->PDGAbs() == AliRsnDaughter::SpeciesPDG(fDaughter[1]))
509 if (!decayMatch) continue;
510 if ( (fMaxNSisters>0) && (p1->NTotSisters()==p2->NTotSisters()) && (p1->NTotSisters()>fMaxNSisters)) continue;
511 if ( fCheckP &&(TMath::Abs(fPair.PmotherX()-(p1->Px(1)+p2->Px(1)))/(TMath::Abs(fPair.PmotherX())+1.e-13)) > 0.00001 &&
512 (TMath::Abs(fPair.PmotherY()-(p1->Py(1)+p2->Py(1)))/(TMath::Abs(fPair.PmotherY())+1.e-13)) > 0.00001 &&
513 (TMath::Abs(fPair.PmotherZ()-(p1->Pz(1)+p2->Pz(1)))/(TMath::Abs(fPair.PmotherZ())+1.e-13)) > 0.00001 ) continue;
515 // check pair against cuts
517 if (!fPairCuts->IsSelected(&fPair)) continue;
519 // get computed values & fill histogram
521 if (refFirst) ComputeValues(event1, valueList); else ComputeValues(event2, valueList);
523 } // end internal loop
524 } // end external loop
526 AliDebugClass(1, Form("Pairs added in total = %4d", nadded));
530 //________________________________________________________________________________________
531 void AliRsnMiniOutput::ComputeValues(AliRsnMiniEvent *event, TClonesArray *valueList)
534 // Using the arguments and the internal 'fPair' data member,
535 // compute all values to be stored in the histogram
538 // check size of computed array
539 Int_t size = fAxes.GetEntries();
540 if (fComputed.GetSize() != size) fComputed.Set(size);
542 Int_t i, ival, nval = valueList->GetEntries();
544 for (i = 0; i < size; i++) {
546 AliRsnMiniAxis *axis = (AliRsnMiniAxis *)fAxes[i];
548 AliError("Null axis");
551 ival = axis->GetValueID();
552 if (ival < 0 || ival >= nval) {
553 AliError(Form("Required value #%d, while maximum is %d", ival, nval));
556 AliRsnMiniValue *val = (AliRsnMiniValue *)valueList->At(ival);
558 AliError(Form("Value in position #%d is NULL", ival));
561 // if none of the above exit points is taken, compute value
562 fComputed[i] = val->Eval(&fPair, event);
566 //________________________________________________________________________________________
567 void AliRsnMiniOutput::FillHistogram()
570 // Fills the internal histogram using the current values stored in the
571 // 'fComputed' array, in the order as they are stored, up to the max
572 // dimension of the initialized histogram itself.
575 // retrieve object from list
577 AliError("List pointer is NULL");
580 TObject *obj = fList->At(fOutputID);
582 if (obj->InheritsFrom(TH1F::Class())) {
583 ((TH1F *)obj)->Fill(fComputed[0]);
584 } else if (obj->InheritsFrom(TH2F::Class())) {
585 ((TH2F *)obj)->Fill(fComputed[0], fComputed[1]);
586 } else if (obj->InheritsFrom(TH3F::Class())) {
587 ((TH3F *)obj)->Fill(fComputed[0], fComputed[1], fComputed[2]);
588 } else if (obj->InheritsFrom(THnSparseF::Class())) {
589 THnSparseF *h = (THnSparseF *)obj;
590 if (fCheckHistRange) {
591 for (Int_t iAxis = 0; iAxis<h->GetNdimensions(); iAxis++) {
592 if (fComputed.At(iAxis)>h->GetAxis(iAxis)->GetXmax() || fComputed.At(iAxis)<h->GetAxis(iAxis)->GetXmin()) return;
595 h->Fill(fComputed.GetArray());
597 AliError("No output initialized");