Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
Systematics.h
Go to the documentation of this file.
1 // @(#)root/roostats:$Id$
2 // Author: George Lewis, Kyle Cranmer
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 #ifndef HISTFACTORY_SYSTEMATICS_H
12 #define HISTFACTORY_SYSTEMATICS_H
13 
14 #include <string>
15 #include <fstream>
16 #include <iostream>
17 
18 #include "TH1.h"
20 
21 namespace RooStats{
22 namespace HistFactory {
23 
24  namespace Constraint {
25  enum Type{ Gaussian, Poisson };
26  std::string Name( Type type );
27  Type GetType( const std::string& Name );
28  }
29 
30 
31  // Base class for common functions
32  /*
33  class Systematic {
34 
35  public:
36 
37  virtual void Print(std::ostream& = std::cout);
38  virtual void writeToFile(const std::string& FileName,
39  const std::string& Directory);
40 
41 
42  };
43  */
44 
45 /** \class OverallSys
46  * \ingroup HistFactory
47  * Configuration for a constrained overall systematic to scale sample normalisations.
48  */
49  class OverallSys {
50 
51  public:
52 
53  OverallSys() : fLow(0), fHigh(0) {}
54 
55  void SetName( const std::string& Name ) { fName = Name; }
56  std::string GetName() const { return fName; }
57 
58  void SetLow( double Low ) { fLow = Low; }
59  void SetHigh( double High ) { fHigh = High; }
60  double GetLow() const { return fLow; }
61  double GetHigh() const { return fHigh; }
62 
63  void Print(std::ostream& = std::cout) const;
64  void PrintXML(std::ostream&) const;
65 
66  protected:
67  std::string fName;
68  double fLow;
69  double fHigh;
70 
71  };
72 
73 /** \class NormFactor
74  * \ingroup HistFactory
75  * Configuration for an \a un- constrained overall systematic to scale sample normalisations.
76  */
77  class NormFactor {
78 
79  public:
80 
81  NormFactor();
82 
83  void SetName( const std::string& Name ) { fName = Name; }
84  std::string GetName() const { return fName; }
85 
86  void SetVal( double Val ) { fVal = Val; }
87  double GetVal() const { return fVal; }
88 
89  void SetConst( bool Const=true ) { fConst = Const; }
90  bool GetConst() const { return fConst; }
91 
92  void SetLow( double Low ) { fLow = Low; }
93  void SetHigh( double High ) { fHigh = High; }
94  double GetLow() const { return fLow; }
95  double GetHigh() const { return fHigh; }
96 
97  void Print(std::ostream& = std::cout) const;
98  void PrintXML(std::ostream&) const;
99 
100  protected:
101 
102  std::string fName;
103  double fVal;
104  double fLow;
105  double fHigh;
106  bool fConst;
107 
108  };
109 
110 
111  /** ////////////////////////////////////////////////////////////////////////////////////////////
112  * \class HistogramUncertaintyBase
113  * \ingroup HistFactory
114  * Base class to store the up and down variations for histogram uncertainties.
115  * Use the derived classes for actual models.
116  */
117  class HistogramUncertaintyBase {
118 
119  public:
120 
121  HistogramUncertaintyBase() : fhLow(nullptr), fhHigh(nullptr) {}
122  HistogramUncertaintyBase(const std::string& Name) : fName(Name), fhLow(nullptr), fhHigh(nullptr) {}
123  HistogramUncertaintyBase(const HistogramUncertaintyBase& oth) :
124  fName{oth.fName},
125  fInputFileLow{oth.fInputFileLow}, fHistoNameLow{oth.fHistoNameLow}, fHistoPathLow{oth.fHistoPathLow},
126  fInputFileHigh{oth.fInputFileHigh}, fHistoNameHigh{oth.fHistoNameHigh}, fHistoPathHigh{oth.fHistoPathHigh},
127  fhLow{oth.fhLow ? static_cast<TH1*>(oth.fhLow->Clone()) : nullptr},
128  fhHigh{oth.fhHigh ? static_cast<TH1*>(oth.fhHigh->Clone()) : nullptr} {
129 
130  }
131  HistogramUncertaintyBase(HistogramUncertaintyBase&&) = default;
132 
133  virtual ~HistogramUncertaintyBase() {};
134 
135 
136  // Need deep copies because the class owns its histograms.
137  HistogramUncertaintyBase& operator=(const HistogramUncertaintyBase& oth) {
138  fName = oth.fName;
139  fInputFileLow = oth.fInputFileLow;
140  fHistoNameLow = oth.fHistoNameLow;
141  fHistoPathLow = oth.fHistoPathLow;
142  fInputFileHigh = oth.fInputFileHigh;
143  fHistoNameHigh = oth.fHistoNameHigh;
144  fHistoPathHigh = oth.fHistoPathHigh;
145  fhLow.reset(oth.fhLow ? static_cast<TH1*>(oth.fhLow->Clone()) : nullptr);
146  fhHigh.reset(oth.fhHigh ? static_cast<TH1*>(oth.fhHigh->Clone()) : nullptr);
147 
148  return *this;
149  }
150  HistogramUncertaintyBase& operator=(HistogramUncertaintyBase&&) = default;
151 
152  virtual void Print(std::ostream& = std::cout) const;
153  virtual void PrintXML(std::ostream&) const = 0;
154  virtual void writeToFile( const std::string& FileName, const std::string& DirName );
155 
156  void SetHistoLow(TH1* Low ) {Low->SetDirectory(nullptr); fhLow.reset(Low);}
157  void SetHistoHigh(TH1* High ) {High->SetDirectory(nullptr); fhHigh.reset(High);}
158 
159  const TH1* GetHistoLow() const {return fhLow.get();}
160  const TH1* GetHistoHigh() const {return fhHigh.get();}
161 
162  void SetName( const std::string& Name ) { fName = Name; }
163  const std::string& GetName() const { return fName; }
164 
165  void SetInputFileLow( const std::string& InputFileLow ) { fInputFileLow = InputFileLow; }
166  void SetInputFileHigh( const std::string& InputFileHigh ) { fInputFileHigh = InputFileHigh; }
167 
168  const std::string& GetInputFileLow() const { return fInputFileLow; }
169  const std::string& GetInputFileHigh() const { return fInputFileHigh; }
170 
171  void SetHistoNameLow( const std::string& HistoNameLow ) { fHistoNameLow = HistoNameLow; }
172  void SetHistoNameHigh( const std::string& HistoNameHigh ) { fHistoNameHigh = HistoNameHigh; }
173 
174  const std::string& GetHistoNameLow() const { return fHistoNameLow; }
175  const std::string& GetHistoNameHigh() const { return fHistoNameHigh; }
176 
177  void SetHistoPathLow( const std::string& HistoPathLow ) { fHistoPathLow = HistoPathLow; }
178  void SetHistoPathHigh( const std::string& HistoPathHigh ) { fHistoPathHigh = HistoPathHigh; }
179 
180  const std::string& GetHistoPathLow() const { return fHistoPathLow; }
181  const std::string& GetHistoPathHigh() const { return fHistoPathHigh; }
182 
183  protected:
184 
185  std::string fName;
186 
187  std::string fInputFileLow;
188  std::string fHistoNameLow;
189  std::string fHistoPathLow;
190 
191  std::string fInputFileHigh;
192  std::string fHistoNameHigh;
193  std::string fHistoPathHigh;
194 
195  // The Low and High Histograms
196  std::unique_ptr<TH1> fhLow;
197  std::unique_ptr<TH1> fhHigh;
198 
199  };
200 
201 /** \class HistoSys
202  * \ingroup HistFactory
203  * Configuration for a constrained, coherent shape variation of affected samples.
204  */
205 class HistoSys final : public HistogramUncertaintyBase {
206 public:
207  virtual ~HistoSys() {}
208  virtual void PrintXML(std::ostream&) const override;
209 };
210 
211 /** \class HistoFactor
212  * \ingroup HistFactory
213  * Configuration for an *un*constrained, coherent shape variation of affected samples.
214  */
215  class HistoFactor final : public HistogramUncertaintyBase {
216  public:
217  virtual ~HistoFactor() {}
218  void PrintXML(std::ostream&) const override;
219  };
220 
221 /** \class ShapeSys
222  * \ingroup HistFactory
223  * Constrained bin-by-bin variation of affected histogram.
224  */
225  class ShapeSys final : public HistogramUncertaintyBase {
226 
227  public:
228 
229  ShapeSys() :
230  HistogramUncertaintyBase(),
231  fConstraintType(Constraint::Gaussian) {}
232  ShapeSys(const ShapeSys& other) :
233  HistogramUncertaintyBase(other),
234  fConstraintType(other.fConstraintType) {}
235 
236  void SetInputFile( const std::string& InputFile ) { fInputFileHigh = InputFile; }
237  std::string GetInputFile() const { return fInputFileHigh; }
238 
239  void SetHistoName( const std::string& HistoName ) { fHistoNameHigh = HistoName; }
240  std::string GetHistoName() const { return fHistoNameHigh; }
241 
242  void SetHistoPath( const std::string& HistoPath ) { fHistoPathHigh = HistoPath; }
243  std::string GetHistoPath() const { return fHistoPathHigh; }
244 
245  void Print(std::ostream& = std::cout) const override;
246  void PrintXML(std::ostream&) const override;
247  void writeToFile( const std::string& FileName, const std::string& DirName ) override;
248 
249  const TH1* GetErrorHist() const {
250  return fhHigh.get();
251  }
252  void SetErrorHist(TH1* hError) {
253  fhHigh.reset(hError);
254  }
255 
256  void SetConstraintType( Constraint::Type ConstrType ) { fConstraintType = ConstrType; }
257  Constraint::Type GetConstraintType() const { return fConstraintType; }
258 
259  protected:
260  Constraint::Type fConstraintType;
261  };
262 
263 /** \class ShapeFactor
264  * \ingroup HistFactory
265  * *Un*constrained bin-by-bin variation of affected histogram.
266  */
267  class ShapeFactor : public HistogramUncertaintyBase {
268 
269  public:
270 
271  ShapeFactor() :
272  HistogramUncertaintyBase(),
273  fConstant{false},
274  fHasInitialShape{false} {}
275 
276  void Print(std::ostream& = std::cout) const override;
277  void PrintXML(std::ostream&) const override;
278  void writeToFile( const std::string& FileName, const std::string& DirName) override;
279 
280  void SetInitialShape(TH1* shape) {
281  fhHigh.reset(shape);
282  }
283  const TH1* GetInitialShape() const { return fhHigh.get(); }
284 
285  void SetConstant(bool constant) { fConstant = constant; }
286  bool IsConstant() const { return fConstant; }
287 
288  bool HasInitialShape() const { return fHasInitialShape; }
289 
290  void SetInputFile( const std::string& InputFile ) {
291  fInputFileHigh = InputFile;
292  fHasInitialShape=true;
293  }
294  const std::string& GetInputFile() const { return fInputFileHigh; }
295 
296  void SetHistoName( const std::string& HistoName ) {
297  fHistoNameHigh = HistoName;
298  fHasInitialShape=true;
299  }
300  const std::string& GetHistoName() const { return fHistoNameHigh; }
301 
302  void SetHistoPath( const std::string& HistoPath ) {
303  fHistoPathHigh = HistoPath;
304  fHasInitialShape=true;
305  }
306  const std::string& GetHistoPath() const { return fHistoPathHigh; }
307 
308  protected:
309 
310  bool fConstant;
311 
312  // A histogram representing
313  // the initial shape
314  bool fHasInitialShape;
315  };
316 
317 /** \class StatError
318  * \ingroup HistFactory
319  * Statistical error of Monte Carlo predictions.
320  */
321  class StatError : public HistogramUncertaintyBase {
322 
323  public:
324 
325  StatError() :
326  HistogramUncertaintyBase(),
327  fActivate(false), fUseHisto(false) {}
328 
329  void Print(std::ostream& = std::cout) const override;
330  void PrintXML(std::ostream&) const override;
331  void writeToFile( const std::string& FileName, const std::string& DirName ) override;
332 
333  void Activate( bool IsActive=true ) { fActivate = IsActive; }
334  bool GetActivate() const { return fActivate; }
335 
336  void SetUseHisto( bool UseHisto=true ) { fUseHisto = UseHisto; }
337  bool GetUseHisto() const { return fUseHisto; }
338 
339  void SetInputFile( const std::string& InputFile ) { fInputFileHigh = InputFile; }
340  const std::string& GetInputFile() const { return fInputFileHigh; }
341 
342  void SetHistoName( const std::string& HistoName ) { fHistoNameHigh = HistoName; }
343  const std::string& GetHistoName() const { return fHistoNameHigh; }
344 
345  void SetHistoPath( const std::string& HistoPath ) { fHistoPathHigh = HistoPath; }
346  const std::string& GetHistoPath() const { return fHistoPathHigh; }
347 
348 
349  const TH1* GetErrorHist() const {
350  return fhHigh.get();
351  }
352  void SetErrorHist(TH1* Error) {
353  fhHigh.reset(Error);
354  }
355 
356  protected:
357 
358  bool fActivate;
359  bool fUseHisto; // Use an external histogram for the errors
360  };
361 
362 /** \class StatErrorConfig
363  * \ingroup HistFactory
364  * Configuration to automatically assign nuisance parameters for the statistical
365  * error of the Monte Carlo simulations.
366  */
367  class StatErrorConfig {
368 
369  public:
370 
371  StatErrorConfig() : fRelErrorThreshold( .05 ), fConstraintType( Constraint::Gaussian ) {;}
372  void Print(std::ostream& = std::cout) const;
373  void PrintXML(std::ostream&) const;
374 
375  void SetRelErrorThreshold( double Threshold ) { fRelErrorThreshold = Threshold; }
376  double GetRelErrorThreshold() const { return fRelErrorThreshold; }
377 
378  void SetConstraintType( Constraint::Type ConstrType ) { fConstraintType = ConstrType; }
379  Constraint::Type GetConstraintType() const { return fConstraintType; }
380 
381  protected:
382 
383  double fRelErrorThreshold;
384  Constraint::Type fConstraintType;
385 
386  };
387 
388 
389 }
390 }
391 
392 #endif