Fix for bug #71737 DELETE_ARRAY reported by Coverity
[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>
28#include <TList.h>
29#include <TObjArray.h>
30#include <TObjString.h>
31#include <TString.h>
32
33#include "AliCFContainer.h"
34#include "AliHFEcontainer.h"
35#include "AliHFEtools.h"
36
37ClassImp(AliHFEcontainer)
38ClassImp(AliHFEcontainer::AliHFEvarInfo)
39
40//__________________________________________________________________
41AliHFEcontainer::AliHFEcontainer():
42 TNamed("HFEcontainer", ""),
43 fContainers(NULL),
44 fVariables(NULL),
45 fNVars(0),
46 fNEvents(0)
47{
48 //
49 // Default constructor
50 //
51 fContainers = new THashList();
52}
53
54//__________________________________________________________________
55AliHFEcontainer::AliHFEcontainer(const Char_t *name):
56 TNamed(name, ""),
57 fContainers(NULL),
58 fVariables(NULL),
59 fNVars(0),
60 fNEvents(0)
61{
62 //
63 // Default constructor
64 //
65 fContainers = new THashList();
66}
67
68//__________________________________________________________________
69AliHFEcontainer::AliHFEcontainer(const Char_t *name, UInt_t nVar):
70 TNamed(name, ""),
71 fContainers(NULL),
72 fVariables(NULL),
73 fNVars(0),
74 fNEvents(0)
75{
76 //
77 // Constructor
78 // Setting Number of Variables too
79 //
80 fContainers = new THashList();
81 SetNumberOfVariables(nVar);
82}
83
84//__________________________________________________________________
85AliHFEcontainer::AliHFEcontainer(const AliHFEcontainer &ref):
86 TNamed(ref),
87 fContainers(NULL),
88 fVariables(NULL),
89 fNVars(ref.fNVars),
90 fNEvents(ref.fNEvents)
91{
92 //
93 // Copy constructor
94 // creates a new object with new containers
95 //
96 fContainers = new THashList;
97 for(Int_t ien = 0; ien < ref.fContainers->GetEntries(); ien++)
98 fContainers->Add(new AliCFContainer(*dynamic_cast<AliCFContainer *>(ref.fContainers->At(ien))));
99 if(fNVars){
100 fVariables = new TObjArray(fNVars);
101 for(UInt_t ivar = 0; ivar < fNVars; ivar++)
102 fVariables->AddAt(new AliHFEvarInfo(*dynamic_cast<AliHFEvarInfo *>(ref.fVariables->UncheckedAt(ivar))), ivar);
103 }
104}
105
106//__________________________________________________________________
107AliHFEcontainer &AliHFEcontainer::operator=(const AliHFEcontainer &ref){
108 //
109 // Assignment operator
110 // Cleanup old object, create a new one with new containers inside
111 //
112 this->~AliHFEcontainer(); // cleanup old object before creating the new onwe
113 TNamed::operator=(ref);
114 fContainers = new THashList();
115 fNVars = ref.fNVars;
116 for(Int_t ien = 0; ien < ref.fContainers->GetEntries(); ien++)
117 fContainers->Add(new AliCFContainer(*dynamic_cast<AliCFContainer *>(ref.fContainers->At(ien))));
118 if(fNVars){
119 fVariables = new TObjArray(fNVars);
120 for(UInt_t ivar = 0; ivar < fNVars; ivar++)
121 fVariables->AddAt(new AliHFEvarInfo(*dynamic_cast<AliHFEvarInfo *>(ref.fVariables->UncheckedAt(ivar))), ivar);
122 } else {
123 fVariables = NULL;
124 }
125
126 return *this;
127}
128
129//__________________________________________________________________
130AliHFEcontainer::~AliHFEcontainer(){
131 //
132 // Destructor
133 //
134 fContainers->Delete();
135 delete fContainers;
136 if(fVariables){
137 fVariables->Delete();
138 delete fVariables;
139 }
140}
141
142//__________________________________________________________________
143Long64_t AliHFEcontainer::Merge(TCollection *coll){
144 //
145 // Merge Container
146 //
147 if(!coll)
148 return 0;
149 if(coll->IsEmpty())
150 return 1;
151
152 TIterator *iter = coll->MakeIterator();
153 TObject *o = NULL;
154 Long64_t count = 0;
155 while((o = iter->Next())){
156 AliHFEcontainer *cont = dynamic_cast<AliHFEcontainer *>(o);
157 if(!cont) continue;
158
159 // Merge the two TObjArrays
160 TList containers;
161 containers.Add(cont->fContainers);
162 fContainers->Merge(&containers);
163
164 fNEvents += cont->GetNumberOfEvents();
165 count++;
166 }
167 return count + 1;
168}
169
170//__________________________________________________________________
171void AliHFEcontainer::SetNumberOfVariables(UInt_t nVar){
172 //
173 // Define the number of variables
174 // Initialize containers for the variable informations
175 //
176 if(fNVars) return;
177
178 fNVars = nVar;
179 fVariables = new TObjArray(nVar);
180 for(UInt_t ivar = 0; ivar < nVar; ivar++)
181 fVariables->AddAt(new AliHFEvarInfo, ivar);
182}
183
184//__________________________________________________________________
185void AliHFEcontainer::CreateContainer(const Char_t *name, const Char_t *title, UInt_t nStep){
186 //
187 // Create a new Correction Framework Container and store it
188 //
189 if(fContainers->FindObject(name)){
190 AliError(Form("Container %s already exists. Cannot replace it!", name));
191 return;
192 }
193
194 Int_t *nBins = new Int_t[fNVars];
195 for(UInt_t ivar = 0; ivar < fNVars; ivar++) nBins[ivar] = (dynamic_cast<AliHFEvarInfo *>(fVariables->UncheckedAt(ivar)))->GetNumberOfBins();
196 AliHFEvarInfo *var = NULL;
197 AliCFContainer *cont = new AliCFContainer(name, title, nStep, fNVars, nBins);
198 for(UInt_t ivar = 0; ivar < fNVars; ivar++){
199 var = dynamic_cast<AliHFEvarInfo *>(fVariables->UncheckedAt(ivar));
200 cont->SetBinLimits(ivar, var->GetBinning());
201 cont->SetVarTitle(ivar, var->GetVarName()->Data());
202 }
203 delete[] nBins;
204 fContainers->Add(cont);
205 AliInfo(Form("Container %s created with %d cut steps", name, nStep));
206}
207
208//__________________________________________________________________
209AliCFContainer *AliHFEcontainer::GetCFContainer(const Char_t *name){
210 //
211 // Find a given container
212 //
213 return dynamic_cast<AliCFContainer *>(fContainers->FindObject(name));
214}
215
216//__________________________________________________________________
217void AliHFEcontainer::FillCFContainer(const Char_t *name, UInt_t step, Double_t *content){
218 //
219 // Fill container
220 //
221 AliCFContainer *cont = GetCFContainer(name);
222 if(!cont) return;
223 cont->Fill(content, step);
224}
225
226//__________________________________________________________________
227AliCFContainer *AliHFEcontainer::MakeMergedCFContainer(const Char_t *name, const Char_t *title, const Char_t* contnames){
228 //
229 // Merge CF Container out of several containers
230 // Container names are separated by :
231 // returns a new object which has to be taken care of by the user
232 //
233
234 TObjArray *containers = TString(contnames).Tokenize(":");
235 // we first need the size of the container to be merged
236 Int_t nStepMerged = 0;
237 AliCFContainer *ctemp = NULL;
238 TObjString *cname = NULL;
239 for(Int_t icont = 0; icont < containers->GetEntries(); icont++){
240 cname = dynamic_cast<TObjString *>(containers->At(icont));
241 ctemp = dynamic_cast<AliCFContainer *>(fContainers->FindObject(cname->String().Data()));
242 if(!ctemp){
243 AliWarning(Form("Container %s not found. It will be unprocessed", cname->String().Data()));
244 continue;
245 }
246 nStepMerged += ctemp->GetNStep();
247 }
248 AliInfo("Please Ignore the messgae comming from AliCFContainer!");
249 Int_t *dummyBinning = new Int_t[fNVars];
250 for(UInt_t ibin = 0; ibin < fNVars; ibin++) dummyBinning[ibin] = 1;
251 AliCFContainer *cmerged = new AliCFContainer(name, title, nStepMerged, fNVars, dummyBinning);
b5ed6afb 252 delete [] dummyBinning;
70da6c5a 253 // Fill container with content
254 AliInfo("Filling new container");
255 Int_t cstep = 0;
256 for(Int_t icont = 0; icont < containers->GetEntries(); icont++){
257 cname = dynamic_cast<TObjString *>(containers->At(icont));
258 ctemp = dynamic_cast<AliCFContainer *>(fContainers->FindObject(cname->String().Data()));
259 if(!ctemp) continue;
260 for(Int_t istep = 0; istep < ctemp->GetNStep(); istep++)
261 cmerged->SetGrid(cstep++, new AliCFGridSparse(*ctemp->GetGrid(istep)));
262 }
263 return cmerged;
264}
265//__________________________________________________________________
266void AliHFEcontainer::MakeLinearBinning(UInt_t var, UInt_t nBins, Double_t begin, Double_t end){
267 //
268 // Set Linear binning for the given container
269 //
270 (dynamic_cast<AliHFEvarInfo *>(fVariables->UncheckedAt(var)))->SetBinning(nBins, AliHFEtools::MakeLinearBinning(nBins, begin, end));
271}
272
273//__________________________________________________________________
274void AliHFEcontainer::MakeLogarithmicBinning(UInt_t var, UInt_t nBins, Double_t begin, Double_t end){
275 //
276 // Set Logarithmic binning for the given container
277 //
278 (dynamic_cast<AliHFEvarInfo *>(fVariables->UncheckedAt(var)))->SetBinning(nBins, AliHFEtools::MakeLogarithmicBinning(nBins, begin, end));
279}
280
281//__________________________________________________________________
282void AliHFEcontainer::SetVariableName(UInt_t var, const Char_t *varname){
283 //
284 // Variable name
285 //
286 (dynamic_cast<AliHFEvarInfo *>(fVariables->UncheckedAt(var)))->SetVarName(varname);
287}
288
289//__________________________________________________________________
290Int_t AliHFEcontainer::GetNumberOfCFContainers() const{
291 //
292 // Get the number of entries
293 //
294 return fContainers->GetEntries();
295}
296
297//__________________________________________________________________
298void AliHFEcontainer::Print(const Option_t *)const{
299 //
300 // Print Container Status
301 //
302 std::cout << "Container status: " << std::endl;
303 std::cout << "=====================================================\n";
304 std::cout << "Number of variables: " << fNVars << std::endl;
305 if(fNVars){
306 UInt_t nVars = fVariables ? fVariables->GetEntriesFast() : 0;
307 if(nVars != fNVars)
308 std::cout << "Inconsistency in number of Variables [" << fNVars << "|" << nVars << "]" << std::endl;
309 AliHFEvarInfo *var = NULL;
310 for(UInt_t ivar = 0; ivar < fNVars; ivar++){
311 var = dynamic_cast<AliHFEvarInfo *>(fVariables->UncheckedAt(ivar));
312 std::cout << "Variable " << ivar << ": Name: " << var->GetVarName()->Data() << ", Number of Bins: " << var->GetNumberOfBins() << std::endl;
313 }
314 }
315 std::cout << std::endl;
316
317 // Print CF Containers:
318 std::cout << "Containers[" << fContainers->GetEntries() << "]: "<< std::endl;
319 std::cout << "=====================================================\n";
320 for(Int_t icont = 0; icont < fContainers->GetEntries(); icont++){
321 AliCFContainer *c = dynamic_cast<AliCFContainer *>(fContainers->At(icont));
322 std::cout << "Name: " << c->GetName() << ", Title: " << c->GetTitle() << std::endl;
323 for(Int_t istep = 0; istep < c->GetNStep(); istep++)
324 std::cout << "Step " << istep << ": Title " << c->GetStepTitle(istep) << std::endl;
325 std::cout << "------------------------------------------------------\n";
326 }
327 std::cout << "Number of Events: " << fNEvents << std::endl;
328}
329
330//------------------------------------ Content of class AliHFEvarInfo -----------------------------------
331//__________________________________________________________________
332AliHFEcontainer::AliHFEvarInfo::AliHFEvarInfo():
333 TObject(),
334 fVarName(NULL),
335 fBinning(NULL)
336{
337 // Default constructor
338 fBinning = new TArrayD;
339 fVarName = new TString;
340}
341
342//__________________________________________________________________
343AliHFEcontainer::AliHFEvarInfo::AliHFEvarInfo(const Char_t *name):
344 TObject(),
345 fVarName(NULL),
346 fBinning(NULL)
347{
348 fBinning = new TArrayD;
349 fVarName = new TString(name);
350}
351
352//__________________________________________________________________
353AliHFEcontainer::AliHFEvarInfo::AliHFEvarInfo(const AliHFEvarInfo &ref):
354 TObject(ref),
355 fVarName(NULL),
356 fBinning(NULL)
357{
358 //
359 // Constructor
360 //
361 fVarName = new TString(*(ref.fVarName));
362 fBinning = new TArrayD(*(ref.fBinning));
363}
364
365//__________________________________________________________________
366AliHFEcontainer::AliHFEvarInfo &AliHFEcontainer::AliHFEvarInfo::operator=(const AliHFEvarInfo &ref){
367 //
368 // Assignment operator
369 //
370 TObject::operator=(ref);
371 *fVarName = *(ref.fVarName);
372 *fBinning = *(ref.fBinning);
373 return *this;
374}
375
376//__________________________________________________________________
377AliHFEcontainer::AliHFEvarInfo::~AliHFEvarInfo(){
378 //
379 // Destructor
380 //
381 delete fVarName;
382 delete fBinning;
383}
384
385//__________________________________________________________________
386void AliHFEcontainer::AliHFEvarInfo::SetVarName(const Char_t *name){
387 //
388 // Setter for var name
389 //
390 *fVarName = name;
391}
392
393//__________________________________________________________________
394void AliHFEcontainer::AliHFEvarInfo::SetBinning(UInt_t nBins, Double_t *content){
395 // Setter for binning
396 //
397 fBinning->Set(nBins + 1, content);
398}
399