Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
Sample.cxx
Go to the documentation of this file.
1 // @(#)root/roostats:$Id$
2 // Author: Kyle Cranmer, George Lewis
3 /*************************************************************************
4  * Copyright (C) 1995-2008, Rene Brun and Fons Rademakers. *
5  * All rights reserved. *
6  * *
7  * For the licensing terms see $ROOTSYS/LICENSE. *
8  * For the list of contributors see $ROOTSYS/README/CREDITS. *
9  *************************************************************************/
10 
11 //////////////////////////////////////////////////////////////////////////////
12 /** \class RooStats::HistFactory::Sample
13  * \ingroup HistFactory
14  */
15 
16 #include "TH1.h"
19 
20 //#include "TClass.h"
21 
22 RooStats::HistFactory::Sample::Sample() :
23  fNormalizeByTheory(false), fStatErrorActivate(false), fhNominal(), fhCountingHist(0) { ; }
24 
25 // copy constructor (important for python)
26 RooStats::HistFactory::Sample::Sample(const Sample& other) :
27  fName(other.fName), fInputFile(other.fInputFile),
28  fHistoName(other.fHistoName), fHistoPath(other.fHistoPath),
29  fChannelName(other.fChannelName),
30 
31  fOverallSysList(other.fOverallSysList),
32  fNormFactorList(other.fNormFactorList),
33  fHistoSysList(other.fHistoSysList),
34  fHistoFactorList(other.fHistoFactorList),
35  fShapeSysList(other.fShapeSysList),
36  fShapeFactorList(other.fShapeFactorList),
37 
38  fStatError(other.fStatError),
39  fNormalizeByTheory(other.fNormalizeByTheory),
40  fStatErrorActivate(other.fStatErrorActivate),
41  fhNominal(other.fhNominal),
42  fhCountingHist(0)
43  {
44  if( other.fhCountingHist ) {
45  SetValue( other.fhCountingHist->GetBinContent(1) );
46  }else{
47  fhCountingHist = NULL;
48  }
49  }
50 
51 RooStats::HistFactory::Sample& RooStats::HistFactory::Sample::operator=(const Sample& other)
52 {
53  fName = other.fName; fInputFile = other.fInputFile;
54  fHistoName = other.fHistoName; fHistoPath = other.fHistoPath;
55  fChannelName = other.fChannelName;
56 
57  fOverallSysList = other.fOverallSysList;
58  fNormFactorList = other.fNormFactorList;
59  fHistoSysList = other.fHistoSysList;
60  fHistoFactorList = other.fHistoFactorList;
61  fShapeSysList = other.fShapeSysList;
62  fShapeFactorList = other.fShapeFactorList;
63 
64  fStatError = other.fStatError;
65  fNormalizeByTheory = other.fNormalizeByTheory;
66  fStatErrorActivate = other.fStatErrorActivate;
67  fhNominal = other.fhNominal;
68 
69  if (fhCountingHist)
70  delete fhCountingHist;
71 
72  if( other.fhCountingHist ) {
73  SetValue( other.fhCountingHist->GetBinContent(1) );
74  } else {
75  fhCountingHist = NULL;
76  }
77 
78  return *this;
79 }
80 
81 
82 RooStats::HistFactory::Sample::Sample(std::string SampName, std::string SampHistoName, std::string SampInputFile, std::string SampHistoPath) :
83  fName( SampName ), fInputFile( SampInputFile),
84  fHistoName( SampHistoName ), fHistoPath( SampHistoPath ),
85  fNormalizeByTheory(true), fStatErrorActivate(false), fhNominal(),
86  fhCountingHist(0) { ; }
87 
88 RooStats::HistFactory::Sample::Sample(std::string SampName) :
89  fName( SampName ), fInputFile( "" ),
90  fHistoName( "" ), fHistoPath( "" ),
91  fNormalizeByTheory(true), fStatErrorActivate(false),fhNominal(),
92  fhCountingHist(0) { ; }
93 
94 RooStats::HistFactory::Sample::~Sample() {
95  if(fhCountingHist)
96  delete fhCountingHist;
97 }
98 
99 const TH1* RooStats::HistFactory::Sample::GetHisto() const {
100  TH1* histo = (TH1*) fhNominal.GetObject();
101  return histo;
102 }
103 
104 
105 void RooStats::HistFactory::Sample::writeToFile( std::string OutputFileName, std::string DirName ) {
106 
107  const TH1* histNominal = GetHisto();
108  histNominal->Write();
109 
110  // Set the location of the data
111  // in the output measurement
112 
113  fInputFile = OutputFileName;
114  fHistoName = histNominal->GetName();
115  fHistoPath = DirName;
116 
117  // Write this sample's StatError
118  GetStatError().writeToFile( OutputFileName, DirName );
119 
120  // Must write all systematics that contain internal histograms
121  // (This is not all systematics)
122  for( unsigned int i = 0; i < GetHistoSysList().size(); ++i ) {
123  GetHistoSysList().at(i).writeToFile( OutputFileName, DirName );
124  }
125  for( unsigned int i = 0; i < GetHistoFactorList().size(); ++i ) {
126  GetHistoFactorList().at(i).writeToFile( OutputFileName, DirName );
127  }
128  for( unsigned int i = 0; i < GetShapeSysList().size(); ++i ) {
129  GetShapeSysList().at(i).writeToFile( OutputFileName, DirName );
130  }
131  for( unsigned int i = 0; i < GetShapeFactorList().size(); ++i ) {
132  GetShapeFactorList().at(i).writeToFile( OutputFileName, DirName );
133  }
134 
135  return;
136 
137 }
138 
139 
140 void RooStats::HistFactory::Sample::SetValue( Double_t val ) {
141 
142  // For use in a number counting measurement
143  // Create a 1-bin histogram,
144  // fill it with this input value,
145  // and set this Sample's histogram to that hist
146 
147  std::string SampleHistName = fName + "_hist";
148 
149  // Histogram has 1-bin (hard-coded)
150  if(fhCountingHist)
151  delete fhCountingHist;
152 
153  fhCountingHist = new TH1F( SampleHistName.c_str(), SampleHistName.c_str(), 1, 0, 1 );
154  fhCountingHist->SetBinContent( 1, val );
155 
156  // Set the histogram of the internally held data
157  // node of this channel to this newly created histogram
158  SetHisto( fhCountingHist );
159 
160 }
161 
162 
163 
164 void RooStats::HistFactory::Sample::Print( std::ostream& stream ) const {
165 
166 
167  stream << "\t \t Name: " << fName
168  << "\t \t Channel: " << fChannelName
169  << "\t NormalizeByTheory: " << (fNormalizeByTheory ? "True" : "False")
170  << "\t StatErrorActivate: " << (fStatErrorActivate ? "True" : "False")
171  << std::endl;
172 
173  stream << "\t \t \t \t "
174  << "\t InputFile: " << fInputFile
175  << "\t HistName: " << fHistoName
176  << "\t HistoPath: " << fHistoPath
177  << "\t HistoAddress: " << GetHisto()
178  // << "\t Type: " << GetHisto()->ClassName()
179  << std::endl;
180 
181  if( fStatError.GetActivate() ) {
182  stream << "\t \t \t StatError Activate: " << fStatError.GetActivate()
183  << "\t InputFile: " << fInputFile
184  << "\t HistName: " << fStatError.GetHistoName()
185  << "\t HistoPath: " << fStatError.GetHistoPath()
186  << "\t HistoAddress: " << fStatError.GetErrorHist()
187  << std::endl;
188  }
189 
190 
191  /*
192  stream<< " NormalizeByTheory: ";
193  if(NormalizeByTheory) stream << "True";
194  else stream << "False";
195 
196  stream<< " StatErrorActivate: ";
197  if(StatErrorActivate) stream << "True";
198  else stream << "False";
199  */
200 
201 
202 }
203 
204 void RooStats::HistFactory::Sample::PrintXML( std::ofstream& xml ) {
205 
206 
207  // Create the sample tag
208  xml << " <Sample Name=\"" << fName << "\" "
209  << " HistoPath=\"" << fHistoPath << "\" "
210  << " HistoName=\"" << fHistoName << "\" "
211  << " InputFile=\"" << fInputFile << "\" "
212  << " NormalizeByTheory=\"" << (fNormalizeByTheory ? std::string("True") : std::string("False")) << "\" "
213  << ">" << std::endl;
214 
215 
216  // Print Stat Error (if necessary)
217  fStatError.PrintXML( xml );
218  /*
219  if( fStatError.GetActivate() ) {
220  xml << " <StatError Activate=\"" << (fStatError.GetActivate() ? std::string("True") : std::string("False")) << "\" "
221  << " InputFile=\"" << fStatError.GetInputFile() << "\" "
222  << " HistoName=\"" << fStatError.GetHistoName() << "\" "
223  << " HistoPath=\"" << fStatError.GetHistoPath() << "\" "
224  << " /> " << std::endl;
225  }
226  */
227 
228 
229  // Now, print the systematics:
230  for( unsigned int i = 0; i < fOverallSysList.size(); ++i ) {
231  RooStats::HistFactory::OverallSys sys = fOverallSysList.at(i);
232  sys.PrintXML(xml);
233  /*
234  xml << " <OverallSys Name=\"" << sys.GetName() << "\" "
235  << " High=\"" << sys.GetHigh() << "\" "
236  << " Low=\"" << sys.GetLow() << "\" "
237  << " /> " << std::endl;
238  */
239  }
240  for( unsigned int i = 0; i < fNormFactorList.size(); ++i ) {
241  RooStats::HistFactory::NormFactor sys = fNormFactorList.at(i);
242  sys.PrintXML(xml);
243  /*
244  xml << " <NormFactor Name=\"" << sys.GetName() << "\" "
245  << " Val=\"" << sys.GetVal() << "\" "
246  << " High=\"" << sys.GetHigh() << "\" "
247  << " Low=\"" << sys.GetLow() << "\" "
248  << " Const=\"" << (sys.GetConst() ? std::string("True") : std::string("False")) << "\" "
249  << " /> " << std::endl;
250  */
251  }
252  for( unsigned int i = 0; i < fHistoSysList.size(); ++i ) {
253  RooStats::HistFactory::HistoSys sys = fHistoSysList.at(i);
254  sys.PrintXML(xml);
255  /*
256  xml << " <HistoSys Name=\"" << sys.GetName() << "\" "
257 
258  << " InputFileLow=\"" << sys.GetInputFileLow() << "\" "
259  << " HistoNameLow=\"" << sys.GetHistoNameLow() << "\" "
260  << " HistoPathLow=\"" << sys.GetHistoPathLow() << "\" "
261 
262  << " InputFileHigh=\"" << sys.GetInputFileHigh() << "\" "
263  << " HistoNameHigh=\"" << sys.GetHistoNameHigh() << "\" "
264  << " HistoPathHigh=\"" << sys.GetHistoPathHigh() << "\" "
265  << " /> " << std::endl;
266  */
267  }
268  for( unsigned int i = 0; i < fHistoFactorList.size(); ++i ) {
269  RooStats::HistFactory::HistoFactor sys = fHistoFactorList.at(i);
270  sys.PrintXML(xml);
271  /*
272  xml << " <HistoFactor Name=\"" << sys.GetName() << "\" "
273 
274  << " InputFileLow=\"" << sys.GetInputFileLow() << "\" "
275  << " HistoNameLow=\"" << sys.GetHistoNameLow() << "\" "
276  << " HistoPathLow=\"" << sys.GetHistoPathLow() << "\" "
277 
278  << " InputFileHigh=\"" << sys.GetInputFileHigh() << "\" "
279  << " HistoNameHigh=\"" << sys.GetHistoNameHigh() << "\" "
280  << " HistoPathHigh=\"" << sys.GetHistoPathHigh() << "\" "
281  << " /> " << std::endl;
282  */
283  }
284  for( unsigned int i = 0; i < fShapeSysList.size(); ++i ) {
285  RooStats::HistFactory::ShapeSys sys = fShapeSysList.at(i);
286  sys.PrintXML(xml);
287  /*
288  xml << " <ShapeSys Name=\"" << sys.GetName() << "\" "
289 
290  << " InputFile=\"" << sys.GetInputFile() << "\" "
291  << " HistoName=\"" << sys.GetHistoName() << "\" "
292  << " HistoPath=\"" << sys.GetHistoPath() << "\" "
293  << " ConstraintType=\"" << std::string(Constraint::Name(sys.GetConstraintType())) << "\" "
294  << " /> " << std::endl;
295  */
296  }
297  for( unsigned int i = 0; i < fShapeFactorList.size(); ++i ) {
298  RooStats::HistFactory::ShapeFactor sys = fShapeFactorList.at(i);
299  sys.PrintXML(xml);
300  /*
301  xml << " <ShapeFactor Name=\"" << sys.GetName() << "\" "
302  << " /> " << std::endl;
303  */
304  }
305 
306  // Finally, close the tag
307  xml << " </Sample>" << std::endl;
308 
309 }
310 
311 
312 
313 // Some helper functions
314 // (Not strictly necessary because
315 // methods are publicly accessable)
316 
317 
318 void RooStats::HistFactory::Sample::ActivateStatError() {
319 
320  fStatError.Activate( true );
321  fStatError.SetUseHisto( false );
322 
323 }
324 
325 
326 void RooStats::HistFactory::Sample::ActivateStatError( std::string StatHistoName, std::string StatInputFile, std::string StatHistoPath ) {
327 
328 
329  fStatError.Activate( true );
330  fStatError.SetUseHisto( true );
331 
332  fStatError.SetInputFile( StatInputFile );
333  fStatError.SetHistoName( StatHistoName );
334  fStatError.SetHistoPath( StatHistoPath );
335 
336 }
337 
338 
339 void RooStats::HistFactory::Sample::AddOverallSys( std::string SysName, Double_t SysLow, Double_t SysHigh ) {
340 
341  RooStats::HistFactory::OverallSys sys;
342  sys.SetName( SysName );
343  sys.SetLow( SysLow );
344  sys.SetHigh( SysHigh );
345 
346  fOverallSysList.push_back( sys );
347 
348 }
349 
350 void RooStats::HistFactory::Sample::AddOverallSys( const OverallSys& Sys ) {
351  fOverallSysList.push_back(Sys);
352 }
353 
354 void RooStats::HistFactory::Sample::AddNormFactor( std::string SysName, Double_t SysVal, Double_t SysLow, Double_t SysHigh, bool SysConst ) {
355 
356  RooStats::HistFactory::NormFactor norm;
357 
358  norm.SetName( SysName );
359  norm.SetVal( SysVal );
360  norm.SetLow( SysLow );
361  norm.SetHigh( SysHigh );
362  norm.SetConst( SysConst );
363 
364  fNormFactorList.push_back( norm );
365 
366 }
367 
368 void RooStats::HistFactory::Sample::AddNormFactor( const NormFactor& Factor ) {
369  fNormFactorList.push_back( Factor );
370 }
371 
372 
373 void RooStats::HistFactory::Sample::AddHistoSys( std::string SysName,
374 std::string SysHistoNameLow, std::string SysHistoFileLow, std::string SysHistoPathLow,
375  std::string SysHistoNameHigh, std::string SysHistoFileHigh, std::string SysHistoPathHigh ) {
376 
377  RooStats::HistFactory::HistoSys sys;
378  sys.SetName( SysName );
379 
380  sys.SetHistoNameLow( SysHistoNameLow );
381  sys.SetHistoPathLow( SysHistoPathLow );
382  sys.SetInputFileLow( SysHistoFileLow );
383 
384  sys.SetHistoNameHigh( SysHistoNameHigh );
385  sys.SetHistoPathHigh( SysHistoPathHigh );
386  sys.SetInputFileHigh( SysHistoFileHigh );
387 
388  fHistoSysList.push_back( sys );
389 
390 }
391 
392 void RooStats::HistFactory::Sample::AddHistoSys( const HistoSys& Sys ) {
393  fHistoSysList.push_back( Sys );
394 }
395 
396 
397 void RooStats::HistFactory::Sample::AddHistoFactor( std::string SysName, std::string SysHistoNameLow, std::string SysHistoFileLow, std::string SysHistoPathLow,
398  std::string SysHistoNameHigh, std::string SysHistoFileHigh, std::string SysHistoPathHigh ) {
399 
400  RooStats::HistFactory::HistoFactor factor;
401  factor.SetName( SysName );
402 
403  factor.SetHistoNameLow( SysHistoNameLow );
404  factor.SetHistoPathLow( SysHistoPathLow );
405  factor.SetInputFileLow( SysHistoFileLow );
406 
407  factor.SetHistoNameHigh( SysHistoNameHigh );
408  factor.SetHistoPathHigh( SysHistoPathHigh );
409  factor.SetInputFileHigh( SysHistoFileHigh );
410 
411  fHistoFactorList.push_back( factor );
412 
413 }
414 
415 void RooStats::HistFactory::Sample::AddHistoFactor( const HistoFactor& Factor ) {
416  fHistoFactorList.push_back(Factor);
417 }
418 
419 
420 void RooStats::HistFactory::Sample::AddShapeFactor( std::string SysName ) {
421 
422  RooStats::HistFactory::ShapeFactor factor;
423  factor.SetName( SysName );
424  fShapeFactorList.push_back( factor );
425 
426 }
427 
428 
429 void RooStats::HistFactory::Sample::AddShapeFactor( const ShapeFactor& Factor ) {
430  fShapeFactorList.push_back(Factor);
431 }
432 
433 
434 void RooStats::HistFactory::Sample::AddShapeSys( std::string SysName, Constraint::Type SysConstraintType, std::string SysHistoName, std::string SysHistoFile, std::string SysHistoPath ) {
435 
436  RooStats::HistFactory::ShapeSys sys;
437  sys.SetName( SysName );
438  sys.SetConstraintType( SysConstraintType );
439 
440  sys.SetHistoName( SysHistoName );
441  sys.SetHistoPath( SysHistoPath );
442  sys.SetInputFile( SysHistoFile );
443 
444  fShapeSysList.push_back( sys );
445 
446 }
447 
448 void RooStats::HistFactory::Sample::AddShapeSys( const ShapeSys& Sys ) {
449  fShapeSysList.push_back(Sys);
450 }