GammaWare  Head Version for release 0.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CorrelatedSpaceTree2.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_CorrelatedSpaceTree2
24 #include "CorrelatedSpaceTree2.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_TString
44 #include "TString.h"
45 #endif
46 
47 #ifndef ROOT_TTree
48 #include "TTree.h"
49 #endif
50 
51 
52 using namespace Gw;
53 
54 //
56 
57 //__________________________________________________________
58 CorrelatedSpaceTree2::CorrelatedSpaceTree2(const char* name, const char* title)
59  : SymCorrelatedSpace(name, title),
60  fTree(0x0),
61  fBranchName("mat"),
62  fLog("CorrelatedSpaceTree2")
63 {
64  // default constructor
65 
66  SetDimension(2);
67 }
68 
69 //__________________________________________________________
71 {
72  // default destructor
73  delete fTree;
74 }
75 
76 //__________________________________________________________
77 const Char_t* CorrelatedSpaceTree2::GetTreeName() const
78 {
79  return Form("%s_2D", GetName());
80 }
81 
82 //__________________________________________________________
84 {
85  return Form("%s_2D", GetTitle());
86 }
87 
88 //__________________________________________________________
90 {
91  fTree->Delete(opt);
92 }
93 
94 //__________________________________________________________
96 {
97 
98  if (fTree == 0x0) {
99  fTree = new TTree(GetTreeName(), GetTreeTitle());
100  }
101 }
102 
103 //__________________________________________________________
104 void CorrelatedSpaceTree2::Fill(const Double_t* Xn, const Double_t weight)
105 {
106 
107  AssertTree();
108 
109  Int_t dimX = GetHisto("x")->GetNbinsX();
110  SetNofCells(dimX*dimX);
111 
112  SetBranch();
113 
114  for (Int_t i = 0; i < dimX; ++i) {
115  for (Int_t j = 0; j < dimX; ++j) {
116  FillCounts(i, j, dimX, Xn, weight);
117  fTree->Fill();
118  }
119  }
120 }
121 
122 //__________________________________________________________
124 {
125 
126  fLog.SetProcessMethod("FillFromH2(TH2* )");
127 
128 
129  if (gFile == 0x0) {
130  fLog << error << "No file exists to write" << dolog;
131  return;
132  }
133 
134  CheckH2InTree(h2);
135 
136  AssertTree();
137 
138  Int_t dimX = h2->GetNbinsX();
139  SetNofCells(dimX*dimX);
140 
141  SetBins(dimX, 0, dimX, 0);
142 
143  SetBranch();
144 
145  for (Int_t i = 0; i < dimX; ++i) {
146  printf("%d\r", i);
147  for (Int_t j = 0; j < dimX; ++j) {
148  FillCounts(i,j,h2);
149  fTree->Fill();
150  }
151  }
152 
153  fLog << dolog;
154 }
155 
156 //__________________________________________________________
158 {
159  fLog.SetProcessMethod("FillRandom(Int_t )");
160 
161  AssertTree();
162 
163  SetBins(dim, 0, dim, 0);
164  SetNofCells(dim*dim);
165 
166  SetBranch();
167 
168  if (gFile == 0x0) {
169  fLog << error << "No file exists to write" << dolog;
170  return;
171  }
172 
173  for (Int_t i = 0; i < dim; ++i) {
174  printf("%d\r", i);
175  for (Int_t j = 0; j < dim; ++j) {
176  FillCounts(j);
177  fTree->Fill();
178  }
179  }
180 
181  fLog << dolog;
182 }
183 
184 //__________________________________________________________
186 {
187  fTree->Browse(b);
188 }
189 
190 //__________________________________________________________
191 void CorrelatedSpaceTree2::Project(CSFilter* filter, Option_t* axis)
192 {
193  fLog.SetProcessMethod("Project(CSFilter*, Option_t* )");
194 
195  if (fStatus == kUndefined) {
196  fLog << error << "Tree not defined" << dolog;
197  return;
198  }
199 
200  if (filter == 0x0) {
201  fLog << error << "No filter definied yet" << dolog;
202  return;
203  }
204 
205  filter->SetCondition(1);
206  filter->SetDimension(fDimension-1);
207 
208  Int_t idx = GetAxisNumber(axis);
209 
210  if (idx != 0) {
211  fLog << warning << "Axis number out of range" << dolog;
212  return;
213  }
214 
215 
216  TH1D* h1 = 0x0;
217  if ( filter->IsForBkg() )
218  h1 = static_cast<TH1D*> ( fHistoBkgList->At(idx) );
219  else
220  h1 = static_cast<TH1D*> ( fHistoList->At(idx) );
221 
222  h1->Reset();
223  Int_t dim = GetHistoTot("x")->GetNbinsX();
224 
225  SetBins(dim, 0, dim, idx);
226 
227  TBranch* b = SetAddress();
228 
229  Int_t nofGates;
230  Int_t dimProj = fDimension - 1;
231  Short_t inGate[dimProj];
232  Double_t energies[dimProj];
233 
234  for (Int_t i= 0; i < dim; ++i) {
235 
236  energies[0] = (Double_t)i;
237  nofGates = filter->IsInside(energies, dimProj, inGate);
238 
239  if (nofGates > 1) {
240  fLog << error << Form("Too much good energies : %d", nofGates) << dolog;
241  return;
242  }
243 
244  if (inGate[0] == 1) {
245  for (Int_t j = 0; j < dim; ++j) {
246  b->GetEntry(i*dim + j);
247  FillHisto(j, h1);
248  }
249  }
250  }
251  fLog << dolog;
252 }
253 
254 //_____________________________________________________________________________
255 void CorrelatedSpaceTree2::Project(Option_t* axis, Option_t* gateName)
256 {
257  CorrelatedSpace::Project(axis, gateName);
258 }
259 
260 //_____________________________________________________________________________
261 Int_t CorrelatedSpaceTree2::GetNbin(Int_t axis) const
262 {
263  switch (axis) {
264  case 0:
265  case 1:
266  return GetHistoTot("x")->GetNbinsX();
267  default:
268  return -1;
269  }
270 }
271 
272 //__________________________________________________________
273 void CorrelatedSpaceTree2::SetCellContent(Int_t iCell, Double_t weight)
274 {
275  AssertTree();
276  Int_t dim = GetHistoTot("x")->GetNbinsX();
277 
278  if (iCell >= dim*dim) {
279  fLog.SetProcessMethod("GetCellContent(Int_t )");
280  fLog << error << "index to large" << dolog;
281  return;
282  }
283 
284  static Bool_t first = false;
285 
286  if (first == false) {
287  SetBranch();
288  first = true;
289  }
290 
291  SetCounts(weight);
292 
293  Int_t i = iCell / (dim);
294  GetHistoTot("x")->Fill(i, weight);
295 
296  fTree->Fill();
297 }
298 
299 //__________________________________________________________
300 Double_t CorrelatedSpaceTree2::GetContent(Int_t i, Int_t j)
301 {
302  Int_t dim = GetHistoTot("x")->GetNbinsX();
303 
304  TBranch* b = SetAddress();
305 
306  b->GetEntry(i*dim + j);
307 
308  return GetCounts();
309 }
310 
311 //__________________________________________________________
313 {
314  Int_t dim = GetHistoTot("x")->GetNbinsX();
315 
316  TBranch* b = SetAddress();
317 
318  if (i >= dim*dim) {
319  fLog.SetProcessMethod("GetCellContent(Int_t )");
320  fLog << error << "index to large" << dolog;
321  return 0.;
322  }
323 
324  b->GetEntry(i);
325  return GetCounts();
326 }
327 
328 //_____________________________________________________________________________
329 void CorrelatedSpaceTree2::Streamer(TBuffer &R__b)
330 {
331  // Customized streamer
332  fLog.SetProcessMethod("Streamer(TBuffer& )");
333  if (R__b.IsReading()) {
334  CorrelatedSpaceTree2::Class()->ReadBuffer(R__b, this);
335  fTree = (TTree*)gFile->Get(GetTreeName());
336  if (fTree == 0x0) {
337  fLog << error << "Cannot file tree in file" << nline;
339  }
340  fStatus = kDefined;
341  } else {
342  fTree->Write("", kOverwrite);
343  CorrelatedSpaceTree2::Class()->WriteBuffer(R__b, this);
344  }
345  fLog << dolog;
346 }
347 
348 
349 
void SetCellContent(Int_t iCell, Double_t weight)
Set cell content.
virtual void SetBranch()=0
Set Branch.
TBrowser * b
CorrelatedSpaceTree2 class that manages a 2D symmetrical correlated space stored in a tree In the ent...
void FillFromH2(TH2 *h2)
fill correlated space from H2
printf("******************************************************************** \n")
Bool_t IsForBkg()
Is for bkg gates.
Definition: CSFilter.h:118
header file for all type of CorrelatedSpaceTree2
LogMessage & error(LogMessage &)
LogMessage & warning(LogMessage &)
SymCorrelatedSpace base class that contains correlated informations.
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
LogMessage & nline(LogMessage &)
virtual void Browse(TBrowser *b)
Browser method.
virtual void FillHisto(Int_t j, TH1 *h)=0
Fill histo.
void SetNofCells(Int_t nCells)
Set number of cells.
void FillRandom(Int_t dim)
fill random
Int_t GetNbin(Int_t axis) const
Get Bin.
virtual Double_t GetCounts()=0
Get counts.
LogMessage & dolog(LogMessage &)
virtual void FillCounts(Int_t i, Int_t j, TH2 *h2)=0
fill counts from h2
CSFilter base class for filter.
Definition: CSFilter.h:56
virtual void SetCounts(Double_t weight)=0
Set counts.
const Char_t * GetTreeTitle() const
Get Tree title.
virtual void CheckH2InTree(TH2 *h2)=0
Check if h2 already in tree.
Double_t GetCellContent(Int_t iCell)
Get cell content.
virtual TH1D * GetHistoTot(Option_t *axis="x")
Get histo total projection.
void SetCondition(Short_t cond)
Set dimension for gating.
Definition: CSFilter.h:109
void DeleteTree(Option_t *opt="")
Reset Tree.
virtual void Project(CSFilter *, Option_t *)=0
project
ClassImp(CorrelatedSpaceTree2) CorrelatedSpaceTree2
virtual void SetProcessMethod(const char *)
To set the current method.
void Project(CSFilter *filter, Option_t *axis="X")
Project.
const Char_t * GetTreeName() const
Get Tree name.
void Fill(const Double_t *Xn, const Double_t weight=1.)
fill correlated space
void SetDimension(Short_t dim)
Set dimension for projected space.
Definition: CSFilter.h:115
virtual TBranch * SetAddress()=0
Set address.
virtual TH1D * GetHisto(Option_t *axis="x")
Get histo.
Double_t GetContent(Int_t i, Int_t j)
Get content.
Int_t dim
Definition: ReadMiniTree3.C:34
virtual Int_t GetAxisNumber(Option_t *name) const
list of Correlated spaces