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