MaCh3  2.6.1
Reference Guide
FitterBase.h
Go to the documentation of this file.
1 #pragma once
2 
3 // C++ includes
4 #include <iostream>
5 #include <fstream>
6 #include <sstream>
7 
8 // MaCh3 Includes
11 #include "Manager/Manager.h"
12 #include "Fitters/MCMCProcessor.h"
13 
14 //KS: Joy of forward declaration https://gieseanw.wordpress.com/2018/02/25/the-joys-of-forward-declarations-results-from-the-real-world/
15 class TRandom3;
16 class TStopwatch;
17 class TTree;
18 class TGraphAsymmErrors;
19 class TDirectory;
20 
29 class FitterBase {
30  public:
33  FitterBase(Manager * const fitMan);
35  virtual ~FitterBase();
36 
40 
44 
46  virtual void RunMCMC() = 0;
47 
50  void DragRace(const int NLaps = 100);
51 
53  void RunLLHScan();
54 
57  void RunLLHMap();
58 
61  void GetStepScaleBasedOnLLHScan(const std::string& filename = "");
62 
65  void Run2DLLHScan();
66 
68  void RunSigmaVar();
69 
72  virtual void StartFromPreviousFit(const std::string& FitName);
73 
75  std::string GetName() const {return AlgorithmName;};
76  protected:
78  void ProcessMCMC();
79 
81  void PrepareOutput();
82 
84  void SaveOutput();
85 
88  void SanitiseInputs();
89 
91  void SaveSettings();
92 
95  bool GetScanRange(std::map<std::string, std::vector<double>>& scanRanges) const;
97  void GetParameterScanRange(const ParameterHandlerBase* cov, const int i, double& CentralValue,
98  double& lower, double& upper, const int n_points, const std::string& suffix = "") const;
100  bool CheckSkipParameter(const std::vector<std::string>& SkipVector, const std::string& ParamName) const;
101 
102 
111  void CustomRange(const std::string& ParName, const double sigma, double& ParamShiftValue) const;
112 
113  //Bin edges needed for logarithmic LLH scans
114  std::vector<double> CalculateBinEdges(double lowerlimit,double upperlimit, int n_points) const;
115 
118 
120  unsigned int step;
122  double logLCurr;
124  double logLProp;
126  double accProb;
128  int accCount;
130  unsigned int stepStart;
131 
133  std::vector<double> sample_llh;
135  std::vector<double> syst_llh;
136 
138  std::vector<SampleHandlerInterface*> samples;
140  unsigned int TotalNSamples;
141 
143  std::vector<ParameterHandlerBase*> systematics;
144 
146  std::unique_ptr<TStopwatch> clock;
148  std::unique_ptr<TStopwatch> stepClock;
150  double stepTime;
151 
153  std::unique_ptr<TRandom3> random;
154 
156  TFile *outputFile;
158  TDirectory *CovFolder;
160  TDirectory *SampleFolder;
162  TTree *outTree;
165 
168 
170  bool FileSaved;
175 
177  std::string AlgorithmName;
178 
179  #ifdef MACH3_DEBUG
181  bool debug;
183  std::ofstream debugFile;
184  #endif
185 };
186 
Base class for implementing fitting algorithms.
Definition: FitterBase.h:29
void RunLLHScan()
Perform a 1D likelihood scan.
Definition: FitterBase.cpp:622
FitterBase(Manager *const fitMan)
Constructor.
Definition: FitterBase.cpp:18
void AddSystObj(ParameterHandlerBase *cov)
This function adds a Covariance object to the analysis framework. The Covariance object will be utili...
Definition: FitterBase.cpp:299
std::string GetName() const
Get name of class.
Definition: FitterBase.h:75
std::unique_ptr< TRandom3 > random
Random number.
Definition: FitterBase.h:153
double logLProp
proposed likelihood
Definition: FitterBase.h:124
bool CheckSkipParameter(const std::vector< std::string > &SkipVector, const std::string &ParamName) const
KS: Check whether we want to skip parameter using skip vector.
Definition: FitterBase.cpp:536
void ProcessMCMC()
Process MCMC output.
Definition: FitterBase.cpp:407
int accCount
counts accepted steps
Definition: FitterBase.h:128
bool OutputPrepared
Checks if output prepared not repeat some operations.
Definition: FitterBase.h:174
void SaveOutput()
Save output and close files.
Definition: FitterBase.cpp:232
TFile * outputFile
Output.
Definition: FitterBase.h:156
void SaveSettings()
Save the settings that the MCMC was run with.
Definition: FitterBase.cpp:80
unsigned int step
current state
Definition: FitterBase.h:120
void PrepareOutput()
Prepare the output file.
Definition: FitterBase.cpp:154
bool SettingsSaved
Checks if setting saved not repeat some operations.
Definition: FitterBase.h:172
virtual void RunMCMC()=0
The specific fitting algorithm implemented in this function depends on the derived class....
double accProb
current acceptance prob
Definition: FitterBase.h:126
virtual void StartFromPreviousFit(const std::string &FitName)
Allow to start from previous fit/chain.
Definition: FitterBase.cpp:349
bool FileSaved
Checks if file saved not repeat some operations.
Definition: FitterBase.h:170
std::string AlgorithmName
Name of fitting algorithm that is being used.
Definition: FitterBase.h:177
std::vector< double > sample_llh
store the llh breakdowns
Definition: FitterBase.h:133
void RunSigmaVar()
Perform a 1D/2D sigma var for all samples.
std::vector< SampleHandlerInterface * > samples
Sample holder.
Definition: FitterBase.h:138
void GetParameterScanRange(const ParameterHandlerBase *cov, const int i, double &CentralValue, double &lower, double &upper, const int n_points, const std::string &suffix="") const
Helper function to get parameter scan range, central value.
Definition: FitterBase.cpp:543
double stepTime
Time of single step.
Definition: FitterBase.h:150
std::unique_ptr< TStopwatch > clock
tells global time how long fit took
Definition: FitterBase.h:146
Manager * fitMan
The manager for configuration handling.
Definition: FitterBase.h:117
unsigned int stepStart
step start, by default 0 if we start from previous chain then it will be different
Definition: FitterBase.h:130
bool GetScanRange(std::map< std::string, std::vector< double >> &scanRanges) const
YSP: Set up a mapping to store parameters with user-specified ranges, suggested by D....
Definition: FitterBase.cpp:516
std::unique_ptr< TStopwatch > stepClock
tells how long single step/fit iteration took
Definition: FitterBase.h:148
TDirectory * CovFolder
Output cov folder.
Definition: FitterBase.h:158
void CustomRange(const std::string &ParName, const double sigma, double &ParamShiftValue) const
For comparison with other fitting frameworks (like P-Theta) we usually have to apply different parame...
TDirectory * SampleFolder
Output sample folder.
Definition: FitterBase.h:160
void DragRace(const int NLaps=100)
Calculates the required time for each sample or covariance object in a drag race simulation....
Definition: FitterBase.cpp:454
void Run2DLLHScan()
Perform a 2D likelihood scan.
Definition: FitterBase.cpp:968
unsigned int TotalNSamples
Total number of samples used, single SampleHandler can store more than one analysis sample!
Definition: FitterBase.h:140
double logLCurr
current likelihood
Definition: FitterBase.h:122
void RunLLHMap()
Perform a general multi-dimensional likelihood scan.
std::vector< double > syst_llh
systematic llh breakdowns
Definition: FitterBase.h:135
int auto_save
auto save every N steps
Definition: FitterBase.h:164
bool fTestLikelihood
Necessary for some fitting algorithms like PSO.
Definition: FitterBase.h:167
void GetStepScaleBasedOnLLHScan(const std::string &filename="")
LLH scan is good first estimate of step scale.
Definition: FitterBase.cpp:906
virtual ~FitterBase()
Destructor for the FitterBase class.
Definition: FitterBase.cpp:70
TTree * outTree
Output tree with posteriors.
Definition: FitterBase.h:162
void SanitiseInputs()
Remove obsolete memory and make other checks before fit starts.
Definition: FitterBase.cpp:224
void AddSampleHandler(SampleHandlerInterface *sample)
This function adds a sample PDF object to the analysis framework. The sample PDF object will be utili...
Definition: FitterBase.cpp:263
std::vector< double > CalculateBinEdges(double lowerlimit, double upperlimit, int n_points) const
Definition: FitterBase.cpp:606
std::vector< ParameterHandlerBase * > systematics
Systematic holder.
Definition: FitterBase.h:143
The manager class is responsible for managing configurations and settings.
Definition: Manager.h:16
Base class for handling systematic uncertainty parameters.
Class responsible for handling implementation of samples used in analysis, reweighting and returning ...