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