GammaWare  Head Version for release 0.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CorrelatedSpaceTH2.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2004-2006 by Olivier Stezowski & Christian Finck *
3  * stezow(AT)ipnl.in2p3.fr, cfinck(AT)ires.in2p3.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
23 #ifndef Gw_CorrelatedSpaceTH2
24 #include "CorrelatedSpaceTH2.h"
25 #endif
26 
27 #ifndef Gw_CSFilter
28 #include "CSFilter.h"
29 #endif
30 
31 #ifndef ROOT_TBrowser
32 #include "TBrowser.h"
33 #endif
34 
35 #ifndef ROOT_TFile
36 #include "TFile.h"
37 #endif
38 
39 #ifndef ROOT_TH2
40 #include "TH2.h"
41 #endif
42 
43 #ifndef ROOT_TROOT
44 #include "TROOT.h"
45 #endif
46 
47 #ifndef ROOT_TRandom
48 #include "TRandom.h"
49 #endif
50 
51 #ifndef ROOT_TString
52 #include "TString.h"
53 #endif
54 
55 #ifndef ROOT_TTree
56 #include "TTree.h"
57 #endif
58 
59 #ifndef ROOT_TVirtualPad
60 #include "TVirtualPad.h"
61 #endif
62 
63 
64 using namespace Gw;
65 
66 //
68 
69 //__________________________________________________________
70 CorrelatedSpaceTH2::CorrelatedSpaceTH2(const char* name, const char* title, Option_t* type)
71  : ASymCorrelatedSpace(name, title),
72  fHisto(0x0),
73  fAdopted(false),
74  fLog("CorrelatedSpaceTH2")
75 {
76  // default constructor
77 
78  SetDimension(2);
79  TString sType(type);
80  sType.ToLower();
81  if (sType.Contains("float"))
82  fType = 0;
83  else if (sType.Contains("short"))
84  fType = 1;
85  else if (sType.Contains("int"))
86  fType = 2;
87  else if (sType.Contains("double"))
88  fType = 3;
89  else {
90  fLog.SetProcessMethod("CorrelatedSpaceTH2(Option_t* )");
91  fLog << warning << "Unkwon type, set to float" << dolog;
92  fType = 0;
93  }
94 }
95 
96 //__________________________________________________________
98 {
99  // default destructor
100  if (!fAdopted)
101  delete fHisto;
102 }
103 
104 //__________________________________________________________
105 const Char_t* CorrelatedSpaceTH2::GetHistoName() const
106 {
107  return Form("%s_H2", GetName());
108 }
109 
110 //__________________________________________________________
112 {
113  return Form("%s_H2", GetTitle());
114 }
115 
116 //__________________________________________________________
117 Double_t CorrelatedSpaceTH2::GetContent(Int_t i, Int_t j)
118 {
119 
120  if (fHisto == 0x0) {
121  fLog.SetProcessMethod("GetContent(Int_t, Int_t )");
122  fLog << error << "Histo TH2 not definied" << dolog;
123  return 0.;
124  }
125 
126  return (Double_t)fHisto->GetBinContent(fHisto->GetBin(i,j));
127 }
128 
129 //__________________________________________________________
131 {
132  if (fHisto == 0x0) {
133  fLog.SetProcessMethod("GetCellContent(Int_t )");
134  fLog << error << "Histo TH2 not definied" << dolog;
135  return 0.;
136  }
137 
138  Int_t dimY = fHisto->GetNbinsY();
139 
140  Int_t j = iCell / dimY;
141  Int_t i = iCell % dimY;
142 
143  return GetContent(i, j);
144 }
145 
146 //__________________________________________________________
147 Int_t CorrelatedSpaceTH2::GetNbin(Int_t axis) const
148 {
149  if (fHisto == 0x0) {
150  fLog.SetProcessMethod("GetNbin(Int_t )");
151  fLog << error << "Histo TH2 not definied" << dolog;
152  return -1;
153  }
154 
155  switch (axis) {
156  case 0:
157  return fHisto->GetNbinsX();
158  case 1:
159  return fHisto->GetNbinsY();
160  default:
161  return -1;
162  }
163 }
164 
165 //__________________________________________________________
166 void CorrelatedSpaceTH2::SetCellContent(Int_t iCell, Double_t weight)
167 {
168  AssertHisto();
169  Int_t dimY = GetHisto("y")->GetNbinsX();
170 
171  Int_t j = iCell / (dimY);
172  Int_t i = iCell % (dimY);
173 
174  fHisto->Fill(i, j, weight);
175 
176  GetHistoTot("x")->Fill(j, weight);
177  GetHistoTot("y")->Fill(i, weight);
178 }
179 
180 //__________________________________________________________
182 {
183  if (fHisto == 0x0) {
184  switch (fType) {
185  case 0:
186  fHisto = new TH2F();
187  break;
188  case 1:
189  fHisto = new TH2S();
190  break;
191  case 2:
192  fHisto = new TH2I();
193  break;
194  case 3:
195  fHisto = new TH2D();
196  break;
197  }
198  fHisto->SetName(GetHistoName());
199  fHisto->SetTitle(GetHistoTitle());
200  Int_t dimX = GetHisto("x")->GetNbinsX();
201  Int_t dimY = GetHisto("y")->GetNbinsX();
202  Float_t minX = GetHisto("x")->GetXaxis()->GetXmin();
203  Float_t maxX = GetHisto("x")->GetXaxis()->GetXmax();
204  Float_t minY = GetHisto("y")->GetXaxis()->GetXmin();
205  Float_t maxY = GetHisto("y")->GetXaxis()->GetXmax();
206  fHisto->SetBins(dimX, minX, maxX, dimY, minY, maxY);
207  }
208 }
209 
210 //__________________________________________________________
211 void CorrelatedSpaceTH2::Fill(const Double_t* Xn, const Double_t weight)
212 {
213 
214  AssertHisto();
215 
216  Int_t dimX = GetHisto("x")->GetNbinsX();
217  Int_t dimY = GetHisto("y")->GetNbinsX();
218 
219 
220  fHisto->SetBins(dimX, 0, dimX, dimY, 0, dimY);
221  SetNofCells(dimX*dimY);
222 
223  for (Int_t i = 0; i < dimX; ++i) {
224  for (Int_t j = 0; j < dimY; ++j) {
225  fHisto->Fill(i,j, Xn[i*dimX + j]*weight);
226  }
227  }
228 
229  TH1D* h1x = fHisto->ProjectionX();
230  TH1D* h1y = fHisto->ProjectionY();
231 
232  for (Int_t i= 0; i < dimX; ++i)
233  GetHistoTot("x")->Fill(i, h1x->GetBinContent(i));
234 
235  for (Int_t j = 0; j < dimY; ++j)
236  GetHistoTot("y")->Fill(j, h1y->GetBinContent(j));
237 }
238 
239 //__________________________________________________________
241 {
242 
243  fLog.SetProcessMethod("FillFromH2(TH2* )");
244 
245  if (gFile == 0x0) {
246  fLog << error << "No file exists to write" << dolog;
247  return;
248  }
249 
250  Int_t dimX = h2->GetNbinsX();
251  Int_t dimY = h2->GetNbinsY();
252  Float_t minX = h2->GetXaxis()->GetXmin(); // modif des 4 lignes
253  Float_t maxX = h2->GetXaxis()->GetXmax();
254  Float_t minY = h2->GetXaxis()->GetXmin();
255  Float_t maxY = h2->GetXaxis()->GetXmax();
256 
257  SetBins(dimX, minX, maxX, 0);
258  SetBins(dimY, minY, maxY, 1);
259 
260  SetNofCells(dimX*dimY);
261 
262  fHisto = (TH2*)h2->Clone();
263  TH1D* h1x = h2->ProjectionX();
264  TH1D* h1y = h2->ProjectionY();
265 
266  for (Int_t i= 0; i < dimX; ++i)
267  GetHistoTot("x")->SetBinContent(i, h1x->GetBinContent(i));
268 
269  for (Int_t j = 0; j < dimY; ++j)
270  GetHistoTot("y")->SetBinContent(j, h1y->GetBinContent(j));
271 
272  fHisto->SetName(GetHistoName());
273  fHisto->SetTitle(GetHistoTitle());
274 
275  fLog << dolog;
276 }
277 
278 
279 //__________________________________________________________
280 void CorrelatedSpaceTH2::FillRandom(Int_t dimX, Int_t dimY)
281 {
282  fLog.SetProcessMethod("FillRandom(Int_t )");
283 
284  AssertHisto();
285 
286  SetBins(dimX, 0, dimX, 0);
287  SetBins(dimY, 0, dimY, 1);
288 
289  fHisto->SetBins(dimX, 0, dimX, dimY, 0, dimY);
290 
291  SetNofCells(dimX*dimY);
292 
293  if (gFile == 0x0) {
294  fLog << error << "No file exists to write" << dolog;
295  return;
296  }
297 
298  Double_t counts = 0;
299  for (Int_t i = 0; i < dimX; ++i) {
300  printf("%d\r", i);
301  for (Int_t j = 0; j < dimY; ++j) {
302  counts = gRandom->Uniform(0, 10);
303  fHisto->Fill(i,j, counts);
304  }
305  }
306 
307  TH1D* h1x = fHisto->ProjectionX();
308  TH1D* h1y = fHisto->ProjectionY();
309 
310  for (Int_t i= 0; i < dimX; ++i)
311  GetHistoTot("x")->Fill(i, h1x->GetBinContent(i));
312 
313  for (Int_t j = 0; j < dimY; ++j)
314  GetHistoTot("y")->Fill(j, h1y->GetBinContent(j));
315 
316  fLog << dolog;
317 }
318 
319 //__________________________________________________________
321 {
322  if (fHisto) delete fHisto;
323  fHisto = h2;
324 
325  Int_t dimX = h2->GetNbinsX();
326  Float_t minX = h2->GetXaxis()->GetXmin();
327  Float_t maxX = h2->GetXaxis()->GetXmax();
328  SetBins(dimX, minX, maxX, 0);
329 
330  Int_t dimY = h2->GetNbinsY();
331  Float_t minY = h2->GetYaxis()->GetXmin();
332  Float_t maxY = h2->GetYaxis()->GetXmax();
333  SetBins(dimY, minY, maxY, 1);
334 
335  SetNofCells(dimX*dimY);
336 
337  TH1D* h1x = fHisto->ProjectionX();
338  TH1D* h1y = fHisto->ProjectionY();
339  h1x->SetTitle("");
340  h1y->SetTitle("");
341 
342  for (Int_t i= 0; i < dimX; ++i)
343  GetHistoTot("x")->SetBinContent(i, h1x->GetBinContent(i));
344 
345  for (Int_t j = 0; j < dimY; ++j)
346  GetHistoTot("y")->SetBinContent(j, h1y->GetBinContent(j));
347 
348  fAdopted = true;
349 }
350 
351 //__________________________________________________________
352 void CorrelatedSpaceTH2::Project(CSFilter* filter, Option_t* axis)
353 {
354  fLog.SetProcessMethod("Project(CSFilter*, Option_t* )");
355 
356  if (fStatus == kUndefined) {
357  fLog << error << "TH2 not defined" << dolog;
358  return;
359  }
360 
361  if (filter == 0x0) {
362  fLog << error << "No filter definied yet" << dolog;
363  return;
364  }
365 
366  filter->SetCondition(1);
367  filter->SetDimension(fDimension-1);
368 
369  Int_t idx = GetAxisNumber(axis);
370 
371  if (idx > 1) {
372  fLog << warning << "Axis number out of range" << dolog;
373  return;
374  }
375 
376  TH1D* h1 = 0x0;
377  TH1D* hProj1 = 0x0;
378  TH1D* hProj2 = 0x0;
379 
380  if ( filter->IsForBkg() )
381  h1 = static_cast<TH1D*> ( fHistoBkgList->At(idx) );
382  else
383  h1 = static_cast<TH1D*> ( fHistoList->At(idx) );
384 
385  h1->Reset();
386 
387  Int_t dim1 = 0;
388  Int_t dim2 = 0;
389  Double_t counts = 0.;
390 
391  if (idx == 0) {
392  dim1 = GetHistoTot("x")->GetNbinsX();
393  dim2 = GetHistoTot("y")->GetNbinsX();
394  hProj1 = GetHistoTot("x");
395  hProj2 = GetHistoTot("y");
396  } else {
397  dim2 = GetHistoTot("x")->GetNbinsX();
398  dim1 = GetHistoTot("y")->GetNbinsX();
399  hProj2 = GetHistoTot("x");
400  hProj1 = GetHistoTot("y");
401  }
402 
403  Int_t nofGates;
404  Int_t dimProj = fDimension - 1;
405  Short_t inGate[dimProj];
406  Double_t energies[dimProj];
407 
408  for (Int_t i= 0; i < dim1; ++i) {
409 
410  energies[0] = hProj1->GetBinCenter(i);
411 
412  nofGates = filter->IsInside(energies, dimProj, inGate);
413 
414  if (nofGates > 1) {
415  fLog << error << Form("Too much good energies : %d", nofGates) << dolog;
416  return;
417  }
418 
419  if (inGate[0] == 1) {
420  for (Int_t j = 0; j < dim2; ++j) {
421  if (idx == 0)
422  counts = (Double_t)fHisto->GetBinContent(fHisto->GetBin(i,j));
423  else
424  counts = (Double_t)fHisto->GetBinContent(fHisto->GetBin(j,i));
425  h1->Fill(hProj2->GetBinCenter(j), counts);
426  }
427  }
428  }
429  fLog << dolog;
430 }
431 
432 //_____________________________________________________________________________
433 void CorrelatedSpaceTH2::Project(Option_t* axis, Option_t* gateName)
434 {
435  CorrelatedSpace::Project(axis, gateName);
436 }
437 
438 //______________________________________________________________________________
440 {
441  fHisto->Browse(b);
442 }
const Char_t * GetHistoName() const
Get histo name.
TBrowser * b
printf("******************************************************************** \n")
void Adopt(TH2 *h2)
Adopt TH2, do not delete the matrix when destroying CS !
Bool_t IsForBkg()
Is for bkg gates.
Definition: CSFilter.h:118
CorrelatedSpaceTH2 class that manages a 2D symmetrical correlated space stored in a TH2...
LogMessage & error(LogMessage &)
LogMessage & warning(LogMessage &)
virtual void SetBins(Int_t bin, Double_t min, Double_t max, Int_t axis=0)
Set bin for each axis.
virtual Int_t IsInside(Double_t *, Int_t, Short_t *, Int_t=0)
Get the pattern of energies inside the gates.
Definition: CSFilter.h:88
header file for all type of CorrelatedSpaceTH2
const Char_t * GetHistoTitle() const
Get histo title.
void SetNofCells(Int_t nCells)
Set number of cells.
virtual void Browse(TBrowser *b)
Browser method.
void FillRandom(Int_t dimX, Int_t dimY)
fill random
virtual Int_t GetAxisNumber(Option_t *name) const
list of Correlated spaces
ASymCorrelatedSpace base class that contains correlated informations.
LogMessage & dolog(LogMessage &)
CSFilter base class for filter.
Definition: CSFilter.h:56
void FillFromH2(TH2 *h2)
fill correlated space from H2
ClassImp(CorrelatedSpaceTH2) CorrelatedSpaceTH2
virtual TH1D * GetHistoTot(Option_t *axis="x")
Get histo total projection.
void SetCondition(Short_t cond)
Set dimension for gating.
Definition: CSFilter.h:109
virtual void Project(CSFilter *, Option_t *)=0
project
Double_t GetContent(Int_t i, Int_t j)
Get content.
virtual void SetProcessMethod(const char *)
To set the current method.
Int_t GetNbin(Int_t axis) const
Get bin.
void SetDimension(Short_t dim)
Set dimension for projected space.
Definition: CSFilter.h:115
void Project(CSFilter *filter, Option_t *axis="X")
Project.
virtual TH1D * GetHisto(Option_t *axis="x")
Get histo.
void SetCellContent(Int_t iCell, Double_t weight)
Set cell content.
Double_t GetCellContent(Int_t iCell)
Get cell content.
void Fill(const Double_t *Xn, const Double_t weight=1.)
fill correlated space