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