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