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