Logo ROOT   6.10/09
Reference Guide
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 
13 /*
14 BEGIN_HTML
15 <p>
16 </p>
17 END_HTML
18 */
19 //
20 
21 #include "TH1.h"
24 
25 //#include "TClass.h"
26 
28  fNormalizeByTheory(false), fStatErrorActivate(false), fhNominal(), fhCountingHist(0) { ; }
29 
30 // copy constructor (important for python)
32  fName(other.fName), fInputFile(other.fInputFile),
35 
42 
43  fStatError(other.fStatError),
46  fhNominal(other.fhNominal),
48  {
49  if( other.fhCountingHist ) {
51  }else{
53  }
54  }
55 
56 
57 RooStats::HistFactory::Sample::Sample(std::string SampName, std::string SampHistoName, std::string SampInputFile, std::string SampHistoPath) :
58  fName( SampName ), fInputFile( SampInputFile),
59  fHistoName( SampHistoName ), fHistoPath( SampHistoPath ),
61  fhCountingHist(0) { ; }
62 
63 RooStats::HistFactory::Sample::Sample(std::string SampName) :
64  fName( SampName ), fInputFile( "" ),
65  fHistoName( "" ), fHistoPath( "" ),
67  fhCountingHist(0) { ; }
68 
70  if(fhCountingHist)
71  delete fhCountingHist;
72 }
73 
75  TH1* histo = (TH1*) fhNominal.GetObject();
76  return histo;
77 }
78 
79 
80 void RooStats::HistFactory::Sample::writeToFile( std::string OutputFileName, std::string DirName ) {
81 
82  TH1* histNominal = GetHisto();
83  histNominal->Write();
84 
85  // Set the location of the data
86  // in the output measurement
87 
88  fInputFile = OutputFileName;
89  fHistoName = histNominal->GetName();
90  fHistoPath = DirName;
91 
92  // Write this sample's StatError
93  GetStatError().writeToFile( OutputFileName, DirName );
94 
95  // Must write all systematics that contain internal histograms
96  // (This is not all systematics)
97  for( unsigned int i = 0; i < GetHistoSysList().size(); ++i ) {
98  GetHistoSysList().at(i).writeToFile( OutputFileName, DirName );
99  }
100  for( unsigned int i = 0; i < GetHistoFactorList().size(); ++i ) {
101  GetHistoFactorList().at(i).writeToFile( OutputFileName, DirName );
102  }
103  for( unsigned int i = 0; i < GetShapeSysList().size(); ++i ) {
104  GetShapeSysList().at(i).writeToFile( OutputFileName, DirName );
105  }
106  for( unsigned int i = 0; i < GetShapeFactorList().size(); ++i ) {
107  GetShapeFactorList().at(i).writeToFile( OutputFileName, DirName );
108  }
109 
110  return;
111 
112 }
113 
114 
116 
117  // For use in a number counting measurement
118  // Create a 1-bin histogram,
119  // fill it with this input value,
120  // and set this Sample's histogram to that hist
121 
122  std::string SampleHistName = fName + "_hist";
123 
124  // Histogram has 1-bin (hard-coded)
125  if(fhCountingHist)
126  delete fhCountingHist;
127 
128  fhCountingHist = new TH1F( SampleHistName.c_str(), SampleHistName.c_str(), 1, 0, 1 );
129  fhCountingHist->SetBinContent( 1, val );
130 
131  // Set the histogram of the internally held data
132  // node of this channel to this newly created histogram
134 
135 }
136 
137 
138 
139 void RooStats::HistFactory::Sample::Print( std::ostream& stream ) {
140 
141 
142  stream << "\t \t Name: " << fName
143  << "\t \t Channel: " << fChannelName
144  << "\t NormalizeByTheory: " << (fNormalizeByTheory ? "True" : "False")
145  << "\t StatErrorActivate: " << (fStatErrorActivate ? "True" : "False")
146  << std::endl;
147 
148  stream << "\t \t \t \t "
149  << "\t InputFile: " << fInputFile
150  << "\t HistName: " << fHistoName
151  << "\t HistoPath: " << fHistoPath
152  << "\t HistoAddress: " << GetHisto()
153  // << "\t Type: " << GetHisto()->ClassName()
154  << std::endl;
155 
156  if( fStatError.GetActivate() ) {
157  stream << "\t \t \t StatError Activate: " << fStatError.GetActivate()
158  << "\t InputFile: " << fInputFile
159  << "\t HistName: " << fStatError.GetHistoName()
160  << "\t HistoPath: " << fStatError.GetHistoPath()
161  << "\t HistoAddress: " << fStatError.GetErrorHist()
162  << std::endl;
163  }
164 
165 
166  /*
167  stream<< " NormalizeByTheory: ";
168  if(NormalizeByTheory) stream << "True";
169  else stream << "False";
170 
171  stream<< " StatErrorActivate: ";
172  if(StatErrorActivate) stream << "True";
173  else stream << "False";
174  */
175 
176 
177 }
178 
179 void RooStats::HistFactory::Sample::PrintXML( std::ofstream& xml ) {
180 
181 
182  // Create the sample tag
183  xml << " <Sample Name=\"" << fName << "\" "
184  << " HistoPath=\"" << fHistoPath << "\" "
185  << " HistoName=\"" << fHistoName << "\" "
186  << " InputFile=\"" << fInputFile << "\" "
187  << " NormalizeByTheory=\"" << (fNormalizeByTheory ? std::string("True") : std::string("False")) << "\" "
188  << ">" << std::endl;
189 
190 
191  // Print Stat Error (if necessary)
192  fStatError.PrintXML( xml );
193  /*
194  if( fStatError.GetActivate() ) {
195  xml << " <StatError Activate=\"" << (fStatError.GetActivate() ? std::string("True") : std::string("False")) << "\" "
196  << " InputFile=\"" << fStatError.GetInputFile() << "\" "
197  << " HistoName=\"" << fStatError.GetHistoName() << "\" "
198  << " HistoPath=\"" << fStatError.GetHistoPath() << "\" "
199  << " /> " << std::endl;
200  }
201  */
202 
203 
204  // Now, print the systematics:
205  for( unsigned int i = 0; i < fOverallSysList.size(); ++i ) {
207  sys.PrintXML(xml);
208  /*
209  xml << " <OverallSys Name=\"" << sys.GetName() << "\" "
210  << " High=\"" << sys.GetHigh() << "\" "
211  << " Low=\"" << sys.GetLow() << "\" "
212  << " /> " << std::endl;
213  */
214  }
215  for( unsigned int i = 0; i < fNormFactorList.size(); ++i ) {
217  sys.PrintXML(xml);
218  /*
219  xml << " <NormFactor Name=\"" << sys.GetName() << "\" "
220  << " Val=\"" << sys.GetVal() << "\" "
221  << " High=\"" << sys.GetHigh() << "\" "
222  << " Low=\"" << sys.GetLow() << "\" "
223  << " Const=\"" << (sys.GetConst() ? std::string("True") : std::string("False")) << "\" "
224  << " /> " << std::endl;
225  */
226  }
227  for( unsigned int i = 0; i < fHistoSysList.size(); ++i ) {
229  sys.PrintXML(xml);
230  /*
231  xml << " <HistoSys Name=\"" << sys.GetName() << "\" "
232 
233  << " InputFileLow=\"" << sys.GetInputFileLow() << "\" "
234  << " HistoNameLow=\"" << sys.GetHistoNameLow() << "\" "
235  << " HistoPathLow=\"" << sys.GetHistoPathLow() << "\" "
236 
237  << " InputFileHigh=\"" << sys.GetInputFileHigh() << "\" "
238  << " HistoNameHigh=\"" << sys.GetHistoNameHigh() << "\" "
239  << " HistoPathHigh=\"" << sys.GetHistoPathHigh() << "\" "
240  << " /> " << std::endl;
241  */
242  }
243  for( unsigned int i = 0; i < fHistoFactorList.size(); ++i ) {
245  sys.PrintXML(xml);
246  /*
247  xml << " <HistoFactor Name=\"" << sys.GetName() << "\" "
248 
249  << " InputFileLow=\"" << sys.GetInputFileLow() << "\" "
250  << " HistoNameLow=\"" << sys.GetHistoNameLow() << "\" "
251  << " HistoPathLow=\"" << sys.GetHistoPathLow() << "\" "
252 
253  << " InputFileHigh=\"" << sys.GetInputFileHigh() << "\" "
254  << " HistoNameHigh=\"" << sys.GetHistoNameHigh() << "\" "
255  << " HistoPathHigh=\"" << sys.GetHistoPathHigh() << "\" "
256  << " /> " << std::endl;
257  */
258  }
259  for( unsigned int i = 0; i < fShapeSysList.size(); ++i ) {
261  sys.PrintXML(xml);
262  /*
263  xml << " <ShapeSys Name=\"" << sys.GetName() << "\" "
264 
265  << " InputFile=\"" << sys.GetInputFile() << "\" "
266  << " HistoName=\"" << sys.GetHistoName() << "\" "
267  << " HistoPath=\"" << sys.GetHistoPath() << "\" "
268  << " ConstraintType=\"" << std::string(Constraint::Name(sys.GetConstraintType())) << "\" "
269  << " /> " << std::endl;
270  */
271  }
272  for( unsigned int i = 0; i < fShapeFactorList.size(); ++i ) {
274  sys.PrintXML(xml);
275  /*
276  xml << " <ShapeFactor Name=\"" << sys.GetName() << "\" "
277  << " /> " << std::endl;
278  */
279  }
280 
281  // Finally, close the tag
282  xml << " </Sample>" << std::endl;
283 
284 }
285 
286 
287 
288 // Some helper functions
289 // (Not strictly necessary because
290 // methods are publicly accessable)
291 
292 
294 
295  fStatError.Activate( true );
296  fStatError.SetUseHisto( false );
297 
298 }
299 
300 
301 void RooStats::HistFactory::Sample::ActivateStatError( std::string StatHistoName, std::string StatInputFile, std::string StatHistoPath ) {
302 
303 
304  fStatError.Activate( true );
305  fStatError.SetUseHisto( true );
306 
307  fStatError.SetInputFile( StatInputFile );
308  fStatError.SetHistoName( StatHistoName );
309  fStatError.SetHistoPath( StatHistoPath );
310 
311 }
312 
313 
314 void RooStats::HistFactory::Sample::AddOverallSys( std::string SysName, Double_t SysLow, Double_t SysHigh ) {
315 
317  sys.SetName( SysName );
318  sys.SetLow( SysLow );
319  sys.SetHigh( SysHigh );
320 
321  fOverallSysList.push_back( sys );
322 
323 }
324 
326  fOverallSysList.push_back(Sys);
327 }
328 
329 void RooStats::HistFactory::Sample::AddNormFactor( std::string SysName, Double_t SysVal, Double_t SysLow, Double_t SysHigh, bool SysConst ) {
330 
332 
333  norm.SetName( SysName );
334  norm.SetVal( SysVal );
335  norm.SetLow( SysLow );
336  norm.SetHigh( SysHigh );
337  norm.SetConst( SysConst );
338 
339  fNormFactorList.push_back( norm );
340 
341 }
342 
344  fNormFactorList.push_back( Factor );
345 }
346 
347 
349 std::string SysHistoNameLow, std::string SysHistoFileLow, std::string SysHistoPathLow,
350  std::string SysHistoNameHigh, std::string SysHistoFileHigh, std::string SysHistoPathHigh ) {
351 
353  sys.SetName( SysName );
354 
355  sys.SetHistoNameLow( SysHistoNameLow );
356  sys.SetHistoPathLow( SysHistoPathLow );
357  sys.SetInputFileLow( SysHistoFileLow );
358 
359  sys.SetHistoNameHigh( SysHistoNameHigh );
360  sys.SetHistoPathHigh( SysHistoPathHigh );
361  sys.SetInputFileHigh( SysHistoFileHigh );
362 
363  fHistoSysList.push_back( sys );
364 
365 }
366 
368  fHistoSysList.push_back( Sys );
369 }
370 
371 
372 void RooStats::HistFactory::Sample::AddHistoFactor( std::string SysName, std::string SysHistoNameLow, std::string SysHistoFileLow, std::string SysHistoPathLow,
373  std::string SysHistoNameHigh, std::string SysHistoFileHigh, std::string SysHistoPathHigh ) {
374 
376  factor.SetName( SysName );
377 
378  factor.SetHistoNameLow( SysHistoNameLow );
379  factor.SetHistoPathLow( SysHistoPathLow );
380  factor.SetInputFileLow( SysHistoFileLow );
381 
382  factor.SetHistoNameHigh( SysHistoNameHigh );
383  factor.SetHistoPathHigh( SysHistoPathHigh );
384  factor.SetInputFileHigh( SysHistoFileHigh );
385 
386  fHistoFactorList.push_back( factor );
387 
388 }
389 
391  fHistoFactorList.push_back(Factor);
392 }
393 
394 
396 
398  factor.SetName( SysName );
399  fShapeFactorList.push_back( factor );
400 
401 }
402 
403 
405  fShapeFactorList.push_back(Factor);
406 }
407 
408 
409 void RooStats::HistFactory::Sample::AddShapeSys( std::string SysName, Constraint::Type SysConstraintType, std::string SysHistoName, std::string SysHistoFile, std::string SysHistoPath ) {
410 
412  sys.SetName( SysName );
413  sys.SetConstraintType( SysConstraintType );
414 
415  sys.SetHistoName( SysHistoName );
416  sys.SetHistoPath( SysHistoPath );
417  sys.SetInputFile( SysHistoFile );
418 
419  fShapeSysList.push_back( sys );
420 
421 }
422 
424  fShapeSysList.push_back(Sys);
425 }
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory.
Definition: TObject.cxx:778
void SetName(const std::string &Name)
Definition: Systematics.h:51
RooStats::HistFactory::StatError & GetStatError()
Definition: Sample.h:117
void ActivateStatError()
Note that histogram name should not include the path of the histogram in the file.
Definition: Sample.cxx:293
void SetConstraintType(Constraint::Type ConstrType)
Definition: Systematics.h:241
void SetHistoNameHigh(const std::string &HistoNameHigh)
Definition: Systematics.h:131
void PrintXML(std::ostream &)
void SetHistoPathLow(const std::string &HistoPathLow)
Definition: Systematics.h:182
THist< 1, float, THistStatContent, THistStatUncertainty > TH1F
Definition: THist.hxx:311
virtual Double_t GetBinContent(Int_t bin) const
Return content of bin number bin.
Definition: TH1.cxx:4639
std::vector< RooStats::HistFactory::ShapeFactor > fShapeFactorList
Definition: Sample.h:142
void SetHistoPathHigh(const std::string &HistoPathHigh)
Definition: Systematics.h:137
std::vector< RooStats::HistFactory::NormFactor > fNormFactorList
Definition: Sample.h:136
#define NULL
Definition: RtypesCore.h:88
RooStats::HistFactory::StatError fStatError
Definition: Sample.h:146
std::vector< RooStats::HistFactory::HistoSys > & GetHistoSysList()
Definition: Sample.h:111
void AddShapeFactor(std::string Name)
Definition: Sample.cxx:395
std::vector< RooStats::HistFactory::HistoFactor > fHistoFactorList
Definition: Sample.h:139
void AddHistoFactor(std::string Name, std::string HistoNameLow, std::string HistoFileLow, std::string HistoPathLow, std::string HistoNameHigh, std::string HistoFileHigh, std::string HistoPathHigh)
Definition: Sample.cxx:372
void AddShapeSys(std::string Name, Constraint::Type ConstraintType, std::string HistoName, std::string HistoFile, std::string HistoPath="")
Definition: Sample.cxx:409
void Print(std::ostream &=std::cout)
Definition: Sample.cxx:139
void SetInputFileLow(const std::string &InputFileLow)
Definition: Systematics.h:124
void writeToFile(const std::string &FileName, const std::string &DirName)
void PrintXML(std::ostream &)
Definition: Systematics.cxx:87
void SetHistoNameLow(const std::string &HistoNameLow)
Definition: Systematics.h:176
void SetHistoPathLow(const std::string &HistoPathLow)
Definition: Systematics.h:136
void writeToFile(std::string FileName, std::string DirName)
Definition: Sample.cxx:80
void SetValue(Double_t Val)
Definition: Sample.cxx:115
void SetName(const std::string &Name)
Definition: Systematics.h:76
void SetHistoName(const std::string &HistoName)
Definition: Systematics.h:228
void SetName(const std::string &Name)
Definition: Systematics.h:264
void SetName(const std::string &Name)
Definition: Systematics.h:167
void AddHistoSys(std::string Name, std::string HistoNameLow, std::string HistoFileLow, std::string HistoPathLow, std::string HistoNameHigh, std::string HistoFileHigh, std::string HistoPathHigh)
Definition: Sample.cxx:348
virtual void SetBinContent(Int_t bin, Double_t content)
Set bin content see convention for numbering bins in TH1::GetBin In case the bin number is greater th...
Definition: TH1.cxx:8325
void SetConst(bool Const=true)
Definition: Systematics.h:82
std::vector< RooStats::HistFactory::ShapeSys > & GetShapeSysList()
Definition: Sample.h:114
void SetHistoName(const std::string &HistoName)
Definition: Systematics.h:332
void SetInputFileLow(const std::string &InputFileLow)
Definition: Systematics.h:170
void SetHistoNameLow(const std::string &HistoNameLow)
Definition: Systematics.h:130
std::vector< RooStats::HistFactory::HistoFactor > & GetHistoFactorList()
Definition: Sample.h:112
void SetHistoPathHigh(const std::string &HistoPathHigh)
Definition: Systematics.h:183
std::vector< RooStats::HistFactory::ShapeSys > fShapeSysList
Definition: Sample.h:141
void SetHistoPath(const std::string &HistoPath)
Definition: Systematics.h:335
void Activate(bool IsActive=true)
Definition: Systematics.h:323
void PrintXML(std::ostream &)
Definition: Systematics.cxx:70
void AddOverallSys(std::string Name, Double_t Low, Double_t High)
Definition: Sample.cxx:314
void AddNormFactor(std::string Name, Double_t Val, Double_t Low, Double_t High, bool Const=false)
Definition: Sample.cxx:329
void SetHistoNameHigh(const std::string &HistoNameHigh)
Definition: Systematics.h:177
double Double_t
Definition: RtypesCore.h:55
void PrintXML(std::ostream &)
The TH1 histogram class.
Definition: TH1.h:56
std::vector< RooStats::HistFactory::OverallSys > fOverallSysList
Definition: Sample.h:135
void SetName(const std::string &Name)
Definition: Systematics.h:121
void SetHisto(TH1 *histo)
Definition: Sample.h:45
void SetUseHisto(bool UseHisto=true)
Definition: Systematics.h:326
void SetHistoPath(const std::string &HistoPath)
Definition: Systematics.h:231
void SetName(const std::string &Name)
Definition: Systematics.h:222
void PrintXML(std::ostream &)
std::vector< RooStats::HistFactory::HistoSys > fHistoSysList
Definition: Sample.h:138
double norm(double *x, double *p)
Definition: unuranDistr.cxx:40
void PrintXML(std::ofstream &xml)
Definition: Sample.cxx:179
void SetInputFile(const std::string &InputFile)
Definition: Systematics.h:225
std::vector< RooStats::HistFactory::ShapeFactor > & GetShapeFactorList()
Definition: Sample.h:115
void SetInputFileHigh(const std::string &InputFileHigh)
Definition: Systematics.h:125
void SetInputFile(const std::string &InputFile)
Definition: Systematics.h:329
void SetInputFileHigh(const std::string &InputFileHigh)
Definition: Systematics.h:171