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