]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGHF/hfe/AliHFEcontainer.cxx
add EMCal trigger in eh analysis. add hists. in Raa study
[u/mrichter/AliRoot.git] / PWGHF / 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   delete containers;
393   return cmerged;
394 }
395
396 //__________________________________________________________________
397 void AliHFEcontainer::SetStepTitle(const Char_t *contname, const Char_t *steptitle, UInt_t step){
398   //
399   // Set title for given analysis step in container with name contname
400   //
401   AliCFContainer *cont = GetCFContainer(contname);
402   if(!cont) return;
403   if(step >= static_cast<UInt_t>(cont->GetNStep())) return;
404   cont->SetStepTitle(step, steptitle);
405 }
406
407 //__________________________________________________________________
408 void AliHFEcontainer::MakeLinearBinning(UInt_t var, UInt_t nBins, Double_t begin, Double_t end){
409   //
410   // Set Linear binning for the given container
411   //
412   AliHFEvarInfo *myvar = dynamic_cast<AliHFEvarInfo *>(fVariables->UncheckedAt(var));
413   if(myvar) myvar->SetBinning(nBins, AliHFEtools::MakeLinearBinning(nBins, begin, end));
414 }
415
416 //__________________________________________________________________
417 void AliHFEcontainer::MakeLogarithmicBinning(UInt_t var, UInt_t nBins, Double_t begin, Double_t end){
418   //
419   // Set Logarithmic binning for the given container
420   //
421   AliHFEvarInfo *myvar = dynamic_cast<AliHFEvarInfo *>(fVariables->UncheckedAt(var));
422   if(myvar) myvar->SetBinning(nBins, AliHFEtools::MakeLogarithmicBinning(nBins, begin, end));
423 }
424
425 //__________________________________________________________________
426 void AliHFEcontainer::MakeUserDefinedBinning(UInt_t var, UInt_t nBins, const Double_t *binning){
427   //
428   // Set User defined binning
429   //
430   AliHFEvarInfo *myvar = dynamic_cast<AliHFEvarInfo *>(fVariables->UncheckedAt(var));
431   if(myvar) myvar->SetBinning(nBins, binning);
432 }
433
434 //__________________________________________________________________
435 void AliHFEcontainer::SetVariableName(UInt_t var, const Char_t *varname){
436   //
437   // Variable name
438   // 
439   AliHFEvarInfo *myvar = dynamic_cast<AliHFEvarInfo *>(fVariables->UncheckedAt(var));
440   if(myvar) myvar->SetVarName(varname);
441 }
442
443 //__________________________________________________________________
444 Int_t AliHFEcontainer::GetNumberOfCFContainers() const{
445   //
446   // Get the number of entries
447   //
448   return fContainers->GetEntries();
449 }
450
451 //__________________________________________________________________
452 void AliHFEcontainer::Sumw2(const char *contname) const {
453   // 
454   // Set sums of weights for all steps of a given container
455   //
456   AliCFContainer *cont = GetCFContainer(contname);
457   if(cont){
458     for(Int_t istep = 0; istep < cont->GetNStep(); istep++)
459       cont->GetGrid(istep)->SumW2();
460   }
461 }
462
463 //__________________________________________________________________
464 void AliHFEcontainer::Print(const Option_t *)const{
465   //
466   // Print Container Status
467   //
468   std::cout << "Container status: " << std::endl;
469   std::cout << "=====================================================\n";
470   std::cout << "Number of variables: " << fNVars << std::endl;
471   if(fNVars){
472     UInt_t nVars = fVariables ? fVariables->GetEntriesFast() : 0;
473     if(nVars != fNVars)
474       std::cout << "Inconsistency in number of Variables [" << fNVars << "|" << nVars << "]" << std::endl;
475     AliHFEvarInfo *var = NULL;
476     if(fVariables){
477       for(UInt_t ivar = 0; ivar < fNVars; ivar++){
478         var = dynamic_cast<AliHFEvarInfo *>(fVariables->UncheckedAt(ivar));
479         if(var)
480           std::cout << "Variable " << ivar << ": Name: " << var->GetVarName()->Data() << ", Number of Bins: " << var->GetNumberOfBins() << std::endl;
481       }
482     }
483   }
484   std::cout << std::endl;
485
486   // Print CF Containers:
487   if(fContainers){
488     std::cout << "Containers[" << fContainers->GetEntries() << "]: "<< std::endl;
489     std::cout << "=====================================================\n";
490     for(Int_t icont = 0; icont < fContainers->GetEntries(); icont++){
491       AliCFContainer *c = dynamic_cast<AliCFContainer *>(fContainers->At(icont));
492       if(c){
493         std::cout << "Name: " << c->GetName() << ", Title: "  << c->GetTitle() << std::endl;
494         for(Int_t istep = 0; istep < c->GetNStep(); istep++)
495           std::cout << "Step " << istep << ": Title " << c->GetStepTitle(istep) << std::endl;
496       }
497       std::cout << "------------------------------------------------------\n";
498     }
499   }
500   std::cout << "Number of Events: " << fNEvents << std::endl;
501 }
502
503 //------------------------------------ Content of class AliHFEvarInfo -----------------------------------
504 //__________________________________________________________________
505 AliHFEcontainer::AliHFEvarInfo::AliHFEvarInfo():
506   TObject(),
507   fVarName(NULL),
508   fBinning(NULL)
509 {
510   // Default constructor
511   fBinning = new TArrayD;
512   fVarName = new TString;
513 }
514
515 //__________________________________________________________________
516 AliHFEcontainer::AliHFEvarInfo::AliHFEvarInfo(const Char_t *name):
517   TObject(),
518   fVarName(NULL),
519   fBinning(NULL)
520 {
521   fBinning = new TArrayD;
522   fVarName = new TString(name);
523 }
524
525 //__________________________________________________________________
526 AliHFEcontainer::AliHFEvarInfo::AliHFEvarInfo(const AliHFEvarInfo &ref):
527   TObject(ref),
528   fVarName(NULL),
529   fBinning(NULL)
530 {
531   //
532   // Constructor
533   //
534   fVarName = new TString(*(ref.fVarName));
535   fBinning = new TArrayD(*(ref.fBinning));
536 }
537
538 //__________________________________________________________________
539 AliHFEcontainer::AliHFEvarInfo &AliHFEcontainer::AliHFEvarInfo::operator=(const AliHFEvarInfo &ref){
540   //
541   // Assignment operator
542   //
543   TObject::operator=(ref);
544   *fVarName = *(ref.fVarName);
545   *fBinning = *(ref.fBinning);
546   return *this;
547 }
548
549 //__________________________________________________________________
550 AliHFEcontainer::AliHFEvarInfo::~AliHFEvarInfo(){
551   //
552   // Destructor
553   //
554   delete fVarName;
555   delete fBinning;
556 }
557
558 //__________________________________________________________________
559 void AliHFEcontainer::AliHFEvarInfo::SetVarName(const Char_t *name){
560   //
561   // Setter for var name
562   //
563   *fVarName = name;
564 }
565
566 //__________________________________________________________________
567 void AliHFEcontainer::AliHFEvarInfo::SetBinning(UInt_t nBins, const Double_t *content){
568   // Setter for binning
569   //
570   fBinning->Set(nBins + 1, content);
571 }
572