]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG3/hfe/AliHFEcontainer.cxx
Fixes to run w/o OCDB infotrmation (in that case the class only provides statistics...
[u/mrichter/AliRoot.git] / PWG3 / hfe / AliHFEcontainer.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 // HFE correction framework container
17 // Contains many single containers
18 // Extra fuctionality like appending added
19 //
20 // Author:
21 //   Markus Fasel <M.Fasel@gsi.de>
22 //
23 #include <iostream>
24 #include <TAxis.h>
25 #include <TClass.h>
26 #include <TCollection.h>
27 #include <THashList.h>
28 #include <THnSparse.h>
29 #include <TList.h>
30 #include <TObjArray.h>
31 #include <TObjString.h>
32 #include <TString.h>
33
34 #include "AliCFContainer.h"
35 #include "AliHFEcontainer.h"
36 #include "AliHFEtools.h"
37
38 ClassImp(AliHFEcontainer)
39 ClassImp(AliHFEcontainer::AliHFEvarInfo)
40
41 //__________________________________________________________________
42 AliHFEcontainer::AliHFEcontainer():
43   TNamed("HFEcontainer", ""),
44   fContainers(NULL),
45   fCorrelationMatrices(NULL),
46   fVariables(NULL),
47   fNVars(0),
48   fNEvents(0)
49 {
50   //
51   // Default constructor
52   //
53   fContainers = new THashList();
54   fContainers->SetOwner();
55 }
56
57 //__________________________________________________________________
58 AliHFEcontainer::AliHFEcontainer(const Char_t *name):
59   TNamed(name, ""),
60   fContainers(NULL),
61   fCorrelationMatrices(NULL),
62   fVariables(NULL),
63   fNVars(0),
64   fNEvents(0)
65 {
66   //
67   // Default constructor
68   //
69   fContainers = new THashList();
70   fContainers->SetOwner();
71 }
72
73 //__________________________________________________________________
74 AliHFEcontainer::AliHFEcontainer(const Char_t *name, UInt_t nVar):
75   TNamed(name, ""),
76   fContainers(NULL),
77   fCorrelationMatrices(NULL),
78   fVariables(NULL),
79   fNVars(0),
80   fNEvents(0)
81 {
82   //
83   // Constructor
84   // Setting Number of Variables too
85   //
86   fContainers = new THashList();
87   fContainers->SetOwner();
88   SetNumberOfVariables(nVar);
89 }
90
91 //__________________________________________________________________
92 AliHFEcontainer::AliHFEcontainer(const AliHFEcontainer &ref):
93   TNamed(ref),
94   fContainers(NULL),
95   fCorrelationMatrices(NULL),
96   fVariables(NULL),
97   fNVars(ref.fNVars),
98   fNEvents(ref.fNEvents)
99 {
100   //
101   // Copy constructor
102   // creates a new object with new (empty) containers
103   //
104   if(fNVars){
105     fVariables = new TObjArray(fNVars);
106     AliHFEvarInfo *vtmp = NULL;
107     for(UInt_t ivar = 0; ivar < fNVars; ivar++){
108       vtmp = dynamic_cast<AliHFEvarInfo *>(ref.fVariables->UncheckedAt(ivar));
109       if(vtmp) fVariables->AddAt(new AliHFEvarInfo(*vtmp), ivar);
110     }
111   }
112   fContainers = new THashList;
113   fContainers->SetOwner();
114   AliCFContainer *ctmp = NULL;
115   for(Int_t ien = 0; ien < ref.fContainers->GetEntries(); ien++){
116     ctmp = dynamic_cast<AliCFContainer *>(ref.fContainers->At(ien));
117     if(ctmp) CreateContainer(ctmp->GetName(), ctmp->GetTitle(), ctmp->GetNStep());
118   }
119   // Copy also correlation matrices
120   if(ref.fCorrelationMatrices){
121     THnSparseF *htmp = NULL;
122     fCorrelationMatrices = new THashList;
123     fCorrelationMatrices->SetOwner();
124     for(Int_t ien = 0; ien < ref.fCorrelationMatrices->GetEntries(); ien++){
125       htmp = dynamic_cast<THnSparseF *>(ref.fCorrelationMatrices->At(ien));
126       if(htmp) CreateCorrelationMatrix(htmp->GetName(), htmp->GetTitle());
127     }
128   }
129 }
130
131 //__________________________________________________________________
132 AliHFEcontainer &AliHFEcontainer::operator=(const AliHFEcontainer &ref){
133   //
134   // Assignment operator
135   // Cleanup old object, create a new one with new containers inside
136   //
137   this->~AliHFEcontainer(); // cleanup old object before creating the new onwe
138   TNamed::operator=(ref);
139   fContainers = new THashList();
140   fNVars = ref.fNVars;
141   AliCFContainer *ctmp = NULL;
142   for(Int_t ien = 0; ien < ref.fContainers->GetEntries(); ien++){
143     ctmp = dynamic_cast<AliCFContainer *>(ref.fContainers->At(ien));
144     fContainers->Add(new AliCFContainer(*ctmp));
145   }
146   if(fNVars){
147     fVariables = new TObjArray(fNVars);
148     AliHFEvarInfo *vtmp = NULL;
149     for(UInt_t ivar = 0; ivar < fNVars; ivar++){
150       vtmp = dynamic_cast<AliHFEvarInfo *>(ref.fVariables->UncheckedAt(ivar));
151       if(vtmp) fVariables->AddAt(new AliHFEvarInfo(*vtmp), ivar);
152     }
153   } else {
154     fVariables = NULL;
155   }
156   // Copy also correlation matrices
157   if(ref.fCorrelationMatrices){
158     THnSparseF *htmp = NULL;
159     fCorrelationMatrices = new THashList;
160     fCorrelationMatrices->SetOwner();
161     for(Int_t ien = 0; ien < ref.fCorrelationMatrices->GetEntries(); ien++){
162       htmp = dynamic_cast<THnSparseF *>(ref.fCorrelationMatrices->At(ien));
163       if(htmp) CreateCorrelationMatrix(htmp->GetName(), htmp->GetTitle());
164     }
165   }
166   return *this;
167 }
168
169 //__________________________________________________________________
170 AliHFEcontainer::~AliHFEcontainer(){
171   //
172   // Destructor
173   //
174   delete fContainers;
175   if(fCorrelationMatrices) delete fCorrelationMatrices;
176   if(fVariables){
177     fVariables->Delete();
178     delete fVariables;
179   }
180 }
181
182 //__________________________________________________________________
183 Long64_t AliHFEcontainer::Merge(TCollection *coll){
184   //
185   // Merge Container
186   //
187   if(!coll)
188     return 0;
189   if(coll->IsEmpty())
190     return 1;
191
192   TIterator *iter = coll->MakeIterator();
193   TObject *o = NULL;
194   Long64_t count = 0;
195   while((o = iter->Next())){
196     AliHFEcontainer *cont = dynamic_cast<AliHFEcontainer *>(o);
197     if(!cont) continue;
198
199     // Merge the two TObjArrays
200     TList containers;
201     containers.Add(cont->fContainers);
202     fContainers->Merge(&containers);
203
204     if(fCorrelationMatrices && cont->fCorrelationMatrices){
205       containers.Clear();
206       containers.Add(cont->fCorrelationMatrices);
207       fCorrelationMatrices->Merge(&containers);
208     }
209
210     fNEvents += cont->GetNumberOfEvents();
211     count++;
212   }
213   return count + 1;
214 }
215
216 //__________________________________________________________________
217 void AliHFEcontainer::SetNumberOfVariables(UInt_t nVar){
218   //
219   // Define the number of variables 
220   // Initialize containers for the variable informations
221   //
222   if(fNVars) return;
223
224   fNVars = nVar;
225   fVariables = new TObjArray(nVar);
226   for(UInt_t ivar = 0; ivar < nVar; ivar++)
227     fVariables->AddAt(new AliHFEvarInfo, ivar);
228 }
229
230 //__________________________________________________________________
231 void AliHFEcontainer::CreateContainer(const Char_t *name, const Char_t *title, UInt_t nStep){
232   //
233   // Create a new Correction Framework Container and store it 
234   //
235   if(fContainers->FindObject(name)){
236     AliError(Form("Container %s already exists. Cannot replace it!", name));
237     return;
238   }
239   
240   Int_t *nBins = new Int_t[fNVars];
241   AliHFEvarInfo *var = NULL;
242   for(UInt_t ivar = 0; ivar < fNVars; ivar++){ 
243     var = dynamic_cast<AliHFEvarInfo *>(fVariables->UncheckedAt(ivar));
244     nBins[ivar] = var ? var->GetNumberOfBins() : 0;
245   }
246   AliCFContainer *cont = new AliCFContainer(name, title, nStep, fNVars, nBins);
247   for(UInt_t ivar = 0; ivar < fNVars; ivar++){
248     var = dynamic_cast<AliHFEvarInfo *>(fVariables->UncheckedAt(ivar));
249     if(var){
250       cont->SetBinLimits(ivar, var->GetBinning());
251       cont->SetVarTitle(ivar, var->GetVarName()->Data());
252     }
253   }
254   delete[] nBins;
255   fContainers->Add(cont);
256   AliInfo(Form("Container %s created with %d cut steps", name, nStep));
257 }
258
259 //__________________________________________________________________
260 void AliHFEcontainer::CreateCorrelationMatrix(const Char_t *name, const Char_t *title){
261   //
262   // Create Correlation Matrix
263   //
264   if(!fCorrelationMatrices){
265     fCorrelationMatrices = new THashList;
266     fCorrelationMatrices->SetName("fCorrelationMatrices");
267     fCorrelationMatrices->SetOwner();
268   }
269
270   Int_t *nBins = new Int_t[2*fNVars];
271   AliHFEvarInfo *var = NULL;
272   for(UInt_t ivar = 0; ivar < fNVars; ivar++){
273     var = dynamic_cast<AliHFEvarInfo *>(fVariables->UncheckedAt(ivar));
274     if(var){
275       nBins[ivar] = var->GetNumberOfBins();
276       nBins[ivar+fNVars] = var->GetNumberOfBins();
277     }
278   }
279
280   THnSparseF * hTmp = new THnSparseF(name, title, 2*fNVars, nBins);
281   for(UInt_t ivar = 0; ivar < fNVars; ivar++){
282     var = dynamic_cast<AliHFEvarInfo *>(fVariables->UncheckedAt(ivar));
283     if(var){
284       hTmp->SetBinEdges(ivar,var->GetBinning());
285       //hTmp->GetAxis(ivar)->Set(var->GetNumberOfBins(), var->GetBinning());
286       hTmp->GetAxis(ivar)->SetTitle(var->GetVarName()->Data());
287       //hTmp->GetAxis(ivar + fNVars)->Set(var->GetNumberOfBins(), var->GetBinning());
288       hTmp->GetAxis(ivar + fNVars)->SetTitle(Form("%s_{MC}", var->GetVarName()->Data()));
289       hTmp->SetBinEdges(ivar+fNVars,var->GetBinning());
290     }
291   }
292   hTmp->Sumw2();
293   fCorrelationMatrices->AddLast(hTmp);
294 }
295
296 //__________________________________________________________________
297 AliCFContainer *AliHFEcontainer::GetCFContainer(const Char_t *name) const{
298   //
299   // Find a given container 
300   //
301   return dynamic_cast<AliCFContainer *>(fContainers->FindObject(name));
302 }
303
304 //__________________________________________________________________
305 THnSparseF *AliHFEcontainer::GetCorrelationMatrix(const Char_t *name) const{
306   //
307   // Find Correlation Matrix
308   //
309   if(fCorrelationMatrices) return dynamic_cast<THnSparseF *>(fCorrelationMatrices->FindObject(name));
310   else return 0x0;
311
312 }
313
314 //__________________________________________________________________
315 void AliHFEcontainer::FillCFContainer(const Char_t *name, UInt_t step, Double_t *content, Double_t weight) const {
316   //
317   // Fill container
318   //
319   AliCFContainer *cont = GetCFContainer(name);
320   if(!cont) return;
321   cont->Fill(content, step, weight);
322 }
323
324 //__________________________________________________________________
325 void AliHFEcontainer::FillCFContainerStepname(const Char_t *name, const Char_t *steptitle, Double_t *content, Double_t weight)const{
326   //
327   // Fill container
328   //
329   AliCFContainer *cont = GetCFContainer(name);
330   if(!cont) return;
331   // find the matching step title
332   Int_t mystep = -1;
333   for(Int_t istep = 0; istep < cont->GetNStep(); istep++){
334     TString tstept = cont->GetStepTitle(istep);
335     if(!tstept.CompareTo(steptitle)){
336       mystep = istep;
337       break;
338     }
339   }
340   if(mystep < 0){
341     // step not found
342     AliDebug(1, Form("Step %s not found in container %s", steptitle, name));
343     return;
344   }
345   AliDebug(1, Form("Filling step %s(%d) for container %s", steptitle, mystep, name));
346   cont->Fill(content, mystep, weight);
347 }
348
349 //__________________________________________________________________
350 AliCFContainer *AliHFEcontainer::MakeMergedCFContainer(const Char_t *name, const Char_t *title, const Char_t* contnames) const {
351   //
352   // Merge CF Container out of several containers 
353   // Container names are separated by :
354   // returns a new object which has to be taken care of by the user
355   //
356
357   TObjArray *containers = TString(contnames).Tokenize(":");
358   // we first need the size of the container to be merged
359   Int_t nStepMerged = 0;
360   AliCFContainer *ctemp = NULL;
361   TObjString *cname = NULL;
362   for(Int_t icont = 0; icont < containers->GetEntries(); icont++){
363     cname = dynamic_cast<TObjString *>(containers->At(icont));
364     ctemp = dynamic_cast<AliCFContainer *>(fContainers->FindObject(cname->String().Data()));
365     if(!ctemp){
366       AliWarning(Form("Container %s not found. It will be unprocessed", cname->String().Data()));
367       continue;
368     }
369     nStepMerged += ctemp->GetNStep(); 
370   }
371   AliInfo("Please Ignore the messgae comming from AliCFContainer!");
372   Int_t *dummyBinning = new Int_t[fNVars];
373   for(UInt_t ibin = 0; ibin < fNVars; ibin++) dummyBinning[ibin] = 1;
374   AliCFContainer *cmerged = new AliCFContainer(name, title, nStepMerged, fNVars, dummyBinning);
375   delete[] dummyBinning;
376   // Fill container with content
377   AliInfo("Filling new container");
378   Int_t cstep = 0;
379   for(Int_t icont = 0; icont < containers->GetEntries(); icont++){
380     cname = dynamic_cast<TObjString *>(containers->At(icont));
381     ctemp = dynamic_cast<AliCFContainer *>(fContainers->FindObject(cname->String().Data()));
382     if(!ctemp) continue;
383     for(Int_t istep = 0; istep < ctemp->GetNStep(); istep++)
384       cmerged->SetGrid(cstep++, new AliCFGridSparse(*ctemp->GetGrid(istep)));
385   }
386   return cmerged;
387 }
388
389 //__________________________________________________________________
390 void AliHFEcontainer::SetStepTitle(const Char_t *contname, const Char_t *steptitle, UInt_t step){
391   //
392   // Set title for given analysis step in container with name contname
393   //
394   AliCFContainer *cont = GetCFContainer(contname);
395   if(!cont) return;
396   if(step >= static_cast<UInt_t>(cont->GetNStep())) return;
397   cont->SetStepTitle(step, steptitle);
398 }
399
400 //__________________________________________________________________
401 void AliHFEcontainer::MakeLinearBinning(UInt_t var, UInt_t nBins, Double_t begin, Double_t end){
402   //
403   // Set Linear binning for the given container
404   //
405   AliHFEvarInfo *myvar = dynamic_cast<AliHFEvarInfo *>(fVariables->UncheckedAt(var));
406   if(myvar) myvar->SetBinning(nBins, AliHFEtools::MakeLinearBinning(nBins, begin, end));
407 }
408
409 //__________________________________________________________________
410 void AliHFEcontainer::MakeLogarithmicBinning(UInt_t var, UInt_t nBins, Double_t begin, Double_t end){
411   //
412   // Set Logarithmic binning for the given container
413   //
414   AliHFEvarInfo *myvar = dynamic_cast<AliHFEvarInfo *>(fVariables->UncheckedAt(var));
415   if(myvar) myvar->SetBinning(nBins, AliHFEtools::MakeLogarithmicBinning(nBins, begin, end));
416 }
417
418 //__________________________________________________________________
419 void AliHFEcontainer::SetVariableName(UInt_t var, const Char_t *varname){
420   //
421   // Variable name
422   // 
423   AliHFEvarInfo *myvar = dynamic_cast<AliHFEvarInfo *>(fVariables->UncheckedAt(var));
424   if(myvar) myvar->SetVarName(varname);
425 }
426
427 //__________________________________________________________________
428 Int_t AliHFEcontainer::GetNumberOfCFContainers() const{
429   //
430   // Get the number of entries
431   //
432   return fContainers->GetEntries();
433 }
434
435 //__________________________________________________________________
436 void AliHFEcontainer::Print(const Option_t *)const{
437   //
438   // Print Container Status
439   //
440   std::cout << "Container status: " << std::endl;
441   std::cout << "=====================================================\n";
442   std::cout << "Number of variables: " << fNVars << std::endl;
443   if(fNVars){
444     UInt_t nVars = fVariables ? fVariables->GetEntriesFast() : 0;
445     if(nVars != fNVars)
446       std::cout << "Inconsistency in number of Variables [" << fNVars << "|" << nVars << "]" << std::endl;
447     AliHFEvarInfo *var = NULL;
448     if(fVariables){
449       for(UInt_t ivar = 0; ivar < fNVars; ivar++){
450         var = dynamic_cast<AliHFEvarInfo *>(fVariables->UncheckedAt(ivar));
451         if(var)
452           std::cout << "Variable " << ivar << ": Name: " << var->GetVarName()->Data() << ", Number of Bins: " << var->GetNumberOfBins() << std::endl;
453       }
454     }
455   }
456   std::cout << std::endl;
457
458   // Print CF Containers:
459   if(fContainers){
460     std::cout << "Containers[" << fContainers->GetEntries() << "]: "<< std::endl;
461     std::cout << "=====================================================\n";
462     for(Int_t icont = 0; icont < fContainers->GetEntries(); icont++){
463       AliCFContainer *c = dynamic_cast<AliCFContainer *>(fContainers->At(icont));
464       if(c){
465         std::cout << "Name: " << c->GetName() << ", Title: "  << c->GetTitle() << std::endl;
466         for(Int_t istep = 0; istep < c->GetNStep(); istep++)
467           std::cout << "Step " << istep << ": Title " << c->GetStepTitle(istep) << std::endl;
468       }
469       std::cout << "------------------------------------------------------\n";
470     }
471   }
472   std::cout << "Number of Events: " << fNEvents << std::endl;
473 }
474
475 //------------------------------------ Content of class AliHFEvarInfo -----------------------------------
476 //__________________________________________________________________
477 AliHFEcontainer::AliHFEvarInfo::AliHFEvarInfo():
478   TObject(),
479   fVarName(NULL),
480   fBinning(NULL)
481 {
482   // Default constructor
483   fBinning = new TArrayD;
484   fVarName = new TString;
485 }
486
487 //__________________________________________________________________
488 AliHFEcontainer::AliHFEvarInfo::AliHFEvarInfo(const Char_t *name):
489   TObject(),
490   fVarName(NULL),
491   fBinning(NULL)
492 {
493   fBinning = new TArrayD;
494   fVarName = new TString(name);
495 }
496
497 //__________________________________________________________________
498 AliHFEcontainer::AliHFEvarInfo::AliHFEvarInfo(const AliHFEvarInfo &ref):
499   TObject(ref),
500   fVarName(NULL),
501   fBinning(NULL)
502 {
503   //
504   // Constructor
505   //
506   fVarName = new TString(*(ref.fVarName));
507   fBinning = new TArrayD(*(ref.fBinning));
508 }
509
510 //__________________________________________________________________
511 AliHFEcontainer::AliHFEvarInfo &AliHFEcontainer::AliHFEvarInfo::operator=(const AliHFEvarInfo &ref){
512   //
513   // Assignment operator
514   //
515   TObject::operator=(ref);
516   *fVarName = *(ref.fVarName);
517   *fBinning = *(ref.fBinning);
518   return *this;
519 }
520
521 //__________________________________________________________________
522 AliHFEcontainer::AliHFEvarInfo::~AliHFEvarInfo(){
523   //
524   // Destructor
525   //
526   delete fVarName;
527   delete fBinning;
528 }
529
530 //__________________________________________________________________
531 void AliHFEcontainer::AliHFEvarInfo::SetVarName(const Char_t *name){
532   //
533   // Setter for var name
534   //
535   *fVarName = name;
536 }
537
538 //__________________________________________________________________
539 void AliHFEcontainer::AliHFEvarInfo::SetBinning(UInt_t nBins, Double_t *content){
540   // Setter for binning
541   //
542   fBinning->Set(nBins + 1, content);
543 }
544