]> git.uio.no Git - u/mrichter/AliRoot.git/blame - PWG3/hfe/AliHFEcollection.cxx
extra check on pointer
[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 <TList.h>
30#include <TString.h>
31#include <TBrowser.h>
9bcfd1ab 32#include <TMath.h>
809a4336 33
34#include "AliLog.h"
35#include "AliHFEcollection.h"
36
37using namespace std;
38
39
40ClassImp(AliHFEcollection)
41
42//___________________________________________________________________
43AliHFEcollection::AliHFEcollection():
44 TNamed()
9bcfd1ab 45 , fList(0x0)
809a4336 46{
9bcfd1ab 47
809a4336 48 //
49 // default constructor
50 //
51
9bcfd1ab 52 fList = new TList();
53 if(!fList){
809a4336 54 AliError("Initialization of the list failed");
55 }
56 else{
57 // list is owner of the objects. Once list is deleted, the objects
58 // it contains will be deleted too
9bcfd1ab 59 fList->SetOwner(kTRUE);
809a4336 60 }
61 //Printf("%s:%d,%p",(char*)__FILE__,__LINE__,fInstance);
62
63}
64//___________________________________________________________________
65AliHFEcollection::AliHFEcollection(char* name, char* title):
66 TNamed(name, title)
9bcfd1ab 67 , fList(0x0)
809a4336 68{
69
70 //
71 // constructor
72 //
73
9bcfd1ab 74 fList = new TList();
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
9bcfd1ab 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
9bcfd1ab 117 target.fList = fList;
809a4336 118}
119//___________________________________________________________________
120AliHFEcollection::~AliHFEcollection(){
9bcfd1ab 121
50685501 122 //
123 // Destructor
124 //
9bcfd1ab 125
809a4336 126 AliInfo("DESTRUCTOR");
127}
128//___________________________________________________________________
75d81601 129Bool_t AliHFEcollection::CreateTH1F(const char* name, const char* title, Int_t nBin, Float_t nMin, Float_t nMax){
9bcfd1ab 130
50685501 131 //
132 // Creates a TH1F histogram for the collection
133 //
9bcfd1ab 134
135 if(!fList){
809a4336 136 AliError("No TList pointer ! ");
137 return kFALSE;
138 }
139 else{
9bcfd1ab 140 fList->Add(new TH1F(name, title, nBin, nMin, nMax));
75d81601 141 return CheckObject(name);
809a4336 142 }
143}
144//___________________________________________________________________
75d81601 145Bool_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 146
50685501 147 //
148 // Creates a TH2F histogram for the collection
149 //
9bcfd1ab 150
151 if(!fList){
809a4336 152 AliError("No TList pointer ! ");
153 return kFALSE;
154 }
9bcfd1ab 155 fList->Add(new TH2F(name, title, nBinX, nMinX, nMaxX, nBinY, nMinY, nMaxY));
75d81601 156 return CheckObject(name);
809a4336 157}
158//___________________________________________________________________
75d81601 159Bool_t AliHFEcollection::CreateTH1Fvector1(Int_t X, const char* name, const char* title, Int_t nBin, Float_t nMin, Float_t nMax){
9bcfd1ab 160
50685501 161 //
75d81601 162 // create a 1 dimensional array of size [X]
50685501 163 //
9bcfd1ab 164
165 if(!fList){
809a4336 166 AliError("No TList pointer ! ");
167 return kFALSE;
168 }
75d81601 169 if(X <=0){
809a4336 170 AliError("can not create array with negative or zero size ");
171 return kFALSE;
172 }
75d81601 173 TString hname;
174 for(Int_t i=0; i<X; ++i){
175 hname = "";
176 hname.Append(Form("%s_[%d]", name, i));
809a4336 177 //cout<<" -D: name: "<<name.str().c_str()<<endl;
178 //cout<<" -D: nBin: "<<_nBin<<" ,Min: "<<_nMin<<" , Max: "<<_nMax<<endl;
75d81601 179 CreateTH1F(hname.Data(), title, nBin, nMin, nMax);
180 if(!CheckObject(hname.Data())){
181 AliError(Form("Not possible to create object: ", hname.Data()));
809a4336 182 return kFALSE;
183 }
184 }
185 return kTRUE;
186}
187//___________________________________________________________________
75d81601 188Bool_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 189
50685501 190 //
75d81601 191 // create a 1 dimensinal array of TH2F histograms with size [X]
50685501 192 //
9bcfd1ab 193
194 if(!fList){
809a4336 195 AliError("No TList pointer !");
196 return kFALSE;
197 }
75d81601 198 if(X <=0){
809a4336 199 AliError("can not create array with negative or zero size ");
200 return kFALSE;
201 }
75d81601 202 TString hname;
203 for(Int_t i=0; i<X; ++i){
204 hname = "";
205 hname.Append(Form("%s_[%d]", name, i));
809a4336 206 //cout<<" -D: name: "<<name<<endl;
207 //cout<<" -D: nBin: "<<_nBin<<" ,Min: "<<_nMin<<" , Max: "<<_nMax<<endl;
75d81601 208 CreateTH2F(hname.Data(), title, nBinX, nMinX, nMaxX, nBinY, nMinY, nMaxY);
209 if(!CheckObject(hname.Data())){
210 AliError(Form("Not possible to create object: %s", hname.Data()));
809a4336 211 return kFALSE;
212 }
213 }
214 return kTRUE;
215}
216//___________________________________________________________________
75d81601 217Bool_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 218
50685501 219 //
75d81601 220 // create a 2 dimensional array of histograms of size [X, Y]
50685501 221 //
9bcfd1ab 222
223 if(!fList){
809a4336 224 AliError("No TList pointer ! ");
225 return kFALSE;
226 }
75d81601 227 if(X <=0 || Y <=0){
809a4336 228 AliError("can not create array with negative or zero size ");
229 return kFALSE;
230 }
75d81601 231 TString hname;
232 for(Int_t i=0; i<X; ++i){
233 for(Int_t j=0; j<Y; ++j){
234 hname = "";
235 hname.Append(Form("%s_[%d][%d]", name, i, j));
809a4336 236 //cout<<" -D: name: "<<name.str().c_str()<<endl;
237 //cout<<" -D: nBin: "<<_nBin<<" ,Min: "<<_nMin<<" , Max: "<<_nMax<<endl;
75d81601 238 CreateTH1F(hname.Data(), title, nBin, nMin, nMax);
239 if(!CheckObject(hname.Data())){
240 AliError(Form("Not possible to create object: %s", hname.Data()));
809a4336 241 return kFALSE;
242 }
243 }
244 }
9bcfd1ab 245 return kTRUE;
246}
247//___________________________________________________________________
248Bool_t AliHFEcollection::CreateProfile(const char* name, const char* title, Int_t nbins, Double_t xmin, Double_t xmax){
249
250 //
251 // create a simple TProfile
252 //
253
254 if(!fList){
255 AliError("No TList pointer ! ");
256 return kFALSE;
257 }
258 fList->Add(new TProfile(name, title, nbins, xmin, xmax));
259 return CheckObject(name);
260
261}
262//___________________________________________________________________
263Bool_t AliHFEcollection::CreateTHnSparse(const char* name, const char* title, Int_t dim, Int_t* nbins, Double_t* xmin, Double_t* xmax){
264
265 //
266 // create 'dim' dimensional THnSparse
267 //
268
269 if(!fList){
270 AliError("No TList pointer ! ");
271 return kFALSE;
272 }
273 fList->Add(new THnSparseF(name, title, dim, nbins, xmin, xmax));
274 return CheckObject(name);
275
276}
277//___________________________________________________________________
278TObject* AliHFEcollection::Get(const char* name){
279
280 //
281 // Get histogram with the required name
282 //
809a4336 283
9bcfd1ab 284
285 if(!CheckObject(name)){
286 AliError(Form("Not possible to return pointer to the object '%s'\n", name));
287 return 0;
288 }
289
290 return fList->FindObject(name);
809a4336 291
292}
293//___________________________________________________________________
9bcfd1ab 294Bool_t AliHFEcollection::Fill(const char* name, Double_t v){
295
50685501 296 //
9bcfd1ab 297 // fill function for one TH1 histograms
50685501 298 //
9bcfd1ab 299
300 if(!CheckObject(name)){
301 AliError(Form("Not possible to return pointer to the object '%s'\n", name));
302 return kFALSE;
809a4336 303 }
9bcfd1ab 304
305 // chack the possible object types
306 if(fList->FindObject(name)->InheritsFrom("TH1")){
307 (dynamic_cast<TH1F*>(fList->FindObject(name)))->Fill(v);
308 return kTRUE;
309 }
310
311 return kFALSE;
312
313}
314//___________________________________________________________________
315Bool_t AliHFEcollection::Fill(const char* name, Int_t X, Double_t v){
316
317 //
318 // fill function for one dimension arrays of TH1
319 //
320
321 const char* n = Form("%s_[%d]", name, X);
322 TObject *o = Get(n);
323 if(!o){
324 return kFALSE;
809a4336 325 }
9bcfd1ab 326 Fill(o->GetName(), v);
327 return kTRUE;
809a4336 328}
329//___________________________________________________________________
9bcfd1ab 330Bool_t AliHFEcollection::Fill(const char* name, Int_t X, Int_t Y, Double_t v){
331
332 const char* n = Form("%s_[%d][%d]", name, X, Y);
333 TObject *o = Get(n);
334 if(!o){
335 return kFALSE;
336 }
337 Fill(o->GetName(), v);
338 return kTRUE;
339}
340//___________________________________________________________________
341Bool_t AliHFEcollection::Fill(const char* name, Int_t X, Double_t v1, Double_t v2){
342
50685501 343 //
9bcfd1ab 344 // fill function for one dimension array of TH2
50685501 345 //
9bcfd1ab 346
347 const char* n = Form("%s_[%d]", name, X);
348 TObject *o = Get(n);
349 if(!o){
350 return kFALSE;
809a4336 351 }
9bcfd1ab 352 Fill(o->GetName(), v1, v2);
353
354 return kTRUE;
355}
356//___________________________________________________________________
357Bool_t AliHFEcollection::Fill(const char* name, Double_t v1, Double_t v2){
358
359 //
360 // fill function for TH2 objects
361 //
362
363 if(!CheckObject(name)){
364 AliError(Form("Not possible to return pointer to the object '%s'\n", name));
365 return kFALSE;
809a4336 366 }
9bcfd1ab 367
368 // chack the possible object types
369 if(fList->FindObject(name)->InheritsFrom("TH2")){
370 (dynamic_cast<TH2F*>(fList->FindObject(name)))->Fill(v1, v2);
371 return kTRUE;
372 }
373 if(fList->FindObject(name)->InheritsFrom("TProfile")){
374 (dynamic_cast<TProfile*>(fList->FindObject(name)))->Fill(v1, v2);
375 return kTRUE;
376 }
377
378 return kFALSE;
379
809a4336 380}
9bcfd1ab 381
809a4336 382//___________________________________________________________________
75d81601 383Bool_t AliHFEcollection::CheckObject(const char* name){
9bcfd1ab 384
50685501 385 //
809a4336 386 // check wheter the creation of the histogram was succesfull
50685501 387 //
809a4336 388
9bcfd1ab 389 if(!fList){
809a4336 390 AliError("No TList pointer ! ");
391 return kFALSE;
392 }
393
9bcfd1ab 394 if(!fList->FindObject(name)){
395 AliError(Form("Creating or Finding the object '%s' failed\n", name));
809a4336 396 return kFALSE;
397 }
398 return kTRUE;
399}
400//___________________________________________________________________
9bcfd1ab 401Bool_t AliHFEcollection::BinLogAxis(const char* name, Int_t dim){
402
50685501 403 //
9bcfd1ab 404 // converts the axis (defined by the dimension) of THx or THnSparse
405 // object to Log scale. Number of bins and bin min and bin max are preserved
406 //
407
408
409 if(!CheckObject(name)){
410 return kFALSE;
411 }
412
413 TObject *o = Get(name);
414 TAxis *axis = 0x0;
415 if(o->InheritsFrom("TH1")){
416 axis = (dynamic_cast<TH1F*>(o))->GetXaxis();
417 }
418 if(o->InheritsFrom("TH2")){
419 if(0 == dim){
420 axis = (dynamic_cast<TH2F*>(o))->GetXaxis();
421 }
422 else if(1 == dim){
423 axis = (dynamic_cast<TH2F*>(o))->GetYaxis();
424 }
425 else{
426 AliError("Only dim = 0 or 1 possible for TH2F");
427 }
428 }
429 if(o->InheritsFrom("THnSparse")){
430 axis = (dynamic_cast<THnSparse*>(o))->GetAxis(dim);
431 }
432
433 if(!axis){
434 AliError(Form("Axis '%d' could not be identified in the object '%s'\n", dim, name));
435 return kFALSE;
436 }
437
438 Int_t bins = axis->GetNbins();
439
440 Double_t from = axis->GetXmin();
441 Double_t to = axis->GetXmax();
442 Double_t *newBins = new Double_t[bins+1];
443 newBins[0] = from;
444 Double_t factor = TMath::Power(to/from, 1./bins);
445 for(Int_t i=1; i<=bins; ++i){
446 newBins[i] = factor * newBins[i-1];
447 }
448 axis->Set(bins, newBins);
449 delete newBins;
450
451 return kTRUE;
452
453
809a4336 454}
455//___________________________________________________________________
456Long64_t AliHFEcollection::Merge(TCollection *list){
9bcfd1ab 457
50685501 458 //
459 // Merge the collections
460 //
9bcfd1ab 461
462 if(!fList){
809a4336 463 AliError("AliHFEcollection::Merge : No TList pointer ! ");
464 return 0;
465 }
466
9bcfd1ab 467 return fList->Merge(list);
809a4336 468
469}
470//____________________________________________________________________
471void AliHFEcollection::Browse(TBrowser *b)
472{
9bcfd1ab 473
50685501 474 //
475 // Browse the content of the directory.
476 //
809a4336 477
478 if (b) {
479 TObject *obj = 0;
9bcfd1ab 480 TIter nextin(fList);
809a4336 481
482 //Add objects that are only in memory
483 while ((obj = nextin())) {
484 b->Add(obj, obj->GetName());
485 }
486 }
487}