CMS 3D CMS Logo

TagProbeFitter.h
Go to the documentation of this file.
1 #ifndef TagProbeFitter_h
2 #define TagProbeFitter_h
3 
4 #include "TFile.h"
5 #include "TChain.h"
6 #include "TGraphAsymmErrors.h"
7 #include "RooWorkspace.h"
8 #include "RooFitResult.h"
9 #include "RooDataSet.h"
10 
12  public:
14  TagProbeFitter(const std::vector<std::string>& inputFileNames, std::string inputDirectoryName, std::string inputTreeName, std::string outputFileName, int numCPU = 1, bool saveWorkspace_ = false, bool floatShapeParameters = true, const std::vector<std::string>& fixVars_ = std::vector<std::string>() );
15 
18 
20  bool addVariable(std::string variableName, std::string title, double low, double hi, std::string units);
21 
23  bool addCategory(std::string categoryName, std::string title, std::string expression);
24 
26  bool addExpression(std::string expressionName, std::string title, std::string expression, const std::vector<std::string>& arguments);
27 
29  bool addThresholdCategory(std::string categoryName, std::string title, std::string varName, double cutValue);
30 
33  void addPdf(std::string pdfName, std::vector<std::string>& pdfCommands);
34 
36  void addFixedVariavles(const std::vector<std::string>&);
37 
39  std::string calculateEfficiency(std::string dirName, std::string efficiencyCategory, std::string efficiencyState, std::vector<std::string>& unbinnedVariables, std::map<std::string, std::vector<double> >& binnedReals, std::map<std::string, std::vector<std::string> >& binnedCategories, std::vector<std::string>& binToPDFmap) {
40  std::vector<std::string> efficiencyCategories(1,efficiencyCategory);
41  std::vector<std::string> efficiencyStates(1,efficiencyState);
42  return calculateEfficiency(dirName, efficiencyCategories, efficiencyStates, unbinnedVariables, binnedReals, binnedCategories, binToPDFmap);
43  }
44 
45  std::string calculateEfficiency(std::string dirName,const std::vector<std::string>& efficiencyCategories, const std::vector<std::string>& efficiencyStates, std::vector<std::string>& unbinnedVariables, std::map<std::string, std::vector<double> >& binnedReals, std::map<std::string, std::vector<std::string> >& binnedCategories, std::vector<std::string>& binToPDFmap);
46 
48  void setBinnedFit(bool binned, int bins=0) { binnedFit = binned; massBins = bins; }
49 
51  void setBinsForMassPlots(int bins) ;
52 
54  void setSaveDistributionsPlot(bool saveDistributionsPlot_) { doSaveDistributionsPlot = saveDistributionsPlot_; }
55 
57  void setWeightVar(const std::string &weight);
58 
60  void setQuiet(bool quiet_=true);
61 
65  void setSplitMode(unsigned int nevents);
66 
67  protected:
69  TChain* inputTree;
70 
72  TFile* outputFile;
73 
75  TDirectory* outputDirectory;
76 
78  int numCPU;
79 
82 
85 
87  int binnedFit;
88 
90  int massBins;
91 
93  std::map<std::string, std::vector<std::string> > pdfs;
94 
96  RooArgSet variables;
97 
100 
102  //RooArgSet expressionVars;
103  std::vector<std::pair<std::pair<std::string,std::string>, std::pair<std::string, std::vector<std::string> > > > expressionVars;
104 
105  // Threshold categories have to be created at the last minute
106  // so we store just the info about them
107  std::vector<std::pair<std::pair<std::string,std::string>, std::pair<std::string, double> > > thresholdCategories;
108 
110  std::vector<std::string> fixVars;
111  std::vector<double> fixVarValues;
112 
117 
119  RooWorkspace parameterParser;
120 
122  bool quiet;
123 
127  unsigned int split_mode;
128 
130  void varFixer(RooWorkspace* w, bool fix);
132  void varSaver(RooWorkspace* w);
134  void varRestorer(RooWorkspace* w);
135 
137  void doFitEfficiency(RooWorkspace* w, std::string pdfName, RooRealVar& efficiency);
138 
140  void doSBSEfficiency(RooWorkspace* w, RooRealVar& efficiency);
141 
143  void doCntEfficiency(RooWorkspace* w, RooRealVar& efficiency);
144 
146  void createPdf(RooWorkspace* w, std::vector<std::string>& pdfCommands);
147 
149  void setInitialValues(RooWorkspace* w);
150 
152  void saveFitPlot(RooWorkspace* w);
153 
155  void saveDistributionsPlot(RooWorkspace* w);
156 
158  void saveEfficiencyPlots(RooDataSet& eff, const TString& effName, RooArgSet& binnedVariables, RooArgSet& mappedCategories);
159 
161  void makeEfficiencyPlot1D(RooDataSet& eff, RooRealVar& v, const TString& plotName, const TString& plotTitle, const TString& effName, const char *catName = nullptr, int catIndex = -1);
162 
164  void makeEfficiencyPlot2D(RooDataSet& eff, RooRealVar& v1, RooRealVar& v2, const TString& plotName, const TString& plotTitle, const TString& effName, const char *catName = nullptr, int catIndex = -1);
165 
166 };
167 
168 #endif //TagProbeFitter_h
void setSaveDistributionsPlot(bool saveDistributionsPlot_)
void doCntEfficiency(RooWorkspace *w, RooRealVar &efficiency)
calculate the efficiecny by counting in the dataset found in the workspace
std::vector< std::pair< std::pair< std::string, std::string >, std::pair< std::string, std::vector< std::string > > > > expressionVars
expressions computed almost on the fly
void makeEfficiencyPlot1D(RooDataSet &eff, RooRealVar &v, const TString &plotName, const TString &plotTitle, const TString &effName, const char *catName=nullptr, int catIndex=-1)
makes the 1D plot
const double w
Definition: UKUtility.cc:23
void addPdf(std::string pdfName, std::vector< std::string > &pdfCommands)
void setWeightVar(const std::string &weight)
set a variable to be used as weight for a dataset. empty string means no weights. ...
std::string calculateEfficiency(std::string dirName, std::string efficiencyCategory, std::string efficiencyState, std::vector< std::string > &unbinnedVariables, std::map< std::string, std::vector< double > > &binnedReals, std::map< std::string, std::vector< std::string > > &binnedCategories, std::vector< std::string > &binToPDFmap)
calculate the efficiency for a particular binning of the data; it saves everything in the directory "...
void makeEfficiencyPlot2D(RooDataSet &eff, RooRealVar &v1, RooRealVar &v2, const TString &plotName, const TString &plotTitle, const TString &effName, const char *catName=nullptr, int catIndex=-1)
makes the 2D plot
Definition: weight.py:1
bool addThresholdCategory(std::string categoryName, std::string title, std::string varName, double cutValue)
adds a new category based on a cut
std::string weightVar
weight variable (or empy string for no weights)
std::vector< std::string > fixVars
list of variables fo fix (see below)
TChain * inputTree
pointer to the input TTree Chain of data
void setQuiet(bool quiet_=true)
suppress most of the output from RooFit and Minuit
TagProbeFitter(const std::vector< std::string > &inputFileNames, std::string inputDirectoryName, std::string inputTreeName, std::string outputFileName, int numCPU=1, bool saveWorkspace_=false, bool floatShapeParameters=true, const std::vector< std::string > &fixVars_=std::vector< std::string >())
construct the fitter with the inputFileName, inputDirectoryName, inputTreeName, outputFileName and sp...
bool addExpression(std::string expressionName, std::string title, std::string expression, const std::vector< std::string > &arguments)
adds a new category based on a cut
unsigned int split_mode
void saveEfficiencyPlots(RooDataSet &eff, const TString &effName, RooArgSet &binnedVariables, RooArgSet &mappedCategories)
saves the efficiency plots
std::map< std::string, std::vector< std::string > > pdfs
the map of pdf names to the vector of commands to build the pdf
void varSaver(RooWorkspace *w)
store values in the vector
void setBinnedFit(bool binned, int bins=0)
set if to do a binned fit
void varRestorer(RooWorkspace *w)
restore variables&#39;s values for fit starting point
void saveFitPlot(RooWorkspace *w)
saves the fit canvas
~TagProbeFitter()
destructor closes the files
bool saveWorkspace
the default option wether to save the workspace for each bin
bool doSaveDistributionsPlot
save distribution plots
RooWorkspace parameterParser
a RooWorkspace object to parse input parameters with ".factory()"
void setInitialValues(RooWorkspace *w)
sets initial values of the PDF parameters based on the data available in the workspace ...
int numCPU
number of CPUs to use for the fit
int massBins
number of bins to use in mass shape plots; 0 = automatic
void saveDistributionsPlot(RooWorkspace *w)
saves the distributions canvas
bool quiet
suppress most printout
std::vector< std::pair< std::pair< std::string, std::string >, std::pair< std::string, double > > > thresholdCategories
int binnedFit
do binned fit; 0 = automatic, 1 = yes, -1 = no. d
void varFixer(RooWorkspace *w, bool fix)
fix or release variables selected by user
std::vector< double > fixVarValues
void setSplitMode(unsigned int nevents)
TString units(TString variable, Char_t axis)
TFile * outputFile
pointer to the output file
void setBinsForMassPlots(int bins)
set number of bins to use when making the plots; 0 = automatic
void doSBSEfficiency(RooWorkspace *w, RooRealVar &efficiency)
calculate the efficiecny with side band substraction in the dataset found in the workspace ...
void createPdf(RooWorkspace *w, std::vector< std::string > &pdfCommands)
creates the simultaneous PDF in the workspace according to the "pdfCommands"
void doFitEfficiency(RooWorkspace *w, std::string pdfName, RooRealVar &efficiency)
calculate the efficiecny with a simulataneous maximum likelihood fit in the dataset found in the work...
TDirectory * outputDirectory
pointer to the TDirectory in the output file that is the root directory for this fitter ...
RooArgSet variables
the set of variables describing the data in the input TTree
bool addCategory(std::string categoryName, std::string title, std::string expression)
adds a new category variable to the set of variables describing the data in the tree; "expression" is...
bool addVariable(std::string variableName, std::string title, double low, double hi, std::string units)
adds a new real variable to the set of variables describing the data in the tree
void addFixedVariavles(const std::vector< std::string > &)
set a list of variables to fix during first fit iteration. If the list is empty, do one iteration...