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 "AliCFGridSparse.h"
36 #include "AliHFEcontainer.h"
37 #include "AliHFEtools.h"
39 ClassImp(AliHFEcontainer)
40 ClassImp(AliHFEcontainer::AliHFEvarInfo)
42 //__________________________________________________________________
43 AliHFEcontainer::AliHFEcontainer():
44 TNamed("HFEcontainer", ""),
46 fCorrelationMatrices(NULL),
52 // Default constructor
56 //__________________________________________________________________
57 AliHFEcontainer::AliHFEcontainer(const Char_t *name):
60 fCorrelationMatrices(NULL),
66 // Default constructor
68 fContainers = new THashList();
69 fContainers->SetOwner();
72 //__________________________________________________________________
73 AliHFEcontainer::AliHFEcontainer(const Char_t *name, UInt_t nVar):
76 fCorrelationMatrices(NULL),
83 // Setting Number of Variables too
85 fContainers = new THashList();
86 fContainers->SetOwner();
87 SetNumberOfVariables(nVar);
90 //__________________________________________________________________
91 AliHFEcontainer::AliHFEcontainer(const AliHFEcontainer &ref):
94 fCorrelationMatrices(NULL),
97 fNEvents(ref.fNEvents)
101 // creates a new object with new (empty) containers
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);
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());
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());
130 //__________________________________________________________________
131 AliHFEcontainer &AliHFEcontainer::operator=(const AliHFEcontainer &ref){
133 // Assignment operator
134 // Cleanup old object, create a new one with new containers inside
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;
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);
150 // No varible defined, do not try to copy anything
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));
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());
175 //__________________________________________________________________
176 AliHFEcontainer::~AliHFEcontainer(){
181 if(fCorrelationMatrices) delete fCorrelationMatrices;
183 fVariables->Delete();
188 //__________________________________________________________________
189 Long64_t AliHFEcontainer::Merge(TCollection *coll){
202 AliHFEcontainer *cont = dynamic_cast<AliHFEcontainer *>(o);
205 // Merge the two TObjArrays
207 containers.Add(cont->fContainers);
208 fContainers->Merge(&containers);
210 if(fCorrelationMatrices && cont->fCorrelationMatrices){
212 containers.Add(cont->fCorrelationMatrices);
213 fCorrelationMatrices->Merge(&containers);
216 fNEvents += cont->GetNumberOfEvents();
222 //__________________________________________________________________
223 void AliHFEcontainer::SetNumberOfVariables(UInt_t nVar){
225 // Define the number of variables
226 // Initialize containers for the variable informations
231 fVariables = new TObjArray(nVar);
232 for(UInt_t ivar = 0; ivar < nVar; ivar++)
233 fVariables->AddAt(new AliHFEvarInfo, ivar);
236 //__________________________________________________________________
237 void AliHFEcontainer::CreateContainer(const Char_t *name, const Char_t *title, UInt_t nStep){
239 // Create a new Correction Framework Container and store it
241 if(fContainers->FindObject(name)){
242 AliError(Form("Container %s already exists. Cannot replace it!", name));
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;
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));
256 cont->SetBinLimits(ivar, var->GetBinning());
257 cont->SetVarTitle(ivar, var->GetVarName()->Data());
261 fContainers->Add(cont);
262 AliInfo(Form("Container %s created with %d cut steps", name, nStep));
265 //__________________________________________________________________
266 void AliHFEcontainer::CreateCorrelationMatrix(const Char_t *name, const Char_t *title){
268 // Create Correlation Matrix
270 if(!fCorrelationMatrices){
271 fCorrelationMatrices = new THashList;
272 fCorrelationMatrices->SetName("fCorrelationMatrices");
273 fCorrelationMatrices->SetOwner();
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));
281 nBins[ivar] = var->GetNumberOfBins();
282 nBins[ivar+fNVars] = var->GetNumberOfBins();
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));
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());
299 fCorrelationMatrices->AddLast(hTmp);
302 //__________________________________________________________________
303 AliCFContainer *AliHFEcontainer::GetCFContainer(const Char_t *name) const{
305 // Find a given container
307 return dynamic_cast<AliCFContainer *>(fContainers->FindObject(name));
310 //__________________________________________________________________
311 THnSparseF *AliHFEcontainer::GetCorrelationMatrix(const Char_t *name) const{
313 // Find Correlation Matrix
315 if(fCorrelationMatrices) return dynamic_cast<THnSparseF *>(fCorrelationMatrices->FindObject(name));
320 //__________________________________________________________________
321 void AliHFEcontainer::FillCFContainer(const Char_t *name, UInt_t step, const Double_t * const content, Double_t weight) const {
325 AliCFContainer *cont = GetCFContainer(name);
327 cont->Fill(content, step, weight);
330 //__________________________________________________________________
331 void AliHFEcontainer::FillCFContainerStepname(const Char_t *name, const Char_t *steptitle, const Double_t * const content, Double_t weight)const{
335 AliCFContainer *cont = GetCFContainer(name);
337 // find the matching step title
339 for(Int_t istep = 0; istep < cont->GetNStep(); istep++){
340 TString tstept = cont->GetStepTitle(istep);
341 if(!tstept.CompareTo(steptitle)){
348 AliDebug(1, Form("Step %s not found in container %s", steptitle, name));
351 AliDebug(1, Form("Filling step %s(%d) for container %s", steptitle, mystep, name));
352 cont->Fill(content, mystep, weight);
355 //__________________________________________________________________
356 AliCFContainer *AliHFEcontainer::MakeMergedCFContainer(const Char_t *name, const Char_t *title, const Char_t* contnames) const {
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
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()));
372 AliWarning(Form("Container %s not found. It will be unprocessed", cname->String().Data()));
375 nStepMerged += ctemp->GetNStep();
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");
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()));
389 for(Int_t istep = 0; istep < ctemp->GetNStep(); istep++)
390 cmerged->SetGrid(cstep++, new AliCFGridSparse(*ctemp->GetGrid(istep)));
396 //__________________________________________________________________
397 void AliHFEcontainer::SetStepTitle(const Char_t *contname, const Char_t *steptitle, UInt_t step){
399 // Set title for given analysis step in container with name contname
401 AliCFContainer *cont = GetCFContainer(contname);
403 if(step >= static_cast<UInt_t>(cont->GetNStep())) return;
404 cont->SetStepTitle(step, steptitle);
407 //__________________________________________________________________
408 void AliHFEcontainer::MakeLinearBinning(UInt_t var, UInt_t nBins, Double_t begin, Double_t end){
410 // Set Linear binning for the given container
412 AliHFEvarInfo *myvar = dynamic_cast<AliHFEvarInfo *>(fVariables->UncheckedAt(var));
413 if(myvar) myvar->SetBinning(nBins, AliHFEtools::MakeLinearBinning(nBins, begin, end));
416 //__________________________________________________________________
417 void AliHFEcontainer::MakeLogarithmicBinning(UInt_t var, UInt_t nBins, Double_t begin, Double_t end){
419 // Set Logarithmic binning for the given container
421 AliHFEvarInfo *myvar = dynamic_cast<AliHFEvarInfo *>(fVariables->UncheckedAt(var));
422 if(myvar) myvar->SetBinning(nBins, AliHFEtools::MakeLogarithmicBinning(nBins, begin, end));
425 //__________________________________________________________________
426 void AliHFEcontainer::MakeUserDefinedBinning(UInt_t var, UInt_t nBins, const Double_t *binning){
428 // Set User defined binning
430 AliHFEvarInfo *myvar = dynamic_cast<AliHFEvarInfo *>(fVariables->UncheckedAt(var));
431 if(myvar) myvar->SetBinning(nBins, binning);
434 //__________________________________________________________________
435 void AliHFEcontainer::SetVariableName(UInt_t var, const Char_t *varname){
439 AliHFEvarInfo *myvar = dynamic_cast<AliHFEvarInfo *>(fVariables->UncheckedAt(var));
440 if(myvar) myvar->SetVarName(varname);
443 //__________________________________________________________________
444 Int_t AliHFEcontainer::GetNumberOfCFContainers() const{
446 // Get the number of entries
448 return fContainers->GetEntries();
451 //__________________________________________________________________
452 void AliHFEcontainer::Sumw2(const char *contname) const {
454 // Set sums of weights for all steps of a given container
456 AliCFContainer *cont = GetCFContainer(contname);
458 for(Int_t istep = 0; istep < cont->GetNStep(); istep++)
459 cont->GetGrid(istep)->SumW2();
463 //__________________________________________________________________
464 void AliHFEcontainer::Print(const Option_t *)const{
466 // Print Container Status
468 std::cout << "Container status: " << std::endl;
469 std::cout << "=====================================================\n";
470 std::cout << "Number of variables: " << fNVars << std::endl;
472 UInt_t nVars = fVariables ? fVariables->GetEntriesFast() : 0;
474 std::cout << "Inconsistency in number of Variables [" << fNVars << "|" << nVars << "]" << std::endl;
475 AliHFEvarInfo *var = NULL;
477 for(UInt_t ivar = 0; ivar < fNVars; ivar++){
478 var = dynamic_cast<AliHFEvarInfo *>(fVariables->UncheckedAt(ivar));
480 std::cout << "Variable " << ivar << ": Name: " << var->GetVarName()->Data() << ", Number of Bins: " << var->GetNumberOfBins() << std::endl;
484 std::cout << std::endl;
486 // Print CF Containers:
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));
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;
497 std::cout << "------------------------------------------------------\n";
500 std::cout << "Number of Events: " << fNEvents << std::endl;
503 //------------------------------------ Content of class AliHFEvarInfo -----------------------------------
504 //__________________________________________________________________
505 AliHFEcontainer::AliHFEvarInfo::AliHFEvarInfo():
510 // Default constructor
511 fBinning = new TArrayD;
512 fVarName = new TString;
515 //__________________________________________________________________
516 AliHFEcontainer::AliHFEvarInfo::AliHFEvarInfo(const Char_t *name):
521 fBinning = new TArrayD;
522 fVarName = new TString(name);
525 //__________________________________________________________________
526 AliHFEcontainer::AliHFEvarInfo::AliHFEvarInfo(const AliHFEvarInfo &ref):
534 fVarName = new TString(*(ref.fVarName));
535 fBinning = new TArrayD(*(ref.fBinning));
538 //__________________________________________________________________
539 AliHFEcontainer::AliHFEvarInfo &AliHFEcontainer::AliHFEvarInfo::operator=(const AliHFEvarInfo &ref){
541 // Assignment operator
543 TObject::operator=(ref);
544 *fVarName = *(ref.fVarName);
545 *fBinning = *(ref.fBinning);
549 //__________________________________________________________________
550 AliHFEcontainer::AliHFEvarInfo::~AliHFEvarInfo(){
558 //__________________________________________________________________
559 void AliHFEcontainer::AliHFEvarInfo::SetVarName(const Char_t *name){
561 // Setter for var name
566 //__________________________________________________________________
567 void AliHFEcontainer::AliHFEvarInfo::SetBinning(UInt_t nBins, const Double_t *content){
568 // Setter for binning
570 fBinning->Set(nBins + 1, content);