1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
16 // HFE correction framework container
17 // Contains many single containers
18 // Extra fuctionality like appending added
21 // Markus Fasel <M.Fasel@gsi.de>
26 #include <TCollection.h>
27 #include <THashList.h>
28 #include <THnSparse.h>
30 #include <TObjArray.h>
31 #include <TObjString.h>
34 #include "AliCFContainer.h"
35 #include "AliHFEcontainer.h"
36 #include "AliHFEtools.h"
38 ClassImp(AliHFEcontainer)
39 ClassImp(AliHFEcontainer::AliHFEvarInfo)
41 //__________________________________________________________________
42 AliHFEcontainer::AliHFEcontainer():
43 TNamed("HFEcontainer", ""),
45 fCorrelationMatrices(NULL),
51 // Default constructor
53 fContainers = new THashList();
54 fContainers->SetOwner();
57 //__________________________________________________________________
58 AliHFEcontainer::AliHFEcontainer(const Char_t *name):
61 fCorrelationMatrices(NULL),
67 // Default constructor
69 fContainers = new THashList();
70 fContainers->SetOwner();
73 //__________________________________________________________________
74 AliHFEcontainer::AliHFEcontainer(const Char_t *name, UInt_t nVar):
77 fCorrelationMatrices(NULL),
84 // Setting Number of Variables too
86 fContainers = new THashList();
87 fContainers->SetOwner();
88 SetNumberOfVariables(nVar);
91 //__________________________________________________________________
92 AliHFEcontainer::AliHFEcontainer(const AliHFEcontainer &ref):
95 fCorrelationMatrices(NULL),
98 fNEvents(ref.fNEvents)
102 // creates a new object with new (empty) containers
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);
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());
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());
131 //__________________________________________________________________
132 AliHFEcontainer &AliHFEcontainer::operator=(const AliHFEcontainer &ref){
134 // Assignment operator
135 // Cleanup old object, create a new one with new containers inside
137 this->~AliHFEcontainer(); // cleanup old object before creating the new onwe
138 TNamed::operator=(ref);
139 fContainers = new THashList();
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));
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);
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());
169 //__________________________________________________________________
170 AliHFEcontainer::~AliHFEcontainer(){
175 if(fCorrelationMatrices) delete fCorrelationMatrices;
177 fVariables->Delete();
182 //__________________________________________________________________
183 Long64_t AliHFEcontainer::Merge(TCollection *coll){
192 TIterator *iter = coll->MakeIterator();
195 while((o = iter->Next())){
196 AliHFEcontainer *cont = dynamic_cast<AliHFEcontainer *>(o);
199 // Merge the two TObjArrays
201 containers.Add(cont->fContainers);
202 fContainers->Merge(&containers);
204 if(fCorrelationMatrices && cont->fCorrelationMatrices){
206 containers.Add(cont->fCorrelationMatrices);
207 fCorrelationMatrices->Merge(&containers);
210 fNEvents += cont->GetNumberOfEvents();
216 //__________________________________________________________________
217 void AliHFEcontainer::SetNumberOfVariables(UInt_t nVar){
219 // Define the number of variables
220 // Initialize containers for the variable informations
225 fVariables = new TObjArray(nVar);
226 for(UInt_t ivar = 0; ivar < nVar; ivar++)
227 fVariables->AddAt(new AliHFEvarInfo, ivar);
230 //__________________________________________________________________
231 void AliHFEcontainer::CreateContainer(const Char_t *name, const Char_t *title, UInt_t nStep){
233 // Create a new Correction Framework Container and store it
235 if(fContainers->FindObject(name)){
236 AliError(Form("Container %s already exists. Cannot replace it!", name));
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;
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));
250 cont->SetBinLimits(ivar, var->GetBinning());
251 cont->SetVarTitle(ivar, var->GetVarName()->Data());
255 fContainers->Add(cont);
256 AliInfo(Form("Container %s created with %d cut steps", name, nStep));
259 //__________________________________________________________________
260 void AliHFEcontainer::CreateCorrelationMatrix(const Char_t *name, const Char_t *title){
262 // Create Correlation Matrix
264 if(!fCorrelationMatrices){
265 fCorrelationMatrices = new THashList;
266 fCorrelationMatrices->SetName("fCorrelationMatrices");
267 fCorrelationMatrices->SetOwner();
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));
275 nBins[ivar] = var->GetNumberOfBins();
276 nBins[ivar+fNVars] = var->GetNumberOfBins();
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));
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());
293 fCorrelationMatrices->AddLast(hTmp);
296 //__________________________________________________________________
297 AliCFContainer *AliHFEcontainer::GetCFContainer(const Char_t *name) const{
299 // Find a given container
301 return dynamic_cast<AliCFContainer *>(fContainers->FindObject(name));
304 //__________________________________________________________________
305 THnSparseF *AliHFEcontainer::GetCorrelationMatrix(const Char_t *name) const{
307 // Find Correlation Matrix
309 if(fCorrelationMatrices) return dynamic_cast<THnSparseF *>(fCorrelationMatrices->FindObject(name));
314 //__________________________________________________________________
315 void AliHFEcontainer::FillCFContainer(const Char_t *name, UInt_t step, Double_t *content, Double_t weight) const {
319 AliCFContainer *cont = GetCFContainer(name);
321 cont->Fill(content, step, weight);
324 //__________________________________________________________________
325 void AliHFEcontainer::FillCFContainerStepname(const Char_t *name, const Char_t *steptitle, Double_t *content, Double_t weight)const{
329 AliCFContainer *cont = GetCFContainer(name);
331 // find the matching step title
333 for(Int_t istep = 0; istep < cont->GetNStep(); istep++){
334 TString tstept = cont->GetStepTitle(istep);
335 if(!tstept.CompareTo(steptitle)){
342 AliDebug(1, Form("Step %s not found in container %s", steptitle, name));
345 AliDebug(1, Form("Filling step %s(%d) for container %s", steptitle, mystep, name));
346 cont->Fill(content, mystep, weight);
349 //__________________________________________________________________
350 AliCFContainer *AliHFEcontainer::MakeMergedCFContainer(const Char_t *name, const Char_t *title, const Char_t* contnames) const {
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
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()));
366 AliWarning(Form("Container %s not found. It will be unprocessed", cname->String().Data()));
369 nStepMerged += ctemp->GetNStep();
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");
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()));
383 for(Int_t istep = 0; istep < ctemp->GetNStep(); istep++)
384 cmerged->SetGrid(cstep++, new AliCFGridSparse(*ctemp->GetGrid(istep)));
389 //__________________________________________________________________
390 void AliHFEcontainer::SetStepTitle(const Char_t *contname, const Char_t *steptitle, UInt_t step){
392 // Set title for given analysis step in container with name contname
394 AliCFContainer *cont = GetCFContainer(contname);
396 if(step >= static_cast<UInt_t>(cont->GetNStep())) return;
397 cont->SetStepTitle(step, steptitle);
400 //__________________________________________________________________
401 void AliHFEcontainer::MakeLinearBinning(UInt_t var, UInt_t nBins, Double_t begin, Double_t end){
403 // Set Linear binning for the given container
405 AliHFEvarInfo *myvar = dynamic_cast<AliHFEvarInfo *>(fVariables->UncheckedAt(var));
406 if(myvar) myvar->SetBinning(nBins, AliHFEtools::MakeLinearBinning(nBins, begin, end));
409 //__________________________________________________________________
410 void AliHFEcontainer::MakeLogarithmicBinning(UInt_t var, UInt_t nBins, Double_t begin, Double_t end){
412 // Set Logarithmic binning for the given container
414 AliHFEvarInfo *myvar = dynamic_cast<AliHFEvarInfo *>(fVariables->UncheckedAt(var));
415 if(myvar) myvar->SetBinning(nBins, AliHFEtools::MakeLogarithmicBinning(nBins, begin, end));
418 //__________________________________________________________________
419 void AliHFEcontainer::SetVariableName(UInt_t var, const Char_t *varname){
423 AliHFEvarInfo *myvar = dynamic_cast<AliHFEvarInfo *>(fVariables->UncheckedAt(var));
424 if(myvar) myvar->SetVarName(varname);
427 //__________________________________________________________________
428 Int_t AliHFEcontainer::GetNumberOfCFContainers() const{
430 // Get the number of entries
432 return fContainers->GetEntries();
435 //__________________________________________________________________
436 void AliHFEcontainer::Print(const Option_t *)const{
438 // Print Container Status
440 std::cout << "Container status: " << std::endl;
441 std::cout << "=====================================================\n";
442 std::cout << "Number of variables: " << fNVars << std::endl;
444 UInt_t nVars = fVariables ? fVariables->GetEntriesFast() : 0;
446 std::cout << "Inconsistency in number of Variables [" << fNVars << "|" << nVars << "]" << std::endl;
447 AliHFEvarInfo *var = NULL;
449 for(UInt_t ivar = 0; ivar < fNVars; ivar++){
450 var = dynamic_cast<AliHFEvarInfo *>(fVariables->UncheckedAt(ivar));
452 std::cout << "Variable " << ivar << ": Name: " << var->GetVarName()->Data() << ", Number of Bins: " << var->GetNumberOfBins() << std::endl;
456 std::cout << std::endl;
458 // Print CF Containers:
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));
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;
469 std::cout << "------------------------------------------------------\n";
472 std::cout << "Number of Events: " << fNEvents << std::endl;
475 //------------------------------------ Content of class AliHFEvarInfo -----------------------------------
476 //__________________________________________________________________
477 AliHFEcontainer::AliHFEvarInfo::AliHFEvarInfo():
482 // Default constructor
483 fBinning = new TArrayD;
484 fVarName = new TString;
487 //__________________________________________________________________
488 AliHFEcontainer::AliHFEvarInfo::AliHFEvarInfo(const Char_t *name):
493 fBinning = new TArrayD;
494 fVarName = new TString(name);
497 //__________________________________________________________________
498 AliHFEcontainer::AliHFEvarInfo::AliHFEvarInfo(const AliHFEvarInfo &ref):
506 fVarName = new TString(*(ref.fVarName));
507 fBinning = new TArrayD(*(ref.fBinning));
510 //__________________________________________________________________
511 AliHFEcontainer::AliHFEvarInfo &AliHFEcontainer::AliHFEvarInfo::operator=(const AliHFEvarInfo &ref){
513 // Assignment operator
515 TObject::operator=(ref);
516 *fVarName = *(ref.fVarName);
517 *fBinning = *(ref.fBinning);
521 //__________________________________________________________________
522 AliHFEcontainer::AliHFEvarInfo::~AliHFEvarInfo(){
530 //__________________________________________________________________
531 void AliHFEcontainer::AliHFEvarInfo::SetVarName(const Char_t *name){
533 // Setter for var name
538 //__________________________________________________________________
539 void AliHFEcontainer::AliHFEvarInfo::SetBinning(UInt_t nBins, Double_t *content){
540 // Setter for binning
542 fBinning->Set(nBins + 1, content);