]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG3/hfe/AliHFEcollection.cxx
Minor fix for software triggers.
[u/mrichter/AliRoot.git] / PWG3 / hfe / AliHFEcollection.cxx
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 **************************************************************************/
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 //
22
23 //#include <iostream>
24
25 #include <TH1F.h>
26 #include <TH2F.h>
27 #include <THnSparse.h>
28 #include <TProfile.h>
29 #include <TString.h>
30 #include <TBrowser.h>
31 #include <TMath.h>
32
33 #include "AliLog.h"
34 #include "AliHFEcollection.h"
35
36 using namespace std;
37
38
39 ClassImp(AliHFEcollection)
40
41 //___________________________________________________________________
42 AliHFEcollection::AliHFEcollection():
43   TNamed()
44   , fList(NULL)
45 {
46
47   //
48   // default constructor
49   //
50
51   fList = new THashList();
52   if(!fList){
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
58     //fList->SetOwner(kTRUE);
59   }
60   //Printf("%s:%d,%p",(char*)__FILE__,__LINE__,fInstance);
61   
62 }
63 //___________________________________________________________________
64 AliHFEcollection::AliHFEcollection(const char* name, const char* title):
65   TNamed(name, title)
66   , fList(NULL)
67 {
68  
69   //
70   // constructor
71   //
72  
73   fList = new THashList();
74   fList->SetName(Form("list_%s", name));
75   if(!fList){
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
81     // fList->SetOwner(kTRUE);
82   }
83 }
84 //___________________________________________________________________
85 AliHFEcollection::AliHFEcollection(const AliHFEcollection &c) :
86   TNamed(c)
87   , fList(NULL)
88 {
89
90   //
91   // copy operator
92   // 
93   
94   c.Copy(*this);
95 }
96 //___________________________________________________________________
97 AliHFEcollection &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 //___________________________________________________________________
109 void AliHFEcollection::Copy(TObject &ref) const {
110
111   //
112   // Performs the copying of the object
113   //
114
115   AliHFEcollection &target = dynamic_cast<AliHFEcollection &>(ref);
116
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());
121 }
122 //___________________________________________________________________
123 AliHFEcollection::~AliHFEcollection(){
124
125   //
126   // Destructor
127   //
128   if(fList)
129     fList->Delete();
130   delete fList;
131   AliInfo("DESTRUCTOR");
132 }
133 //___________________________________________________________________
134 Bool_t AliHFEcollection::CreateTH1F(const char* name, const char* title, Int_t nBin, Float_t nMin, Float_t nMax, Int_t logAxis){
135
136   //
137   // Creates a TH1F histogram for the collection
138   //
139
140   if(!fList){
141     AliError("No TList pointer ! ");
142     return kFALSE;
143   }
144   else{
145     fList->Add(new TH1F(name, title, nBin, nMin, nMax));
146     if(logAxis >= 0){
147       BinLogAxis(name, logAxis);
148     }
149     return CheckObject(name);
150   }
151 }
152 //___________________________________________________________________
153 Bool_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){
154
155   //
156   // Creates a TH2F histogram for the collection
157   //
158
159   if(!fList){
160     AliError("No TList pointer ! ");
161     return kFALSE;
162   }
163   fList->Add(new TH2F(name, title, nBinX, nMinX, nMaxX, nBinY, nMinY, nMaxY));
164   if(logAxis >= 0){
165     BinLogAxis(name, logAxis);
166   }
167   return CheckObject(name); 
168 }
169 //___________________________________________________________________
170 Bool_t AliHFEcollection::CreateTH1Fvector1(Int_t X, const char* name, const char* title, Int_t nBin, Float_t nMin, Float_t nMax, Int_t logAxis){
171
172   //
173   // create a 1 dimensional array of size [X]
174   //
175
176   if(!fList){
177     AliError("No TList pointer ! ");
178     return kFALSE;
179   }
180   if(X <=0){
181     AliError("can not create array with negative or zero size ");
182     return kFALSE;
183   }
184   TString hname;
185   for(Int_t i=0; i<X; ++i){
186     hname = "";
187     hname.Append(Form("%s_[%d]", name, i));
188     //cout<<" -D: name: "<<name.str().c_str()<<endl;
189     //cout<<" -D: nBin: "<<_nBin<<" ,Min: "<<_nMin<<" , Max: "<<_nMax<<endl;
190     CreateTH1F(hname.Data(), title, nBin, nMin, nMax, logAxis);
191     if(!CheckObject(hname.Data())){
192       AliError(Form("Not possible to create object: %s", hname.Data()));
193       return kFALSE;
194     }    
195   }
196   return kTRUE;  
197 }
198 //___________________________________________________________________
199 Bool_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){
200
201   //
202   // create a 1 dimensinal array of TH2F histograms with size [X]
203   //
204
205   if(!fList){
206     AliError("No TList pointer !");
207     return kFALSE;
208   }
209   if(X <=0){
210     AliError("can not create array with negative or zero size ");
211     return kFALSE;
212   }
213   TString hname;
214   for(Int_t i=0; i<X; ++i){
215     hname = "";
216     hname.Append(Form("%s_[%d]", name, i));
217     //cout<<" -D: name: "<<name<<endl;
218     //cout<<" -D: nBin: "<<_nBin<<" ,Min: "<<_nMin<<" , Max: "<<_nMax<<endl;
219     CreateTH2F(hname.Data(), title, nBinX, nMinX, nMaxX, nBinY, nMinY, nMaxY, logAxis);
220     if(!CheckObject(hname.Data())){
221       AliError(Form("Not possible to create object: %s", hname.Data()));
222       return kFALSE;
223     }    
224   }
225   return kTRUE;  
226 }
227 //___________________________________________________________________
228 Bool_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){
229
230   //
231   // create a 2 dimensional array of histograms of size [X, Y]
232   //
233
234   if(!fList){
235     AliError("No TList pointer ! ");
236     return kFALSE;
237   }
238   if(X <=0 || Y <=0){
239     AliError("can not create array with negative or zero size ");
240     return kFALSE;
241   }
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));
247       //cout<<" -D: name: "<<name.str().c_str()<<endl;
248       //cout<<" -D: nBin: "<<_nBin<<" ,Min: "<<_nMin<<" , Max: "<<_nMax<<endl;
249       CreateTH1F(hname.Data(), title, nBin, nMin, nMax, logAxis);
250       if(!CheckObject(hname.Data())){
251               AliError(Form("Not possible to create object: %s", hname.Data()));
252               return kFALSE;
253       }
254     }
255   }
256   return kTRUE;  
257 }
258 //___________________________________________________________________
259 Bool_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 //___________________________________________________________________
274 Bool_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 //___________________________________________________________________
289 TObject* AliHFEcollection::Get(const char* name){ 
290
291   //
292   // Get histogram with the required name
293   // 
294   
295
296   if(!CheckObject(name)){
297     AliWarning(Form("Not possible to return pointer to the object '%s'\n", name));
298     return 0;
299   }
300
301   return fList->FindObject(name);
302   
303 }
304 //___________________________________________________________________
305 Bool_t AliHFEcollection::Fill(const char* name, Double_t v){
306
307   //
308   // fill function for one TH1 histograms
309   //
310
311   if(!CheckObject(name)){
312     AliError(Form("Not possible to fill the object '%s', the object does not exist\n", name));
313     return kFALSE;
314   }
315
316   TH1 *htmp = dynamic_cast<TH1F*>(fList->FindObject(name));
317   // chack the possible object types
318   if(htmp){
319     htmp->Fill(v);
320     return kTRUE;
321   }
322   
323   return kFALSE;
324
325 }
326 //___________________________________________________________________
327 Bool_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 //___________________________________________________________________
336 Bool_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;
346   }
347   Fill(o->GetName(), v);
348   return kTRUE;
349 }
350 //___________________________________________________________________
351 Bool_t AliHFEcollection::Fill(const char* name, Int_t X, Int_t Y, Double_t v){
352
353   //
354   // Fill function fir 2 dimensional TH1 arrays
355   //
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 //___________________________________________________________________
366 Bool_t AliHFEcollection::Fill(const char* name, Int_t X, Double_t v1, Double_t v2){
367
368   //
369   // fill function for one dimension array of TH2
370   //
371
372   const char* n = Form("%s_[%d]", name, X);
373   TObject *o = Get(n);
374   if(!o){
375     return kFALSE;
376   }
377   Fill(o->GetName(), v1, v2);
378   
379   return kTRUE;
380 }
381 //___________________________________________________________________
382 Bool_t AliHFEcollection::Fill(const char* name, Double_t v1, Double_t v2){
383
384   //
385   // fill function for TH2 objects
386   //
387
388   if(!CheckObject(name)){
389     AliError(Form("Not possible to fill the object '%s', the object does not exist\n", name));
390     return kFALSE;
391   }
392
393   // chack the possible object types
394   if(fList->FindObject(name)->InheritsFrom("TH2")){
395     TH2 *h2 = dynamic_cast<TH2F*>(fList->FindObject(name));
396     if(h2) h2->Fill(v1, v2);
397     return kTRUE;
398   }  
399   if(fList->FindObject(name)->InheritsFrom("TProfile")){
400     (dynamic_cast<TProfile*>(fList->FindObject(name)))->Fill(v1, v2);
401     return kTRUE;
402   }  
403   
404   return kFALSE;
405   
406 }
407 //___________________________________________________________________
408 Bool_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   
418   THnSparseF *htmp = dynamic_cast<THnSparseF*>(fList->FindObject(name));
419    if(htmp){
420      htmp->Fill(entry, weight);
421      return kTRUE;
422    }
423    return kFALSE;
424
425 }
426 //___________________________________________________________________
427 Bool_t AliHFEcollection::CheckObject(const char* name){
428
429   //
430   // check wheter the creation of the histogram was succesfull
431   //
432   
433   if(!fList){
434     AliError("No TList pointer ! ");
435     return kFALSE;
436   }
437   
438   if(!fList->FindObject(name)){
439     AliWarning(Form("Creating or Finding the object '%s' failed\n", name));
440     return kFALSE;
441   }
442   return kTRUE;
443 }
444 //___________________________________________________________________
445 Bool_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);
454   THnSparse *htmp = dynamic_cast<THnSparse*>(o);
455   if(htmp){
456     htmp->Sumw2();
457   }
458   return kTRUE;
459 }
460 //___________________________________________________________________
461 Bool_t AliHFEcollection::BinLogAxis(const char* name, Int_t dim){
462
463   // 
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);
474   TAxis *axis = NULL;
475   if(o->InheritsFrom("TH1")){
476     TH1 *h1 = dynamic_cast<TH1F*>(o);
477     if(h1) axis = h1->GetXaxis();
478   }
479   if(o->InheritsFrom("TH2")){
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       }
491     }
492   }
493   if(o->InheritsFrom("THnSparse")){
494     THnSparse *hs = dynamic_cast<THnSparse*>(o);
495     if(hs) axis = hs->GetAxis(dim);
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();
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   }
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);
518   delete[] newBins;
519
520   return kTRUE;
521
522
523 }
524 //___________________________________________________________________
525 Long64_t AliHFEcollection::Merge(TCollection *list){
526
527   //
528   // Merge the collections
529   //
530   if(!list)
531     return 0;
532   if(list->IsEmpty())
533     return 1;
534   
535   TIterator *iter = list->MakeIterator();
536   TObject *o = NULL;
537   Int_t index = 0;
538   while((o = iter->Next())){
539     AliHFEcollection *coll = dynamic_cast<AliHFEcollection *>(o);
540     if(!coll) continue; 
541     TList templist;       // Create temporary list containing all the lists to merge
542     templist.Add(coll->fList);
543     fList->Merge(&templist);
544     index++;
545   }
546   delete iter;
547   return index + 1;
548 }
549 //____________________________________________________________________
550 void AliHFEcollection::Browse(TBrowser *b)
551 {
552
553   //
554   // Browse the content of the directory.
555   //
556
557    if (b) {
558       TObject *obj = 0;
559       TIter nextin(fList);
560
561       //Add objects that are only in memory
562       while ((obj = nextin())) {
563          b->Add(obj, obj->GetName());
564       }
565    }
566 }