Adding protection
[u/mrichter/AliRoot.git] / CORRFW / AliCFContainer.cxx
CommitLineData
563113d0 1/* $Id$ */
1e9dad92 2/**************************************************************************
3 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * *
5 * Author: The ALICE Off-line Project. *
6 * Contributors are mentioned in the code where appropriate. *
7 * *
8 * Permission to use, copy, modify and distribute this software and its *
9 * documentation strictly for non-commercial purposes is hereby granted *
10 * without fee, provided that the above copyright notice appears in all *
11 * copies and that both the copyright notice and this permission notice *
12 * appear in the supporting documentation. The authors make no claims *
13 * about the suitability of this software for any purpose. It is *
14 * provided "as is" without express or implied warranty. *
15 **************************************************************************/
563113d0 16//--------------------------------------------------------------------//
17// //
9105291d 18// AliCFContainer Class //
19// Class to accumulate data on an N-dimensional grids, at different //
563113d0 20// selection stages. To be used as an input to get corrections for //
21// Reconstruction & Trigger efficiency //
22// //
23// -- Author : S.Arcelli //
24//--------------------------------------------------------------------//
25//
26//
27#include <AliLog.h>
db6722a5 28#include "AliCFGridSparse.h"
563113d0 29#include "AliCFContainer.h"
c8df672e 30#include "TAxis.h"
563113d0 31//____________________________________________________________________
32ClassImp(AliCFContainer)
33
34//____________________________________________________________________
35AliCFContainer::AliCFContainer() :
36 AliCFFrame(),
37 fNStep(0),
38 fGrid(0x0)
39{
40 //
41 // default constructor
42 //
43}
563113d0 44
45//____________________________________________________________________
fb494025 46AliCFContainer::AliCFContainer(const Char_t* name, const Char_t* title, const Int_t nSelSteps, const Int_t nVarIn, const Int_t* nBinIn) :
47 AliCFFrame(name,title),
48 fNStep(nSelSteps),
563113d0 49 fGrid(0x0)
50{
51 //
52 // main constructor
53 //
54
563113d0 55 // The grids
fb494025 56 fGrid = new AliCFGridSparse*[fNStep]; //the grids at the various selection steps
fb494025 57 for (Int_t istep=0; istep<fNStep; istep++) {
d91baff0 58 fGrid[istep] = new AliCFGridSparse(Form("%s_SelStep%d",name,istep),Form("step%d",istep),nVarIn,nBinIn);
fb494025 59 fGrid[istep]->SumW2();
563113d0 60 }
d91baff0 61 for (Int_t iVar=0; iVar<nVarIn; iVar++) SetVarTitle(iVar,Form("var%d",iVar));
fb494025 62 AliInfo(Form("Grids created for %d steps required \n => Don't forget to set the bin limits !!",fNStep));
563113d0 63}
64//____________________________________________________________________
fb494025 65AliCFContainer::AliCFContainer(const AliCFContainer& c) :
66 AliCFFrame(c.fName,c.fTitle),
67 fNStep(0),
68 fGrid(0x0)
563113d0 69{
70 //
71 // copy constructor
72 //
fb494025 73 c.Copy(*this);
563113d0 74}
75//____________________________________________________________________
76AliCFContainer::~AliCFContainer()
77{
78 //
79 // destructor
80 //
fb494025 81 if (fGrid) delete [] fGrid;
563113d0 82}
83//____________________________________________________________________
84AliCFContainer &AliCFContainer::operator=(const AliCFContainer &c)
85{
86 //
87 // assigment operator
88 //
fb494025 89 if (this != &c) c.Copy(*this);
563113d0 90 return *this;
91}
fb494025 92
563113d0 93//____________________________________________________________________
94void AliCFContainer::Copy(TObject& c) const
95{
96 //
97 // copy function
98 //
fb494025 99 AliCFFrame::Copy(c);
563113d0 100 AliCFContainer& target = (AliCFContainer &) c;
fb494025 101 target.fNStep = fNStep;
102 target.fGrid = new AliCFGridSparse*[fNStep];
103 for (Int_t iStep=0; iStep<fNStep; iStep++) {
104 if (fGrid[iStep]) target.fGrid[iStep] = new AliCFGridSparse(*(fGrid[iStep]));
105 }
563113d0 106}
fb494025 107
563113d0 108//____________________________________________________________________
fb494025 109void AliCFContainer::Fill(const Double_t *var, Int_t istep, Double_t weight)
563113d0 110{
111 //
112 // Fills the grid at selection step istep for a set of values of the
113 // input variables, with a given weight (by default w=1)
114 //
a2255142 115 if(istep >= fNStep || istep < 0){
116 AliError("Non-existent selection step, grid was not filled");
117 return;
118 }
563113d0 119 fGrid[istep]->Fill(var,weight);
120}
9105291d 121
d91baff0 122//____________________________________________________________________
123TH1* AliCFContainer::Project(Int_t istep, Int_t ivar1, Int_t ivar2, Int_t ivar3) const
563113d0 124{
125 //
d91baff0 126 // returns a projection along variables ivar1 (and ivar2 (and ivar3))
563113d0 127 // at selection step istep
128 //
d91baff0 129 if (istep >= fNStep || istep < 0){
a2255142 130 AliError("Non-existent selection step, return NULL");
131 return 0x0;
132 }
563113d0 133 return fGrid[istep]->Project(ivar1,ivar2,ivar3);
134}
d91baff0 135
563113d0 136//____________________________________________________________________
98a5f772 137AliCFContainer* AliCFContainer::MakeSlice(Int_t nVars, const Int_t* vars, const Double_t* varMin, const Double_t* varMax, Bool_t useBins) const
9105291d 138{
139 //
38b1447f 140 // Makes a slice along the "nVars" variables defined in the array "vars[nVars]" for all the container steps.
fb494025 141 // The ranges of ALL the container variables must be defined in the array varMin[GetNVar()] and varMax[GetNVar()]
38b1447f 142 // The function returns a new container of nVars variables.
98a5f772 143 // If useBins=true, varMin and varMax are taken as bin numbers
38b1447f 144 //
145 Int_t* steps = new Int_t[fNStep];
146 for (Int_t iStep=0;iStep<fNStep;iStep++) steps[iStep]=iStep;
d91baff0 147 AliCFContainer* out = MakeSlice(fNStep,steps,nVars,vars,varMin,varMax,useBins);
9be0fa4e 148 delete [] steps ;
149 return out;
38b1447f 150}
151
152//____________________________________________________________________
d91baff0 153AliCFContainer* AliCFContainer::MakeSlice(Int_t nSteps, const Int_t* steps,
154 Int_t nVars, const Int_t* vars,
155 const Double_t* varMin, const Double_t* varMax, Bool_t useBins) const
38b1447f 156{
157 //
158 // Makes a slice along the "nVars" variables defined in the array "vars[nVars]" for the given "nSteps" defined in "steps[nSteps]".
fb494025 159 // The ranges of ALL the container variables must be defined in the array varMin[GetNVar()] and varMax[GetNVar()]
a8b83c87 160 // The function returns a new container of nVars variables.
98a5f772 161 // If useBins=true, varMin and varMax are taken as bin numbers
9105291d 162 //
163
fb494025 164 if (nVars < 1 || nVars > GetNVar()) AliError("Bad number of dimensions required for the slice");
38b1447f 165 if (nSteps< 1 || nSteps> fNStep) AliError("Bad number of steps required for the slice");
9105291d 166
9be0fa4e 167 AliInfo(Form("Making a slice in %d dimension(s)",nVars));
168
169 // create the output grids
fb494025 170 AliCFGridSparse** grids = new AliCFGridSparse*[nSteps] ;
d91baff0 171 for (Int_t iStep=0; iStep<nSteps; iStep++) grids[iStep] = fGrid[steps[iStep]]->MakeSlice(nVars,vars,varMin,varMax,useBins);
172
9be0fa4e 173 TAxis ** axis = new TAxis*[nVars];
174 for (Int_t iVar=0; iVar<nVars; iVar++) axis[iVar] = ((AliCFGridSparse*)grids[0])->GetGrid()->GetAxis(iVar); //same axis for every grid
175
9105291d 176 //define new binning for new container
177 Int_t* bins=new Int_t[nVars];
9be0fa4e 178 for (Int_t iVar=0; iVar<nVars; iVar++) bins[iVar] = axis[iVar]->GetNbins();
179
38b1447f 180 AliCFContainer* out = new AliCFContainer(fName,fTitle,nSteps,nVars,bins);
9105291d 181
182 //set the bin limits
183 for (Int_t iVar=0; iVar<nVars; iVar++) {
9be0fa4e 184 Int_t nBins = bins[iVar];
185 Double_t *array = new Double_t[nBins+1];
186 for (Int_t iBin=1; iBin<=nBins; iBin++) {
187 array[iBin-1] = axis[iVar]->GetBinLowEdge(iBin);
188 }
189 array[nBins] = axis[iVar]->GetBinUpEdge(nBins);
9105291d 190 out->SetBinLimits(iVar,array);
9be0fa4e 191 delete [] array;
9105291d 192 }
193
38b1447f 194 //set grid for the given steps
9be0fa4e 195 for (Int_t iStep=0; iStep<nSteps; iStep++) out->SetGrid(iStep,grids[iStep]);
9105291d 196
98a5f772 197 delete [] bins;
9be0fa4e 198 delete [] axis ;
9105291d 199 return out;
200}
201
202//____________________________________________________________________
563113d0 203Long64_t AliCFContainer::Merge(TCollection* list)
204{
205 // Merge a list of AliCorrection objects with this (needed for
206 // PROOF).
207 // Returns the number of merged objects (including this).
208
209 if (!list)
210 return 0;
211
212 if (list->IsEmpty())
213 return 1;
214
215 TIterator* iter = list->MakeIterator();
216 TObject* obj;
217
218 Int_t count = 0;
219 while ((obj = iter->Next())) {
220 AliCFContainer* entry = dynamic_cast<AliCFContainer*> (obj);
221 if (entry == 0)
222 continue;
223 this->Add(entry);
224 count++;
225 }
226
227 return count+1;
228}
229
230//____________________________________________________________________
fb494025 231void AliCFContainer::Add(const AliCFContainer* aContainerToAdd, Double_t c)
563113d0 232{
233 //
234 //add the content of container aContainerToAdd to the current one
235 //
fb494025 236 if ((aContainerToAdd->GetNStep() != fNStep) ||
237 (aContainerToAdd->GetNVar() != GetNVar()) ||
238 (aContainerToAdd->GetNBinsTotal() != GetNBinsTotal()))
239 {
240 AliError("Different number of steps/sensitive variables/grid elements: cannot add the containers");
241 return;
242 }
243 for (Int_t istep=0; istep<fNStep; istep++) {
563113d0 244 fGrid[istep]->Add(aContainerToAdd->GetGrid(istep),c);
245 }
246}
247//____________________________________________________________________
fb494025 248Float_t AliCFContainer::GetOverFlows( Int_t ivar, Int_t istep, Bool_t exclusive) const {
563113d0 249 //
250 // Get overflows in variable var at selection level istep
fb494025 251 // Set 'exclusive' to true for an exclusive check on variable ivar
563113d0 252 //
a2255142 253 if(istep >= fNStep || istep < 0){
254 AliError("Non-existent selection step, return -1");
255 return -1.;
256 }
fb494025 257 return fGrid[istep]->GetOverFlows(ivar,exclusive);
563113d0 258}
259//____________________________________________________________________
fb494025 260Float_t AliCFContainer::GetUnderFlows( Int_t ivar, Int_t istep, Bool_t exclusive) const {
563113d0 261 //
a2255142 262 // Get underflows in variable var at selection level istep
fb494025 263 // Set 'exclusive' to true for an exclusive check on variable ivar
563113d0 264 //
a2255142 265 if(istep >= fNStep || istep < 0){
266 AliError("Non-existent selection step, return -1");
267 return -1.;
268 }
fb494025 269 return fGrid[istep]->GetUnderFlows(ivar,exclusive);
563113d0 270}
271//____________________________________________________________________
fb494025 272Float_t AliCFContainer::GetEntries(Int_t istep) const {
563113d0 273 //
a2255142 274 // Get total entries in variable var at selection level istep
563113d0 275 //
a2255142 276 if(istep >= fNStep || istep < 0){
277 AliError("Non-existent selection step, return -1");
278 return -1.;
279 }
563113d0 280 return fGrid[istep]->GetEntries();
281}
563113d0 282//_____________________________________________________________________
1e9dad92 283Double_t AliCFContainer::GetIntegral( Int_t istep) const
563113d0 284{
285 //
a2255142 286 // Get Integral over the grid at selection level istep
563113d0 287 //
a2255142 288 if(istep >= fNStep || istep < 0){
289 AliError("Non-existent selection step, return -1");
290 return -1.;
291 }
563113d0 292 return fGrid[istep]->GetIntegral();
293}
d91baff0 294
563113d0 295//_____________________________________________________________________
d91baff0 296void AliCFContainer::SetRangeUser(Int_t ivar, Double_t varMin, Double_t varMax, Bool_t useBins) const
c8df672e 297{
298 //
d91baff0 299 // set axis range for variable ivar
300 // put useBins=kTRUE if you want to pass bin numbers instead of values
c8df672e 301 //
fb494025 302 if (ivar >= GetNVar() || ivar < 0){
c8df672e 303 AliError("Non-existent selection var");
304 return ;
305 }
d91baff0 306 for (Int_t iStep=0; iStep<GetNStep(); iStep++) fGrid[iStep]->SetRangeUser(ivar,varMin,varMax,useBins);
9105291d 307}
308
309//_____________________________________________________________________
d91baff0 310void AliCFContainer::SetRangeUser(const Double_t* varMin, const Double_t* varMax, Bool_t useBins) const
9105291d 311{
312 //
d91baff0 313 // set all axis ranges according to arrays varMin and varMax
314 // put useBins=kTRUE if you want to pass bin numbers instead of values
9105291d 315 //
d91baff0 316 for (Int_t iStep=0; iStep<GetNStep(); iStep++) fGrid[iStep]->SetRangeUser(varMin,varMax,useBins);
c8df672e 317}
71b2614f 318
319//_____________________________________________________________________
320void AliCFContainer::Print(const Option_t*) const {
321 AliInfo("====================================================================================");
322 AliInfo(Form("AliCFContainer : name = %s title = %s",GetName(),GetTitle()));
323 AliInfo(Form("number of steps \t %d",GetNStep()));
324 for (Int_t iStep=0;iStep<GetNStep();iStep++) AliInfo(Form("step %d \t -> %s",iStep,GetStepTitle(iStep)));
325 AliInfo(Form("number of variables \t %d",GetNVar()));
326 for (Int_t iVar=0;iVar<GetNVar();iVar++) {
327 Double_t *binLimits = new Double_t[GetNBins(iVar)+1];
328 GetBinLimits(iVar,binLimits);
329 AliInfo(Form("variable %d \t -> %s : %d bins in [%f,%f]",iVar,GetVarTitle(iVar),GetNBins(iVar),binLimits[0],binLimits[GetNBins(iVar)]));
4ce766eb 330 delete[] binLimits;
71b2614f 331 }
332 AliInfo("====================================================================================");
333}