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