]> git.uio.no Git - u/mrichter/AliRoot.git/blame - PWG3/hfe/AliHFEcontainer.cxx
Major update of the HFE package (comments inside the code
[u/mrichter/AliRoot.git] / PWG3 / hfe / AliHFEcontainer.cxx
CommitLineData
70da6c5a 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>
67fe7bd0 28#include <THnSparse.h>
70da6c5a 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
38ClassImp(AliHFEcontainer)
39ClassImp(AliHFEcontainer::AliHFEvarInfo)
40
41//__________________________________________________________________
42AliHFEcontainer::AliHFEcontainer():
43 TNamed("HFEcontainer", ""),
44 fContainers(NULL),
67fe7bd0 45 fCorrelationMatrices(NULL),
70da6c5a 46 fVariables(NULL),
47 fNVars(0),
48 fNEvents(0)
49{
50 //
51 // Default constructor
52 //
53 fContainers = new THashList();
3a72645a 54 fContainers->SetOwner();
70da6c5a 55}
56
57//__________________________________________________________________
58AliHFEcontainer::AliHFEcontainer(const Char_t *name):
59 TNamed(name, ""),
60 fContainers(NULL),
67fe7bd0 61 fCorrelationMatrices(NULL),
70da6c5a 62 fVariables(NULL),
63 fNVars(0),
64 fNEvents(0)
65{
66 //
67 // Default constructor
68 //
69 fContainers = new THashList();
3a72645a 70 fContainers->SetOwner();
70da6c5a 71}
72
73//__________________________________________________________________
74AliHFEcontainer::AliHFEcontainer(const Char_t *name, UInt_t nVar):
75 TNamed(name, ""),
76 fContainers(NULL),
67fe7bd0 77 fCorrelationMatrices(NULL),
70da6c5a 78 fVariables(NULL),
79 fNVars(0),
80 fNEvents(0)
81{
82 //
83 // Constructor
84 // Setting Number of Variables too
85 //
86 fContainers = new THashList();
3a72645a 87 fContainers->SetOwner();
70da6c5a 88 SetNumberOfVariables(nVar);
89}
90
91//__________________________________________________________________
92AliHFEcontainer::AliHFEcontainer(const AliHFEcontainer &ref):
93 TNamed(ref),
94 fContainers(NULL),
67fe7bd0 95 fCorrelationMatrices(NULL),
70da6c5a 96 fVariables(NULL),
97 fNVars(ref.fNVars),
98 fNEvents(ref.fNEvents)
99{
100 //
101 // Copy constructor
67fe7bd0 102 // creates a new object with new (empty) containers
70da6c5a 103 //
70da6c5a 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 }
67fe7bd0 109 fContainers = new THashList;
3a72645a 110 fContainers->SetOwner();
67fe7bd0 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;
3a72645a 120 fCorrelationMatrices->SetOwner();
67fe7bd0 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 }
70da6c5a 126}
127
128//__________________________________________________________________
129AliHFEcontainer &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 }
3a72645a 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 }
70da6c5a 157 return *this;
158}
159
160//__________________________________________________________________
161AliHFEcontainer::~AliHFEcontainer(){
162 //
163 // Destructor
164 //
70da6c5a 165 delete fContainers;
3a72645a 166 if(fCorrelationMatrices) delete fCorrelationMatrices;
70da6c5a 167 if(fVariables){
168 fVariables->Delete();
169 delete fVariables;
170 }
171}
172
173//__________________________________________________________________
174Long64_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
67fe7bd0 195 if(fCorrelationMatrices && cont->fCorrelationMatrices){
196 containers.Clear();
197 containers.Add(cont->fCorrelationMatrices);
198 fCorrelationMatrices->Merge(&containers);
199 }
200
70da6c5a 201 fNEvents += cont->GetNumberOfEvents();
202 count++;
203 }
204 return count + 1;
205}
206
207//__________________________________________________________________
208void 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//__________________________________________________________________
222void 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//__________________________________________________________________
67fe7bd0 246void 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");
3a72645a 253 fCorrelationMatrices->SetOwner();
67fe7bd0 254 }
255
256 Int_t *nBins = new Int_t[2*fNVars];
67fe7bd0 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();
3a72645a 261 nBins[ivar+fNVars] = var->GetNumberOfBins();
67fe7bd0 262 }
263
3a72645a 264 THnSparseF * hTmp = new THnSparseF(name, title, 2*fNVars, nBins);
67fe7bd0 265 for(UInt_t ivar = 0; ivar < fNVars; ivar++){
266 var = dynamic_cast<AliHFEvarInfo *>(fVariables->UncheckedAt(ivar));
3a72645a 267 hTmp->SetBinEdges(ivar,var->GetBinning());
268 //hTmp->GetAxis(ivar)->Set(var->GetNumberOfBins(), var->GetBinning());
67fe7bd0 269 hTmp->GetAxis(ivar)->SetTitle(var->GetVarName()->Data());
3a72645a 270 //hTmp->GetAxis(ivar + fNVars)->Set(var->GetNumberOfBins(), var->GetBinning());
67fe7bd0 271 hTmp->GetAxis(ivar + fNVars)->SetTitle(Form("%s_{MC}", var->GetVarName()->Data()));
3a72645a 272 hTmp->SetBinEdges(ivar+fNVars,var->GetBinning());
67fe7bd0 273 }
274 hTmp->Sumw2();
275 fCorrelationMatrices->AddLast(hTmp);
276}
277
278//__________________________________________________________________
279AliCFContainer *AliHFEcontainer::GetCFContainer(const Char_t *name) const{
70da6c5a 280 //
281 // Find a given container
282 //
283 return dynamic_cast<AliCFContainer *>(fContainers->FindObject(name));
284}
285
67fe7bd0 286//__________________________________________________________________
287THnSparseF *AliHFEcontainer::GetCorrelationMatrix(const Char_t *name) const{
288 //
289 // Find Correlation Matrix
290 //
291 return dynamic_cast<THnSparseF *>(fCorrelationMatrices->FindObject(name));
292}
293
70da6c5a 294//__________________________________________________________________
3a72645a 295void AliHFEcontainer::FillCFContainer(const Char_t *name, UInt_t step, Double_t *content, Double_t weight) const {
70da6c5a 296 //
297 // Fill container
298 //
299 AliCFContainer *cont = GetCFContainer(name);
300 if(!cont) return;
3a72645a 301 cont->Fill(content, step, weight);
302}
303
304//__________________________________________________________________
305void AliHFEcontainer::FillCFContainerStepname(const Char_t *name, const Char_t *steptitle, Double_t *content, Double_t weight)const{
306 //
307 // Fill container
308 //
309 AliCFContainer *cont = GetCFContainer(name);
310 if(!cont) return;
311 // find the matching step title
312 Int_t mystep = -1;
313 for(Int_t istep = 0; istep < cont->GetNStep(); istep++){
314 TString tstept = cont->GetStepTitle(istep);
315 if(!tstept.CompareTo(steptitle)){
316 mystep = istep;
317 break;
318 }
319 }
320 if(mystep < 0){
321 // step not found
322 AliDebug(1, Form("Step %s not found in container %s", steptitle, name));
323 return;
324 }
325 AliDebug(1, Form("Filling step %s(%d) for container %s", steptitle, mystep, name));
326 cont->Fill(content, mystep, weight);
70da6c5a 327}
328
329//__________________________________________________________________
330AliCFContainer *AliHFEcontainer::MakeMergedCFContainer(const Char_t *name, const Char_t *title, const Char_t* contnames){
331 //
332 // Merge CF Container out of several containers
333 // Container names are separated by :
334 // returns a new object which has to be taken care of by the user
335 //
336
337 TObjArray *containers = TString(contnames).Tokenize(":");
338 // we first need the size of the container to be merged
339 Int_t nStepMerged = 0;
340 AliCFContainer *ctemp = NULL;
341 TObjString *cname = NULL;
342 for(Int_t icont = 0; icont < containers->GetEntries(); icont++){
343 cname = dynamic_cast<TObjString *>(containers->At(icont));
344 ctemp = dynamic_cast<AliCFContainer *>(fContainers->FindObject(cname->String().Data()));
345 if(!ctemp){
346 AliWarning(Form("Container %s not found. It will be unprocessed", cname->String().Data()));
347 continue;
348 }
349 nStepMerged += ctemp->GetNStep();
350 }
351 AliInfo("Please Ignore the messgae comming from AliCFContainer!");
352 Int_t *dummyBinning = new Int_t[fNVars];
353 for(UInt_t ibin = 0; ibin < fNVars; ibin++) dummyBinning[ibin] = 1;
354 AliCFContainer *cmerged = new AliCFContainer(name, title, nStepMerged, fNVars, dummyBinning);
e3fc062d 355 delete dummyBinning;
70da6c5a 356 // Fill container with content
357 AliInfo("Filling new container");
358 Int_t cstep = 0;
359 for(Int_t icont = 0; icont < containers->GetEntries(); icont++){
360 cname = dynamic_cast<TObjString *>(containers->At(icont));
361 ctemp = dynamic_cast<AliCFContainer *>(fContainers->FindObject(cname->String().Data()));
362 if(!ctemp) continue;
363 for(Int_t istep = 0; istep < ctemp->GetNStep(); istep++)
364 cmerged->SetGrid(cstep++, new AliCFGridSparse(*ctemp->GetGrid(istep)));
365 }
366 return cmerged;
367}
3a72645a 368
369//__________________________________________________________________
370void AliHFEcontainer::SetStepTitle(const Char_t *contname, const Char_t *steptitle, UInt_t step){
371 //
372 // Set title for given analysis step in container with name contname
373 //
374 AliCFContainer *cont = GetCFContainer(contname);
375 if(!cont) return;
376 if(step >= static_cast<UInt_t>(cont->GetNStep())) return;
377 cont->SetStepTitle(step, steptitle);
378}
379
70da6c5a 380//__________________________________________________________________
381void AliHFEcontainer::MakeLinearBinning(UInt_t var, UInt_t nBins, Double_t begin, Double_t end){
382 //
383 // Set Linear binning for the given container
384 //
385 (dynamic_cast<AliHFEvarInfo *>(fVariables->UncheckedAt(var)))->SetBinning(nBins, AliHFEtools::MakeLinearBinning(nBins, begin, end));
386}
387
388//__________________________________________________________________
389void AliHFEcontainer::MakeLogarithmicBinning(UInt_t var, UInt_t nBins, Double_t begin, Double_t end){
390 //
391 // Set Logarithmic binning for the given container
392 //
393 (dynamic_cast<AliHFEvarInfo *>(fVariables->UncheckedAt(var)))->SetBinning(nBins, AliHFEtools::MakeLogarithmicBinning(nBins, begin, end));
394}
395
396//__________________________________________________________________
397void AliHFEcontainer::SetVariableName(UInt_t var, const Char_t *varname){
398 //
399 // Variable name
400 //
401 (dynamic_cast<AliHFEvarInfo *>(fVariables->UncheckedAt(var)))->SetVarName(varname);
402}
403
404//__________________________________________________________________
405Int_t AliHFEcontainer::GetNumberOfCFContainers() const{
406 //
407 // Get the number of entries
408 //
409 return fContainers->GetEntries();
410}
411
412//__________________________________________________________________
413void AliHFEcontainer::Print(const Option_t *)const{
414 //
415 // Print Container Status
416 //
417 std::cout << "Container status: " << std::endl;
418 std::cout << "=====================================================\n";
419 std::cout << "Number of variables: " << fNVars << std::endl;
420 if(fNVars){
421 UInt_t nVars = fVariables ? fVariables->GetEntriesFast() : 0;
422 if(nVars != fNVars)
423 std::cout << "Inconsistency in number of Variables [" << fNVars << "|" << nVars << "]" << std::endl;
424 AliHFEvarInfo *var = NULL;
425 for(UInt_t ivar = 0; ivar < fNVars; ivar++){
426 var = dynamic_cast<AliHFEvarInfo *>(fVariables->UncheckedAt(ivar));
427 std::cout << "Variable " << ivar << ": Name: " << var->GetVarName()->Data() << ", Number of Bins: " << var->GetNumberOfBins() << std::endl;
428 }
429 }
430 std::cout << std::endl;
431
432 // Print CF Containers:
433 std::cout << "Containers[" << fContainers->GetEntries() << "]: "<< std::endl;
434 std::cout << "=====================================================\n";
435 for(Int_t icont = 0; icont < fContainers->GetEntries(); icont++){
436 AliCFContainer *c = dynamic_cast<AliCFContainer *>(fContainers->At(icont));
437 std::cout << "Name: " << c->GetName() << ", Title: " << c->GetTitle() << std::endl;
438 for(Int_t istep = 0; istep < c->GetNStep(); istep++)
439 std::cout << "Step " << istep << ": Title " << c->GetStepTitle(istep) << std::endl;
440 std::cout << "------------------------------------------------------\n";
441 }
442 std::cout << "Number of Events: " << fNEvents << std::endl;
443}
444
445//------------------------------------ Content of class AliHFEvarInfo -----------------------------------
446//__________________________________________________________________
447AliHFEcontainer::AliHFEvarInfo::AliHFEvarInfo():
448 TObject(),
449 fVarName(NULL),
450 fBinning(NULL)
451{
452 // Default constructor
453 fBinning = new TArrayD;
454 fVarName = new TString;
455}
456
457//__________________________________________________________________
458AliHFEcontainer::AliHFEvarInfo::AliHFEvarInfo(const Char_t *name):
459 TObject(),
460 fVarName(NULL),
461 fBinning(NULL)
462{
463 fBinning = new TArrayD;
464 fVarName = new TString(name);
465}
466
467//__________________________________________________________________
468AliHFEcontainer::AliHFEvarInfo::AliHFEvarInfo(const AliHFEvarInfo &ref):
469 TObject(ref),
470 fVarName(NULL),
471 fBinning(NULL)
472{
473 //
474 // Constructor
475 //
476 fVarName = new TString(*(ref.fVarName));
477 fBinning = new TArrayD(*(ref.fBinning));
478}
479
480//__________________________________________________________________
481AliHFEcontainer::AliHFEvarInfo &AliHFEcontainer::AliHFEvarInfo::operator=(const AliHFEvarInfo &ref){
482 //
483 // Assignment operator
484 //
485 TObject::operator=(ref);
486 *fVarName = *(ref.fVarName);
487 *fBinning = *(ref.fBinning);
488 return *this;
489}
490
491//__________________________________________________________________
492AliHFEcontainer::AliHFEvarInfo::~AliHFEvarInfo(){
493 //
494 // Destructor
495 //
496 delete fVarName;
497 delete fBinning;
498}
499
500//__________________________________________________________________
501void AliHFEcontainer::AliHFEvarInfo::SetVarName(const Char_t *name){
502 //
503 // Setter for var name
504 //
505 *fVarName = name;
506}
507
508//__________________________________________________________________
509void AliHFEcontainer::AliHFEvarInfo::SetBinning(UInt_t nBins, Double_t *content){
510 // Setter for binning
511 //
512 fBinning->Set(nBins + 1, content);
513}
514