]> git.uio.no Git - u/mrichter/AliRoot.git/blame - PWGHF/hfe/AliHFEcollection.cxx
Fix of sigmaZ for crossing tracklets from Alex
[u/mrichter/AliRoot.git] / PWGHF / hfe / AliHFEcollection.cxx
CommitLineData
809a4336 1/**************************************************************************
2* Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3* *
4* Author: The ALICE Off-line Project. *
5* Contributors are mentioned in the code where appropriate. *
6* *
7* Permission to use, copy, modify and distribute this software and its *
8* documentation strictly for non-commercial purposes is hereby granted *
9* without fee, provided that the above copyright notice appears in all *
10* copies and that both the copyright notice and this permission notice *
11* appear in the supporting documentation. The authors make no claims *
12* about the suitability of this software for any purpose. It is *
13* provided "as is" without express or implied warranty. *
14**************************************************************************/
50685501 15//
16// Collection class for histograms
17// Stores either histograms or vectors of histograms
18//
19// Author:
20// Matus Kalisky <matus.kalisky@cern.ch>
21//
809a4336 22
809a4336 23#include <TH1F.h>
24#include <TH2F.h>
8c1c76e9 25#include <TH3F.h>
9bcfd1ab 26#include <THnSparse.h>
27#include <TProfile.h>
809a4336 28#include <TString.h>
29#include <TBrowser.h>
9bcfd1ab 30#include <TMath.h>
809a4336 31
32#include "AliLog.h"
e156c3bb 33
809a4336 34#include "AliHFEcollection.h"
35
36using namespace std;
37
38
39ClassImp(AliHFEcollection)
40
41//___________________________________________________________________
42AliHFEcollection::AliHFEcollection():
43 TNamed()
69ac0e6f 44 , fList(NULL)
809a4336 45{
9bcfd1ab 46
809a4336 47 //
48 // default constructor
49 //
809a4336 50}
51//___________________________________________________________________
faee3b18 52AliHFEcollection::AliHFEcollection(const char* name, const char* title):
809a4336 53 TNamed(name, title)
69ac0e6f 54 , fList(NULL)
809a4336 55{
56
57 //
58 // constructor
59 //
60
70da6c5a 61 fList = new THashList();
bf892a6a 62 if(fList){
63 fList->SetOwner();
64 fList->SetName(Form("list_%s", name));
809a4336 65 }
66}
67//___________________________________________________________________
68AliHFEcollection::AliHFEcollection(const AliHFEcollection &c) :
69 TNamed(c)
69ac0e6f 70 , fList(NULL)
809a4336 71{
72
73 //
74 // copy operator
75 //
76
77 c.Copy(*this);
78}
79//___________________________________________________________________
80AliHFEcollection &AliHFEcollection::operator=(const AliHFEcollection &ref)
81{
82 //
83 // Assignment operator
84 //
85
86 if(this != &ref){
87 ref.Copy(*this);
88 }
89 return *this;
90}
91//___________________________________________________________________
92void AliHFEcollection::Copy(TObject &ref) const {
9bcfd1ab 93
809a4336 94 //
95 // Performs the copying of the object
96 //
9bcfd1ab 97
809a4336 98 AliHFEcollection &target = dynamic_cast<AliHFEcollection &>(ref);
99
70da6c5a 100 // Clone List Content
101 target.fList = new THashList();
e156c3bb 102 target.fList->SetOwner();
70da6c5a 103 for(Int_t ien = 0; ien < fList->GetEntries(); ien++)
104 target.fList->Add(fList->At(ien)->Clone());
809a4336 105}
106//___________________________________________________________________
107AliHFEcollection::~AliHFEcollection(){
9bcfd1ab 108
50685501 109 //
110 // Destructor
111 //
70da6c5a 112 delete fList;
3a72645a 113 AliDebug(1, "DESTRUCTOR");
809a4336 114}
115//___________________________________________________________________
faee3b18 116Bool_t AliHFEcollection::CreateTH1F(const char* name, const char* title, Int_t nBin, Float_t nMin, Float_t nMax, Int_t logAxis){
9bcfd1ab 117
50685501 118 //
119 // Creates a TH1F histogram for the collection
120 //
9bcfd1ab 121
122 if(!fList){
809a4336 123 AliError("No TList pointer ! ");
124 return kFALSE;
125 }
126 else{
9bcfd1ab 127 fList->Add(new TH1F(name, title, nBin, nMin, nMax));
faee3b18 128 if(logAxis >= 0){
129 BinLogAxis(name, logAxis);
130 }
75d81601 131 return CheckObject(name);
809a4336 132 }
133}
c2690925 134
135//___________________________________________________________________
136Bool_t AliHFEcollection::CreateTH1Farray(const char* name, const char* title, Int_t nBin, const Double_t* xbins){
137
138 //
139 // Creates a TH1F histogram for the collection 2nd version
140 //
141
142 if(!fList){
143 AliError("No TList pointer ! ");
144 return kFALSE;
145 }
146 else{
147 fList->Add(new TH1F(name, title, nBin, xbins));
148 return CheckObject(name);
149 }
150}
151
a8ef1999 152//___________________________________________________________________
153Bool_t AliHFEcollection::CreateTH2Farray(const char* name, const char* title, Int_t nBin, const Double_t* xbins, Int_t nBinY, Float_t nMinY, Float_t nMaxY){
154
155 //
156 // Creates a TH1F histogram for the collection 2nd version
157 //
158
159 if(!fList){
160 AliError("No TList pointer ! ");
161 return kFALSE;
162 }
163 else{
164 fList->Add(new TH2F(name, title, nBin, xbins, nBinY, nMinY, nMaxY));
165 return CheckObject(name);
166 }
167}
168
809a4336 169//___________________________________________________________________
faee3b18 170Bool_t AliHFEcollection::CreateTH2F(const char* name, const char* title, Int_t nBinX, Float_t nMinX, Float_t nMaxX, Int_t nBinY, Float_t nMinY, Float_t nMaxY, Int_t logAxis){
9bcfd1ab 171
50685501 172 //
173 // Creates a TH2F histogram for the collection
174 //
9bcfd1ab 175
176 if(!fList){
809a4336 177 AliError("No TList pointer ! ");
178 return kFALSE;
179 }
9bcfd1ab 180 fList->Add(new TH2F(name, title, nBinX, nMinX, nMaxX, nBinY, nMinY, nMaxY));
faee3b18 181 if(logAxis >= 0){
182 BinLogAxis(name, logAxis);
183 }
75d81601 184 return CheckObject(name);
809a4336 185}
186//___________________________________________________________________
faee3b18 187Bool_t AliHFEcollection::CreateTH1Fvector1(Int_t X, const char* name, const char* title, Int_t nBin, Float_t nMin, Float_t nMax, Int_t logAxis){
9bcfd1ab 188
50685501 189 //
75d81601 190 // create a 1 dimensional array of size [X]
50685501 191 //
9bcfd1ab 192
193 if(!fList){
809a4336 194 AliError("No TList pointer ! ");
195 return kFALSE;
196 }
75d81601 197 if(X <=0){
809a4336 198 AliError("can not create array with negative or zero size ");
199 return kFALSE;
200 }
75d81601 201 TString hname;
202 for(Int_t i=0; i<X; ++i){
203 hname = "";
204 hname.Append(Form("%s_[%d]", name, i));
809a4336 205 //cout<<" -D: name: "<<name.str().c_str()<<endl;
206 //cout<<" -D: nBin: "<<_nBin<<" ,Min: "<<_nMin<<" , Max: "<<_nMax<<endl;
faee3b18 207 CreateTH1F(hname.Data(), title, nBin, nMin, nMax, logAxis);
75d81601 208 if(!CheckObject(hname.Data())){
c1bd5735 209 AliError(Form("Not possible to create object: %s", hname.Data()));
809a4336 210 return kFALSE;
211 }
212 }
213 return kTRUE;
214}
215//___________________________________________________________________
faee3b18 216Bool_t AliHFEcollection::CreateTH2Fvector1(Int_t X, const char* name, const char* title, Int_t nBinX, Float_t nMinX, Float_t nMaxX, Int_t nBinY, Float_t nMinY, Float_t nMaxY, Int_t logAxis){
9bcfd1ab 217
50685501 218 //
75d81601 219 // create a 1 dimensinal array of TH2F histograms with size [X]
50685501 220 //
9bcfd1ab 221
222 if(!fList){
809a4336 223 AliError("No TList pointer !");
224 return kFALSE;
225 }
75d81601 226 if(X <=0){
809a4336 227 AliError("can not create array with negative or zero size ");
228 return kFALSE;
229 }
75d81601 230 TString hname;
231 for(Int_t i=0; i<X; ++i){
232 hname = "";
233 hname.Append(Form("%s_[%d]", name, i));
809a4336 234 //cout<<" -D: name: "<<name<<endl;
235 //cout<<" -D: nBin: "<<_nBin<<" ,Min: "<<_nMin<<" , Max: "<<_nMax<<endl;
faee3b18 236 CreateTH2F(hname.Data(), title, nBinX, nMinX, nMaxX, nBinY, nMinY, nMaxY, logAxis);
75d81601 237 if(!CheckObject(hname.Data())){
238 AliError(Form("Not possible to create object: %s", hname.Data()));
809a4336 239 return kFALSE;
240 }
241 }
242 return kTRUE;
243}
244//___________________________________________________________________
faee3b18 245Bool_t AliHFEcollection::CreateTH1Fvector2(Int_t X, Int_t Y, const char* name, const char* title, Int_t nBin, Float_t nMin, Float_t nMax, Int_t logAxis){
9bcfd1ab 246
50685501 247 //
75d81601 248 // create a 2 dimensional array of histograms of size [X, Y]
50685501 249 //
9bcfd1ab 250
251 if(!fList){
809a4336 252 AliError("No TList pointer ! ");
253 return kFALSE;
254 }
75d81601 255 if(X <=0 || Y <=0){
809a4336 256 AliError("can not create array with negative or zero size ");
257 return kFALSE;
258 }
75d81601 259 TString hname;
260 for(Int_t i=0; i<X; ++i){
261 for(Int_t j=0; j<Y; ++j){
262 hname = "";
263 hname.Append(Form("%s_[%d][%d]", name, i, j));
809a4336 264 //cout<<" -D: name: "<<name.str().c_str()<<endl;
265 //cout<<" -D: nBin: "<<_nBin<<" ,Min: "<<_nMin<<" , Max: "<<_nMax<<endl;
faee3b18 266 CreateTH1F(hname.Data(), title, nBin, nMin, nMax, logAxis);
75d81601 267 if(!CheckObject(hname.Data())){
268 AliError(Form("Not possible to create object: %s", hname.Data()));
809a4336 269 return kFALSE;
270 }
271 }
272 }
9bcfd1ab 273 return kTRUE;
274}
275//___________________________________________________________________
8c1c76e9 276Bool_t AliHFEcollection::CreateTH3F(const char* name, const char* title, Int_t nBinX, Float_t nMinX, Float_t nMaxX, Int_t nBinY, Float_t nMinY, Float_t nMaxY, Int_t nBinZ, Float_t nMinZ, Float_t nMaxZ, Int_t logAxis){
277
278 //
279 // Creates a TH2F histogram for the collection
280 //
281
282 if(!fList){
283 AliError("No TList pointer ! ");
284 return kFALSE;
285 }
286 fList->Add(new TH3F(name, title, nBinX, nMinX, nMaxX, nBinY, nMinY, nMaxY, nBinZ, nMinZ, nMaxZ));
287 if(logAxis >= 0){
288 BinLogAxis(name, logAxis);
289 }
290 return CheckObject(name);
291}
292//___________________________________________________________________
9bcfd1ab 293Bool_t AliHFEcollection::CreateProfile(const char* name, const char* title, Int_t nbins, Double_t xmin, Double_t xmax){
294
295 //
296 // create a simple TProfile
297 //
298
299 if(!fList){
300 AliError("No TList pointer ! ");
301 return kFALSE;
302 }
303 fList->Add(new TProfile(name, title, nbins, xmin, xmax));
304 return CheckObject(name);
305
306}
307//___________________________________________________________________
8c1c76e9 308Bool_t AliHFEcollection::CreateTHnSparse(const char* name, const char* title, Int_t dim, const Int_t* nbins, const Double_t* xmin, const Double_t* xmax){
9bcfd1ab 309
310 //
311 // create 'dim' dimensional THnSparse
312 //
313
314 if(!fList){
315 AliError("No TList pointer ! ");
316 return kFALSE;
317 }
318 fList->Add(new THnSparseF(name, title, dim, nbins, xmin, xmax));
319 return CheckObject(name);
320
321}
a8ef1999 322//___________________________________________________________________
323Bool_t AliHFEcollection::CreateTHnSparseNoLimits(const char* name, const char* title, Int_t dim, const Int_t* nbins){
324
325 //
326 // create 'dim' dimensional THnSparse without limits
327 //
328
329 if(!fList){
330 AliError("No TList pointer ! ");
331 return kFALSE;
332 }
333 fList->Add(new THnSparseF(name, title, dim, nbins));
334 return CheckObject(name);
335
336}
9bcfd1ab 337//___________________________________________________________________
338TObject* AliHFEcollection::Get(const char* name){
339
340 //
341 // Get histogram with the required name
342 //
809a4336 343
9bcfd1ab 344
345 if(!CheckObject(name)){
70da6c5a 346 AliWarning(Form("Not possible to return pointer to the object '%s'\n", name));
9bcfd1ab 347 return 0;
348 }
349
350 return fList->FindObject(name);
809a4336 351
352}
353//___________________________________________________________________
9bcfd1ab 354Bool_t AliHFEcollection::Fill(const char* name, Double_t v){
355
50685501 356 //
9bcfd1ab 357 // fill function for one TH1 histograms
50685501 358 //
9bcfd1ab 359
faee3b18 360 if(!CheckObject(name)){
361 AliError(Form("Not possible to fill the object '%s', the object does not exist\n", name));
9bcfd1ab 362 return kFALSE;
809a4336 363 }
9bcfd1ab 364
69ac0e6f 365 TH1 *htmp = dynamic_cast<TH1F*>(fList->FindObject(name));
9bcfd1ab 366 // chack the possible object types
69ac0e6f 367 if(htmp){
368 htmp->Fill(v);
9bcfd1ab 369 return kTRUE;
370 }
371
372 return kFALSE;
373
374}
375//___________________________________________________________________
faee3b18 376Bool_t AliHFEcollection::Fill(const char* name, Int_t v){
377
378 //
379 // fill function for one TH1 histograms for integer numbers
380 //
381
382 return Fill(name, v*1.0);
383}
384//___________________________________________________________________
9bcfd1ab 385Bool_t AliHFEcollection::Fill(const char* name, Int_t X, Double_t v){
386
387 //
388 // fill function for one dimension arrays of TH1
389 //
390
391 const char* n = Form("%s_[%d]", name, X);
392 TObject *o = Get(n);
393 if(!o){
394 return kFALSE;
809a4336 395 }
9bcfd1ab 396 Fill(o->GetName(), v);
397 return kTRUE;
809a4336 398}
399//___________________________________________________________________
9bcfd1ab 400Bool_t AliHFEcollection::Fill(const char* name, Int_t X, Int_t Y, Double_t v){
70da6c5a 401
402 //
403 // Fill function fir 2 dimensional TH1 arrays
404 //
9bcfd1ab 405
406 const char* n = Form("%s_[%d][%d]", name, X, Y);
407 TObject *o = Get(n);
408 if(!o){
409 return kFALSE;
410 }
411 Fill(o->GetName(), v);
412 return kTRUE;
413}
414//___________________________________________________________________
415Bool_t AliHFEcollection::Fill(const char* name, Int_t X, Double_t v1, Double_t v2){
416
50685501 417 //
9bcfd1ab 418 // fill function for one dimension array of TH2
50685501 419 //
9bcfd1ab 420
421 const char* n = Form("%s_[%d]", name, X);
422 TObject *o = Get(n);
423 if(!o){
424 return kFALSE;
809a4336 425 }
9bcfd1ab 426 Fill(o->GetName(), v1, v2);
427
428 return kTRUE;
429}
430//___________________________________________________________________
431Bool_t AliHFEcollection::Fill(const char* name, Double_t v1, Double_t v2){
432
433 //
434 // fill function for TH2 objects
435 //
436
faee3b18 437 if(!CheckObject(name)){
438 AliError(Form("Not possible to fill the object '%s', the object does not exist\n", name));
9bcfd1ab 439 return kFALSE;
809a4336 440 }
9bcfd1ab 441
442 // chack the possible object types
443 if(fList->FindObject(name)->InheritsFrom("TH2")){
69ac0e6f 444 TH2 *h2 = dynamic_cast<TH2F*>(fList->FindObject(name));
445 if(h2) h2->Fill(v1, v2);
9bcfd1ab 446 return kTRUE;
447 }
448 if(fList->FindObject(name)->InheritsFrom("TProfile")){
bf892a6a 449 TProfile *pr = dynamic_cast<TProfile*>(fList->FindObject(name));
450 if(pr) pr->Fill(v1, v2);
9bcfd1ab 451 return kTRUE;
452 }
453
454 return kFALSE;
455
8c1c76e9 456}
457//___________________________________________________________________
458Bool_t AliHFEcollection::Fill(const char* name, Double_t v1, Double_t v2, Double_t v3){
459
460 //
461 // fill function for TH3 objects
462 //
463
464 if(!CheckObject(name)){
465 AliError(Form("Not possible to fill the object '%s', the object does not exist\n", name));
466 return kFALSE;
467 }
468
469 // chack the possible object types
470 TH3 *h3 = dynamic_cast<TH3F*>(fList->FindObject(name));
471 if(h3){
472 h3->Fill(v1, v2, v3);
473 return kTRUE;
474 }
475
476 return kFALSE;
477
809a4336 478}
faee3b18 479//___________________________________________________________________
480Bool_t AliHFEcollection::Fill(const char* name, Double_t* entry, Double_t weight){
481 //
482 // Fill a THnSparse object
483 //
484
485 if(!CheckObject(name)){
486 AliError(Form("Not possible to fill the object '%s', the object does not exist\n", name));
487 return kFALSE;
488 }
489
69ac0e6f 490 THnSparseF *htmp = dynamic_cast<THnSparseF*>(fList->FindObject(name));
491 if(htmp){
492 htmp->Fill(entry, weight);
faee3b18 493 return kTRUE;
494 }
495 return kFALSE;
9bcfd1ab 496
faee3b18 497}
809a4336 498//___________________________________________________________________
75d81601 499Bool_t AliHFEcollection::CheckObject(const char* name){
9bcfd1ab 500
50685501 501 //
809a4336 502 // check wheter the creation of the histogram was succesfull
50685501 503 //
809a4336 504
9bcfd1ab 505 if(!fList){
809a4336 506 AliError("No TList pointer ! ");
507 return kFALSE;
508 }
509
9bcfd1ab 510 if(!fList->FindObject(name)){
70da6c5a 511 AliWarning(Form("Creating or Finding the object '%s' failed\n", name));
809a4336 512 return kFALSE;
513 }
514 return kTRUE;
515}
516//___________________________________________________________________
70da6c5a 517Bool_t AliHFEcollection::Sumw2(const char* name){
518 //
519 // Set Sumw2 for the given object
520 //
521 if(!CheckObject(name)){
522 return kFALSE;
523 }
524
525 TObject *o = Get(name);
69ac0e6f 526 THnSparse *htmp = dynamic_cast<THnSparse*>(o);
527 if(htmp){
528 htmp->Sumw2();
70da6c5a 529 }
530 return kTRUE;
531}
532//___________________________________________________________________
9bcfd1ab 533Bool_t AliHFEcollection::BinLogAxis(const char* name, Int_t dim){
534
50685501 535 //
9bcfd1ab 536 // converts the axis (defined by the dimension) of THx or THnSparse
537 // object to Log scale. Number of bins and bin min and bin max are preserved
538 //
539
540
541 if(!CheckObject(name)){
542 return kFALSE;
543 }
544
545 TObject *o = Get(name);
69ac0e6f 546 TAxis *axis = NULL;
8c1c76e9 547 TString type(o->IsA()->GetName());
548 if(type.Contains("TH1")){ // 1D histogram
69ac0e6f 549 TH1 *h1 = dynamic_cast<TH1F*>(o);
550 if(h1) axis = h1->GetXaxis();
8c1c76e9 551 } else if(type.Contains("TH2")){
69ac0e6f 552 TH2 *h2 = dynamic_cast<TH2F*>(o);
553 if(h2){
554 if(0 == dim){
555 axis = h2->GetXaxis();
556 }
557 else if(1 == dim){
558 axis = h2->GetYaxis();
559 }
560 else{
561 AliError("Only dim = 0 or 1 possible for TH2F");
562 }
9bcfd1ab 563 }
8c1c76e9 564 } else if(type.Contains("TH3")){
565 TH3 *h3 = dynamic_cast<TH3F*>(o);
566 if(h3){
567 if(0 == dim){
568 axis = h3->GetXaxis();
569 }
570 else if(1 == dim){
571 axis = h3->GetYaxis();
572 }
573 else if(2 == dim){
574 axis = h3->GetZaxis();
575 }
576 else{
577 AliError("Only dim = 0, 1 or 2 possible for TH3F");
578 }
579 }
580 } else if(type.Contains("THnSparse")){
69ac0e6f 581 THnSparse *hs = dynamic_cast<THnSparse*>(o);
582 if(hs) axis = hs->GetAxis(dim);
9bcfd1ab 583 }
8c1c76e9 584
9bcfd1ab 585 if(!axis){
586 AliError(Form("Axis '%d' could not be identified in the object '%s'\n", dim, name));
587 return kFALSE;
588 }
589
590 Int_t bins = axis->GetNbins();
591
592 Double_t from = axis->GetXmin();
faee3b18 593 if(from <= 0){
594 AliError(Form(" Log binning not possible for object '%s'because the '%d' axis starts from '%f\n'", name, dim, from));
595 return kFALSE;
596 }
9bcfd1ab 597 Double_t to = axis->GetXmax();
598 Double_t *newBins = new Double_t[bins+1];
599 newBins[0] = from;
600 Double_t factor = TMath::Power(to/from, 1./bins);
601 for(Int_t i=1; i<=bins; ++i){
602 newBins[i] = factor * newBins[i-1];
603 }
604 axis->Set(bins, newBins);
69ac0e6f 605 delete[] newBins;
9bcfd1ab 606
607 return kTRUE;
608
609
809a4336 610}
611//___________________________________________________________________
8c1c76e9 612Long64_t AliHFEcollection::Merge(const TCollection *list){
9bcfd1ab 613
50685501 614 //
615 // Merge the collections
616 //
70da6c5a 617 if(!list)
809a4336 618 return 0;
70da6c5a 619 if(list->IsEmpty())
620 return 1;
809a4336 621
bf892a6a 622 TIter it(list);
70da6c5a 623 TObject *o = NULL;
624 Int_t index = 0;
bf892a6a 625 TList templist; // Create temporary list containing all the lists to merge
626 while((o = it())){
70da6c5a 627 AliHFEcollection *coll = dynamic_cast<AliHFEcollection *>(o);
628 if(!coll) continue;
70da6c5a 629 templist.Add(coll->fList);
70da6c5a 630 index++;
631 }
bf892a6a 632 fList->Merge(&templist);
70da6c5a 633 return index + 1;
809a4336 634}
635//____________________________________________________________________
636void AliHFEcollection::Browse(TBrowser *b)
637{
9bcfd1ab 638
50685501 639 //
640 // Browse the content of the directory.
641 //
809a4336 642
643 if (b) {
644 TObject *obj = 0;
9bcfd1ab 645 TIter nextin(fList);
809a4336 646
647 //Add objects that are only in memory
648 while ((obj = nextin())) {
649 b->Add(obj, obj->GetName());
650 }
651 }
652}
e156c3bb 653//____________________________________________________________________
654void AliHFEcollection::Print(Option_t *) const{
655 //
656 // Print content of the collection
657 //
658 TIter histIter(fList);
659 TObject *o = NULL;
660 Int_t nHistos = 0;
661 printf("Collection %s\n", GetName());
662 printf("Content of the collection:\n=========================================\n");
663 while((o = histIter())){
664 printf("Histo %s, Type %s\n", o->GetName(), o->IsA()->GetName());
665 nHistos++;
666 }
667 printf("Number of histos in the collection: %d\n", nHistos);
668 printf("\n");
669}
670