CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
List of all members | Public Member Functions | Protected Member Functions | Protected Attributes
TagProbeFitter Class Reference

#include <TagProbeFitter.h>

Public Member Functions

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 parsed by factory() More...
 
bool addExpression (std::string expressionName, std::string title, std::string expression, std::vector< std::string > arguments)
 adds a new category based on a cut More...
 
void addFixedVariavles (std::vector< string >)
 set a list of variables to fix during first fit iteration. If the list is empty, do one iteration. More...
 
void addPdf (std::string pdfName, std::vector< std::string > &pdfCommands)
 
bool addThresholdCategory (std::string categoryName, std::string title, std::string varName, double cutValue)
 adds a new category based on a cut More...
 
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 More...
 
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 "dirName", uses the previously defined PDF with name "pdfName" More...
 
std::string calculateEfficiency (std::string dirName, std::vector< std::string > efficiencyCategories, 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)
 
void setBinnedFit (bool binned, int bins=0)
 set if to do a binned fit More...
 
void setBinsForMassPlots (int bins)
 set number of bins to use when making the plots; 0 = automatic More...
 
void setQuiet (bool quiet_=true)
 suppress most of the output from RooFit and Minuit More...
 
void setWeightVar (const std::string &weight)
 set a variable to be used as weight for a dataset. empty string means no weights. More...
 
 TagProbeFitter (std::vector< std::string > inputFileNames, std::string inputDirectoryName, std::string inputTreeName, std::string outputFileName, int numCPU=1, bool saveWorkspace_=false, bool floatShapeParameters=true, std::vector< std::string > fixVars_=std::vector< std::string >())
 construct the fitter with the inputFileName, inputDirectoryName, inputTreeName, outputFileName and specify wether to save the workspace with data for each bin More...
 
 ~TagProbeFitter ()
 destructor closes the files More...
 

Protected Member Functions

void createPdf (RooWorkspace *w, std::vector< std::string > &pdfCommands)
 creates the simultaneous PDF in the workspace according to the "pdfCommands" More...
 
void doCntEfficiency (RooWorkspace *w, RooRealVar &efficiency)
 calculate the efficiecny by counting in the dataset found in the workspace More...
 
void doFitEfficiency (RooWorkspace *w, std::string pdfName, RooRealVar &efficiency)
 calculate the efficiecny with a simulataneous maximum likelihood fit in the dataset found in the workspace with PDF pdfName More...
 
void doSBSEfficiency (RooWorkspace *w, RooRealVar &efficiency)
 calculate the efficiecny with side band substraction in the dataset found in the workspace More...
 
void makeEfficiencyPlot1D (RooDataSet &eff, RooRealVar &v, TString plotName, TString plotTitle, TString effName)
 makes the 1D plot More...
 
void makeEfficiencyPlot2D (RooDataSet &eff, RooRealVar &v1, RooRealVar &v2, TString plotName, TString plotTitle, TString effName)
 makes the 2D plot More...
 
void saveDistributionsPlot (RooWorkspace *w)
 saves the distributions canvas More...
 
void saveEfficiencyPlots (RooDataSet &eff, TString effName, RooArgSet &binnedVariables, RooArgSet &mappedCategories)
 saves the efficiency plots More...
 
void saveFitPlot (RooWorkspace *w)
 saves the fit canvas More...
 
void setInitialValues (RooWorkspace *w)
 sets initial values of the PDF parameters based on the data available in the workspace More...
 
void varFixer (RooWorkspace *w, bool fix)
 fix or release variables selected by user More...
 
void varRestorer (RooWorkspace *w)
 restore variables's values for fit starting point More...
 
void varSaver (RooWorkspace *w)
 store values in the vector More...
 

Protected Attributes

int binnedFit
 do binned fit; 0 = automatic, 1 = yes, -1 = no. d More...
 
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 More...
 
std::vector< std::string > fixVars
 list of variables fo fix (see below) More...
 
std::vector< double > fixVarValues
 
bool floatShapeParameters
 
TChain * inputTree
 pointer to the input TTree Chain of data More...
 
int massBins
 number of bins to use in mass shape plots; 0 = automatic More...
 
int numCPU
 number of CPUs to use for the fit More...
 
TDirectory * outputDirectory
 pointer to the TDirectory in the output file that is the root directory for this fitter More...
 
TFile * outputFile
 pointer to the output file More...
 
RooWorkspace parameterParser
 a RooWorkspace object to parse input parameters with ".factory()" More...
 
std::map< std::string,
std::vector< std::string > > 
pdfs
 the map of pdf names to the vector of commands to build the pdf More...
 
bool quiet
 suppress most printout More...
 
bool saveWorkspace
 the default option wether to save the workspace for each bin More...
 
std::vector< std::pair
< std::pair< std::string,
std::string >, std::pair
< std::string, double > > > 
thresholdCategories
 
RooArgSet variables
 the set of variables describing the data in the input TTree More...
 
std::string weightVar
 weight variable (or empy string for no weights) More...
 

Detailed Description

Definition at line 11 of file TagProbeFitter.h.

Constructor & Destructor Documentation

TagProbeFitter::TagProbeFitter ( std::vector< std::string >  inputFileNames,
std::string  inputDirectoryName,
std::string  inputTreeName,
std::string  outputFileName,
int  numCPU = 1,
bool  saveWorkspace_ = false,
bool  floatShapeParameters = true,
std::vector< std::string >  fixVars_ = std::vector<std::string>() 
)

construct the fitter with the inputFileName, inputDirectoryName, inputTreeName, outputFileName and specify wether to save the workspace with data for each bin

Definition at line 44 of file TagProbeFitter.cc.

References gather_cfg::cout, i, jptDQMConfig_cff::massBins, and download_sqlite_cfg::outputFile.

44  {
45  inputTree = new TChain((inputDirectoryName+"/"+inputTreeName).c_str());
46  for(size_t i=0; i<inputFileNames.size(); i++){
47  inputTree->Add(inputFileNames[i].c_str());
48  }
49  outputFile = new TFile(outputFileName.c_str(),"recreate");
50  outputDirectory = outputFile->mkdir(inputDirectoryName.c_str());
51  numCPU = numCPU_;
52  saveWorkspace = saveWorkspace_;
53  massBins = 0; // automatic default
54  floatShapeParameters = floatShapeParameters_;
55  fixVars = fixVars_;
56  weightVar = "";
57  if(!floatShapeParameters && fixVars.empty()) std::cout << "TagProbeFitter: " << "You wnat to fix some variables but do not specify them!";
58 
59  gROOT->SetStyle("Plain");
60  gStyle->SetTitleFillColor(0);
61  gStyle->SetPalette(1);
62  gStyle->SetOptStat(0);
63  gStyle->SetPaintTextFormat(".2f");
64 
65  quiet = false;
66 
67  binnedFit = false;
68 }
int i
Definition: DBlmapReader.cc:9
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
bool saveWorkspace
the default option wether to save the workspace for each bin
int numCPU
number of CPUs to use for the fit
int massBins
number of bins to use in mass shape plots; 0 = automatic
bool quiet
suppress most printout
int binnedFit
do binned fit; 0 = automatic, 1 = yes, -1 = no. d
TFile * outputFile
pointer to the output file
tuple cout
Definition: gather_cfg.py:121
TDirectory * outputDirectory
pointer to the TDirectory in the output file that is the root directory for this fitter ...
TagProbeFitter::~TagProbeFitter ( )

destructor closes the files

Definition at line 70 of file TagProbeFitter.cc.

References download_sqlite_cfg::outputFile.

70  {
71  if(inputTree)
72  delete inputTree;
73  if(outputFile)
74  outputFile->Close();
75 }
TChain * inputTree
pointer to the input TTree Chain of data
TFile * outputFile
pointer to the output file

Member Function Documentation

bool TagProbeFitter::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 parsed by factory()

Definition at line 90 of file TagProbeFitter.cc.

References trackerHits::c, and makeLayoutFileForGui::variables.

Referenced by TagProbeFitTreeAnalyzer::TagProbeFitTreeAnalyzer().

90  {
91  RooCategory* c = (RooCategory*) parameterParser.factory(expression.c_str());
92  if(!c)
93  return false;
94  //set the name of the category to the passed name instead of the one in the expression
95  c->SetName(name.c_str());
96  c->SetTitle(title.c_str());
97  variables.addClone(*c);
98  return true;
99 }
RooWorkspace parameterParser
a RooWorkspace object to parse input parameters with &quot;.factory()&quot;
RooArgSet variables
the set of variables describing the data in the input TTree
bool TagProbeFitter::addExpression ( std::string  expressionName,
std::string  title,
std::string  expression,
std::vector< std::string >  arguments 
)

adds a new category based on a cut

Definition at line 101 of file TagProbeFitter.cc.

Referenced by TagProbeFitTreeAnalyzer::TagProbeFitTreeAnalyzer().

101  {
102  expressionVars.push_back(make_pair(make_pair(expressionName,title), make_pair(expression,arguments)));
103  return true;
104 }
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 TagProbeFitter::addFixedVariavles ( std::vector< string >  )

set a list of variables to fix during first fit iteration. If the list is empty, do one iteration.

void TagProbeFitter::addPdf ( std::string  pdfName,
std::vector< std::string > &  pdfCommands 
)

add a new PDF to the list of available PDFs; "pdfCommands" are parsed by factory(). the user needs to define efficiency[0.9,0,1] for the initial value, "signal" PDF, "backgroundPass" PDF and "backgroundFail" PDF

Definition at line 113 of file TagProbeFitter.cc.

References mergeVDriftHistosByStation::name.

Referenced by TagProbeFitTreeAnalyzer::TagProbeFitTreeAnalyzer().

113  {
114  pdfs[name] = pdfCommands;
115 }
std::map< std::string, std::vector< std::string > > pdfs
the map of pdf names to the vector of commands to build the pdf
bool TagProbeFitter::addThresholdCategory ( std::string  categoryName,
std::string  title,
std::string  varName,
double  cutValue 
)

adds a new category based on a cut

Definition at line 107 of file TagProbeFitter.cc.

Referenced by TagProbeFitTreeAnalyzer::TagProbeFitTreeAnalyzer().

107  {
108  thresholdCategories.push_back(make_pair(make_pair(categoryName,title), make_pair(varName,cutValue)));
109  return true;
110 }
std::vector< std::pair< std::pair< std::string, std::string >, std::pair< std::string, double > > > thresholdCategories
bool TagProbeFitter::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

Definition at line 85 of file TagProbeFitter.cc.

References makeLayoutFileForGui::variables.

Referenced by TagProbeFitTreeAnalyzer::TagProbeFitTreeAnalyzer().

85  {
86  variables.addClone(RooRealVar(name.c_str(), title.c_str(), low, hi, units.c_str()));
87  return true;
88 }
TString units(TString variable, Char_t axis)
RooArgSet variables
the set of variables describing the data in the input TTree
std::string TagProbeFitter::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 
)
inline

calculate the efficiency for a particular binning of the data; it saves everything in the directory "dirName", uses the previously defined PDF with name "pdfName"

Definition at line 39 of file TagProbeFitter.h.

Referenced by TagProbeFitTreeAnalyzer::calculateEfficiency().

39  {
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  }
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 &quot;...
std::string TagProbeFitter::calculateEfficiency ( std::string  dirName,
std::vector< std::string >  efficiencyCategories,
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 
)
void TagProbeFitter::createPdf ( RooWorkspace *  w,
std::vector< std::string > &  pdfCommands 
)
protected

creates the simultaneous PDF in the workspace according to the "pdfCommands"

Definition at line 484 of file TagProbeFitter.cc.

References edmPickEvents::command, and i.

484  {
485  // create the signal and background pdfs defined by the user
486  for(unsigned int i=0; i<pdfCommands.size(); i++){
487  const std::string & command = pdfCommands[i];
488  if (command.find("#import ") == 0) {
489  TDirectory *here = gDirectory;
490  w->import(command.substr(8).c_str());
491  here->cd();
492  } else {
493  TDirectory *here = gDirectory;
494  w->factory(command.c_str());
495  here->cd();
496  }
497  }
498  // setup the simultaneous extended pdf
499  w->factory("expr::numSignalPass('efficiency*numSignalAll', efficiency, numSignalAll[0.,1e10])");
500  w->factory("expr::numSignalFail('(1-efficiency)*numSignalAll', efficiency, numSignalAll)");
501  TString sPass = "signal", sFail = "signal";
502  if (w->pdf("signalPass") != 0 && w->pdf("signalFail") != 0) {
503  if (w->pdf("signal") != 0) throw std::logic_error("You must either define one 'signal' PDF or two PDFs ('signalPass', 'signalFail'), not both!");
504  sPass = "signalPass"; sFail = "signalFail";
505  } else if (w->pdf("signal") != 0) {
506  if (w->pdf("signalPass") != 0 || w->pdf("signalFail") != 0) {
507  throw std::logic_error("You must either define one 'signal' PDF or two PDFs ('signalPass', 'signalFail'), not both!");
508  }
509  } else {
510  throw std::logic_error("You must either define one 'signal' PDF or two PDFs ('signalPass', 'signalFail')");
511  }
512  w->factory("SUM::pdfPass(numSignalPass*"+sPass+", numBackgroundPass[0.,1e10]*backgroundPass)");
513  w->factory("SUM::pdfFail(numSignalFail*"+sFail+", numBackgroundFail[0.,1e10]*backgroundFail)");
514  w->factory("SIMUL::simPdf(_efficiencyCategory_, Passed=pdfPass, Failed=pdfFail)");
515  // signalFractionInPassing is not used in the fit just to set the initial values
516  if (w->pdf("simPdf") == 0) throw std::runtime_error("Could not create simultaneous fit pdf.");
517  if(w->var("signalFractionInPassing") == 0)
518  w->factory("signalFractionInPassing[0.9]");
519 }
int i
Definition: DBlmapReader.cc:9
T w() const
void TagProbeFitter::doCntEfficiency ( RooWorkspace *  w,
RooRealVar &  efficiency 
)
protected

calculate the efficiecny by counting in the dataset found in the workspace

Definition at line 761 of file TagProbeFitter.cc.

References alpha, alignCSCRings::e, and cmsPerfPublish::fail().

761  {
762  int pass = w->data("data")->sumEntries("_efficiencyCategory_==_efficiencyCategory_::Passed");
763  int fail = w->data("data")->sumEntries("_efficiencyCategory_==_efficiencyCategory_::Failed");
764  double e = (pass+fail == 0) ? 0 : pass/double(pass+fail);
765  // Use Clopper-Pearson
766  double alpha = (1.0 - .68540158589942957)/2;
767  double lo = (pass == 0) ? 0.0 : ROOT::Math::beta_quantile( alpha, pass, fail+1 );
768  double hi = (fail == 0) ? 1.0 : ROOT::Math::beta_quantile( 1-alpha, pass+1, fail );
770  //double lob, hib;
771  //Efficiency( pass, pass+fail, .68540158589942957, e, lob, hib );
772  //std::cerr << "CNT " << pass << "/" << fail << ": Clopper Pearson [" << lo << ", " << hi << "], Bayes [" << lob << ", " << hib << "]" << std::endl;
773  efficiency.setVal(e);
774  efficiency.setAsymError(lo-e, hi-e);
775 }
float alpha
Definition: AMPTWrapper.h:95
T w() const
void TagProbeFitter::doFitEfficiency ( RooWorkspace *  w,
std::string  pdfName,
RooRealVar &  efficiency 
)
protected

calculate the efficiecny with a simulataneous maximum likelihood fit in the dataset found in the workspace with PDF pdfName

—> if "floatShapeParameters==false" && "fixVars is NOT empty" : Perform a global fit to the whole sample, save the fitted values of the user specified parameters, and fix them for bin-by-bin fit.

Definition at line 359 of file TagProbeFitter.cc.

References dtNoiseDBValidation_cfg::cerr, data, and alignCSCRings::e.

359  {
360  //if pdfName is empty skip the fit
361  if(pdfName == ""){
362  return;
363  }
364  //create the simultaneous pdf of name pdfName
365  createPdf(w, pdfs[pdfName]);
366  //set the initial values for the yields of signal and background
368  RooFitResult* res = 0;
369 
370  RooAbsData *data = w->data("data");
371  if (binnedFit) {
372  // get variables from data, which contain also other binning or expression variables
373  const RooArgSet *dataObs = data->get(0);
374  // remove everything which is not a dependency of the pdf
375  RooArgSet *obs = w->pdf("simPdf")->getObservables(dataObs);
376  RooDataHist *bdata = new RooDataHist("data_binned", "data_binned", *obs, *data);
377  w->import(*bdata);
378  data = bdata;
379  delete obs;
380  }
381 
382  double totPassing = data->sumEntries("_efficiencyCategory_==_efficiencyCategory_::Passed");
383  double totFailing = data->sumEntries("_efficiencyCategory_==_efficiencyCategory_::Failed");
384 
385  //******* The block of code below is to make the fit converge faster.
386  // ****** This part is OPTIONAL, i.e., off be default. User can activate this
387  // ****** by setting the following parameters: "fixVars" and "floatShapeParameters"
388  // ****** Here is the full logic:
399 
400 
401  if(!fixVars.empty()){
402  // calculate initial values for parameters user want to fix
403  if(!floatShapeParameters && fixVarValues.empty()){
404  // we want to fix these parameters for each bin.
405  // the following sequence fixes them, fits, releases and fits again
406  // to get reasonable values.
407  // ----------------------------------------------------------------------
408  // This procedure works only once with a whole dataset (without binning)
409  // ----------------------------------------------------------------------
410 
411  // fix them
412  varFixer(w,true);
413  //do fit
414  w->pdf("simPdf")->fitTo(*data, Save(true), Extended(true), NumCPU(numCPU), PrintLevel(quiet?-1:1), PrintEvalErrors(quiet?-1:1), Warnings(!quiet));
415  //release vars
416  varFixer(w,false);
417  //do fit
418  w->pdf("simPdf")->fitTo(*data, Save(true), Extended(true), NumCPU(numCPU), PrintLevel(quiet?-1:1), PrintEvalErrors(quiet?-1:1), Warnings(!quiet));
419  //save vars
420  varSaver(w);
421  // now we have a starting point. Fit will converge faster.
422  }
423 
424  // here we can use initial values if we want (this works for each bin)
425  if(!floatShapeParameters) varRestorer(w); //restore vars
426 
427 
428  // if we don't want to "floatShapeParameters" we just fix, fit,
429  // release, and fit again. No need for global fitting above.
430  //fix vars
431  varFixer(w,true);
432  //do fit
433  res = w->pdf("simPdf")->fitTo(*data, Save(true), Extended(true), NumCPU(numCPU), Minos(*w->var("efficiency")), PrintLevel(quiet?-1:1), PrintEvalErrors(quiet?-1:1), Warnings(!quiet));
434  }//if(!fixVars.empty())
435 
436  // (default = true) if we don't want to fix any parameters or want to fit each bin with all parameters floating
438  //release vars
439  varFixer(w,false);
440 
441  //do fit
442  res = w->pdf("simPdf")->fitTo(*data, Save(true), Extended(true), NumCPU(numCPU), Minos(*w->var("efficiency")), PrintLevel(quiet?-1:1), PrintEvalErrors(quiet?-1:1), Warnings(!quiet));
443  }
444 
445 
446 
447  // save everything
448  res->Write("fitresults");
449  w->saveSnapshot("finalState",w->components());
450  saveFitPlot(w);
451  //extract the efficiency parameter from the results
452  RooRealVar* e = (RooRealVar*) res->floatParsFinal().find("efficiency");
453  //What's wrong with this? and why don't they copy the errors!
454  //efficiency = *e;
455  efficiency.setVal(e->getVal());
456  Double_t errLo = e->getErrorLo(), errHi = e->getErrorHi();
457  if (errLo == 0 && e->getVal() < 0.5) errLo = e->getMin()-e->getVal();
458  if (errHi == 0 && e->getVal() > 0.5) errHi = e->getMax()-e->getVal();
459  efficiency.setAsymError(errLo, errHi);
460 
461  if (totPassing * totFailing == 0) {
462  RooRealVar* nS = (RooRealVar*) res->floatParsFinal().find("numSignalAll");
463  //RooRealVar* nB = (RooRealVar*) res->floatParsFinal().find(totPassing != 0 ? "numBackgroundPass" : "numBackgroundFail");
464  double cerr = ROOT::Math::beta_quantile( 1-(1.0-.68540158589942957)/2, 1, nS->getVal() );
465  /*
466  std::cout << "======================================================================================" << std::endl;
467  std::cout << "======= totPassing " << totPassing << ", totFailing " << totFailing << std::endl;
468  std::cout << "======= FIT: e " << e->getVal() << ", e Lo " << e->getErrorLo() << ", e Hi " << e->getErrorHi() << std::endl;
469  std::cout << "======= FIT:nS " << nS->getVal() << ", nS Lo " << nS->getErrorLo() << ", nS Hi " << nS->getErrorHi() << std::endl;
470  std::cout << "======= FIT:nB " << nB->getVal() << ", nB Lo " << nB->getErrorLo() << ", nB Hi " << nB->getErrorHi() << std::endl;
471  std::cout << "======= CNT: " << cerr << std::endl;
472  std::cout << "======================================================================================" << std::endl;
473  */
474  if (totPassing == 0) {
475  efficiency.setVal(0);
476  efficiency.setAsymError(0,cerr);
477  } else {
478  efficiency.setVal(1);
479  efficiency.setAsymError(-cerr,0);
480  }
481  }
482 }
std::vector< std::string > fixVars
list of variables fo fix (see below)
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 varRestorer(RooWorkspace *w)
restore variables&#39;s values for fit starting point
void saveFitPlot(RooWorkspace *w)
saves the fit canvas
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
bool quiet
suppress most printout
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
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
void createPdf(RooWorkspace *w, std::vector< std::string > &pdfCommands)
creates the simultaneous PDF in the workspace according to the &quot;pdfCommands&quot;
T w() const
void TagProbeFitter::doSBSEfficiency ( RooWorkspace *  w,
RooRealVar &  efficiency 
)
protected

calculate the efficiecny with side band substraction in the dataset found in the workspace

Definition at line 758 of file TagProbeFitter.cc.

758  {
759 }
void TagProbeFitter::makeEfficiencyPlot1D ( RooDataSet &  eff,
RooRealVar &  v,
TString  plotName,
TString  plotTitle,
TString  effName 
)
protected

makes the 1D plot

Definition at line 721 of file TagProbeFitter.cc.

References svgfig::canvas(), alignCSCRings::e, AlCaHLTBitMon_ParallelJobs::p, and runtimedef::set().

721  {
722  TCanvas canvas(plotName);
723  const RooArgSet* set = eff.get();
724  RooRealVar* e = (RooRealVar*) set->find("efficiency");
725  RooPlot* p = v.frame(Name(plotName), Title(plotTitle));
726  eff.plotOnXY(p,YVar(*e));
727  p->SetYTitle(TString("Efficiency of ")+effName);
728  p->SetAxisRange(0,1,"Y");
729  p->Draw();
730  canvas.Write();
731  delete p;
732 }
def canvas
Definition: svgfig.py:481
mathSSE::Vec4< T > v
void set(const std::string &name, int value)
set the flag, with a run-time name
void TagProbeFitter::makeEfficiencyPlot2D ( RooDataSet &  eff,
RooRealVar &  v1,
RooRealVar &  v2,
TString  plotName,
TString  plotTitle,
TString  effName 
)
protected

makes the 2D plot

Definition at line 734 of file TagProbeFitter.cc.

References svgfig::canvas(), Data::Data(), alignCSCRings::e, h, i, and runtimedef::set().

734  {
735  TCanvas canvas(plotName);
736  canvas.SetRightMargin(0.15);
737  TH2F* h = new TH2F(plotName, plotName, v1.getBinning().numBins(), v1.getBinning().array(), v2.getBinning().numBins(), v2.getBinning().array());
738  const RooArgSet* set = eff.get();
739  RooRealVar* e = (RooRealVar*) set->find("efficiency");
740  RooRealVar* v1_ = (RooRealVar*) set->find(v1.GetName());
741  RooRealVar* v2_ = (RooRealVar*) set->find(v2.GetName());
742  h->SetTitle(TString::Format("%s;%s%s;%s%s;Efficiency of %s", plotTitle.Data(),
743  v1.GetTitle(), TString(v1.getUnit()).Length()==0?"":TString::Format(" (%s)", v1.getUnit()).Data(),
744  v2.GetTitle(), TString(v2.getUnit()).Length()==0?"":TString::Format(" (%s)", v2.getUnit()).Data(), effName.Data()));
745  h->SetOption("colztexte");
746  h->GetZaxis()->SetRangeUser(-0.001,1.001);
747  h->SetStats(kFALSE);
748  for(int i=0; i<eff.numEntries(); i++){
749  eff.get(i);
750  h->SetBinContent(h->FindBin(v1_->getVal(), v2_->getVal()), e->getVal());
751  h->SetBinError(h->FindBin(v1_->getVal(), v2_->getVal()), (e->getErrorHi()-e->getErrorLo())/2.);
752  }
753  h->Draw();
754  canvas.Write();
755  delete h;
756 }
int i
Definition: DBlmapReader.cc:9
def canvas
Definition: svgfig.py:481
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
void set(const std::string &name, int value)
set the flag, with a run-time name
void TagProbeFitter::saveDistributionsPlot ( RooWorkspace *  w)
protected

saves the distributions canvas

Definition at line 626 of file TagProbeFitter.cc.

References svgfig::canvas(), i, and v.

626  {
627  // save pointers to datasets to manage memory
628  RooAbsData* dataAll = w->data("data");
629  RooAbsData* dataPass = dataAll->reduce(Cut("_efficiencyCategory_==_efficiencyCategory_::Passed"));
630  RooAbsData* dataFail = dataAll->reduce(Cut("_efficiencyCategory_==_efficiencyCategory_::Failed"));
631 
632  const RooArgSet* vars = dataAll->get();
633  vector<RooRealVar*> reals;
634  TIterator* it = vars->createIterator();
635  for(RooAbsArg* v = (RooAbsArg*)it->Next(); v!=0; v = (RooAbsArg*)it->Next() ){
636  if(!v->InheritsFrom("RooRealVar")) continue;
637  reals.push_back((RooRealVar*)v);
638  }
639  TCanvas canvas("distributions_canvas");
640  canvas.Divide(3,reals.size());
641  vector<RooPlot*> frames;
642  for(unsigned int i=0; i<reals.size(); i++){
643  // plot the Passing Probes
644  canvas.cd(3*i+1);
645  frames.push_back(reals[i]->frame(Name("Passing"), Title("Passing Probes"), Bins(100)));
646  dataPass->plotOn(frames.back(), LineColor(kGreen));
647  dataPass->statOn(frames.back());
648  frames.back()->Draw();
649  // plot the Failing Probes
650  canvas.cd(3*i+2);
651  frames.push_back(reals[i]->frame(Name("Failing"), Title("Failing Probes"), Bins(100)));
652  dataFail->plotOn(frames.back(), LineColor(kRed));
653  dataFail->statOn(frames.back());
654  frames.back()->Draw();
655  // plot the All Probes
656  canvas.cd(3*i+3);
657  frames.push_back(reals[i]->frame(Name("All"), Title("All Probes"), Bins(100)));
658  dataAll->plotOn(frames.back(), LineColor(kBlue));
659  dataAll->statOn(frames.back());
660  frames.back()->Draw();
661  }
662  canvas.Write();
663  for (size_t i=0; i<frames.size(); i++) {
664  delete frames[i];
665  }
666  delete dataPass;
667  delete dataFail;
668 }
int i
Definition: DBlmapReader.cc:9
def canvas
Definition: svgfig.py:481
mathSSE::Vec4< T > v
T w() const
void TagProbeFitter::saveEfficiencyPlots ( RooDataSet &  eff,
TString  effName,
RooArgSet &  binnedVariables,
RooArgSet &  mappedCategories 
)
protected

saves the efficiency plots

Definition at line 670 of file TagProbeFitter.cc.

References lumiQTWidget::t.

670  {
671  TIterator* v1it = binnedVariables.createIterator();
672  for(RooRealVar* v1 = (RooRealVar*)v1it->Next(); v1!=0; v1 = (RooRealVar*)v1it->Next() ){
673  RooArgSet binCategories1D;
674  TIterator* v2it = binnedVariables.createIterator();
675  for(RooRealVar* v2 = (RooRealVar*)v2it->Next(); v2!=0; v2 = (RooRealVar*)v2it->Next() ){
676  if(v2 == v1) continue;
677  binCategories1D.addClone( RooBinningCategory(TString(v2->GetName())+"_bins", TString(v2->GetName())+"_bins", *v2) );
678 
679  RooArgSet binCategories2D;
680  TIterator* v3it = binnedVariables.createIterator();
681  for(RooRealVar* v3 = (RooRealVar*)v3it->Next(); v3!=0; v3 = (RooRealVar*)v3it->Next() ){
682  if(v3 == v1 || v3 == v2) continue;
683  binCategories2D.addClone( RooBinningCategory(TString(v3->GetName())+"_bins", TString(v3->GetName())+"_bins", *v3) );
684  }
685  RooMultiCategory allCats2D("allCats2D", "allCats2D", RooArgSet(binCategories2D, mappedCategories));
686  if(allCats2D.numTypes()==0){
687  makeEfficiencyPlot2D(eff, *v1, *v2, TString::Format("%s_%s_PLOT", v1->GetName(), v2->GetName()), "", effName);
688  }else{
689  RooDataSet myEff(eff);
690  myEff.addColumn(allCats2D);
691  TIterator* catIt = allCats2D.typeIterator();
692  for(RooCatType* t = (RooCatType*)catIt->Next(); t!=0; t = (RooCatType*)catIt->Next() ){
693  TString catName = t->GetName();
694  if(catName.Contains("NotMapped")) continue;
695  catName.ReplaceAll("{","").ReplaceAll("}","").ReplaceAll(";","_&_");
696  RooDataSet* eff_bin = (RooDataSet*) myEff.reduce( Cut(TString::Format("allCats2D==%d",t->getVal())) );
697  makeEfficiencyPlot2D(*eff_bin, *v1, *v2, TString::Format("%s_%s_PLOT_%s",v1->GetName(), v2->GetName(), catName.Data()), catName, effName);
698  delete eff_bin;
699  }
700  }
701  }
702  RooMultiCategory allCats1D("allCats1D", "allCats1D", RooArgSet(binCategories1D, mappedCategories));
703  if(allCats1D.numTypes()==0){
704  makeEfficiencyPlot1D(eff, *v1, TString::Format("%s_PLOT", v1->GetName()), "", effName);
705  }else{
706  RooDataSet myEff(eff);
707  myEff.addColumn(allCats1D);
708  TIterator* catIt = allCats1D.typeIterator();
709  for(RooCatType* t = (RooCatType*)catIt->Next(); t!=0; t = (RooCatType*)catIt->Next() ){
710  TString catName = t->GetName();
711  if(catName.Contains("NotMapped")) continue;
712  catName.ReplaceAll("{","").ReplaceAll("}","").ReplaceAll(";","_&_");
713  RooDataSet* eff_bin = (RooDataSet*) myEff.reduce( Cut(TString::Format("allCats1D==%d",t->getVal())) );
714  makeEfficiencyPlot1D(*eff_bin, *v1, TString::Format("%s_PLOT_%s", v1->GetName(), catName.Data()), catName, effName);
715  delete eff_bin;
716  }
717  }
718  }
719 }
void makeEfficiencyPlot1D(RooDataSet &eff, RooRealVar &v, TString plotName, TString plotTitle, TString effName)
makes the 1D plot
void makeEfficiencyPlot2D(RooDataSet &eff, RooRealVar &v1, RooRealVar &v2, TString plotName, TString plotTitle, TString effName)
makes the 2D plot
void TagProbeFitter::saveFitPlot ( RooWorkspace *  w)
protected

saves the fit canvas

Definition at line 564 of file TagProbeFitter.cc.

References svgfig::canvas(), i, scaleCards::mass, jptDQMConfig_cff::massBins, and v.

564  {
565  // save refferences for convenience
566  RooCategory& efficiencyCategory = *w->cat("_efficiencyCategory_");
567  RooAbsData* dataAll = (binnedFit ? w->data("data_binned") : w->data("data") );
568  RooAbsData* dataPass = dataAll->reduce(Cut("_efficiencyCategory_==_efficiencyCategory_::Passed"));
569  RooAbsData* dataFail = dataAll->reduce(Cut("_efficiencyCategory_==_efficiencyCategory_::Failed"));
570  RooAbsPdf& pdf = *w->pdf("simPdf");
571  RooArgSet *obs = pdf.getObservables(*dataAll);
572  RooRealVar* mass = 0;
573  TIterator* it = obs->createIterator();
574  for(RooAbsArg* v = (RooAbsArg*)it->Next(); v!=0; v = (RooAbsArg*)it->Next() ){
575  if(!v->InheritsFrom("RooRealVar")) continue;
576  mass = (RooRealVar*)v;
577  break;
578  }
579  if(!mass) return;
580  // make a 2x2 canvas
581  TCanvas canvas("fit_canvas");
582  canvas.Divide(2,2);
583  vector<RooPlot*> frames;
584  // plot the Passing Probes
585  canvas.cd(1);
586  if (massBins == 0) {
587  frames.push_back(mass->frame(Name("Passing"), Title("Passing Probes")));
588  frames.push_back(mass->frame(Name("Failing"), Title("Failing Probes")));
589  frames.push_back(mass->frame(Name("All"), Title("All Probes")));
590  } else {
591  frames.push_back(mass->frame(Name("Passing"), Title("Passing Probes"), Bins(massBins)));
592  frames.push_back(mass->frame(Name("Failing"), Title("Failing Probes"), Bins(massBins)));
593  frames.push_back(mass->frame(Name("All"), Title("All Probes"), Bins(massBins)));
594  }
595  dataPass->plotOn(frames[0]);
596  pdf.plotOn(frames[0], Slice(efficiencyCategory, "Passed"), ProjWData(*dataPass), LineColor(kGreen));
597  pdf.plotOn(frames[0], Slice(efficiencyCategory, "Passed"), ProjWData(*dataPass), LineColor(kGreen), Components("backgroundPass"), LineStyle(kDashed));
598  frames[0]->Draw();
599  // plot the Failing Probes
600  canvas.cd(2);
601  dataFail->plotOn(frames[1]);
602  pdf.plotOn(frames[1], Slice(efficiencyCategory, "Failed"), ProjWData(*dataFail), LineColor(kRed));
603  pdf.plotOn(frames[1], Slice(efficiencyCategory, "Failed"), ProjWData(*dataFail), LineColor(kRed), Components("backgroundFail"), LineStyle(kDashed));
604  frames[1]->Draw();
605  // plot the All Probes
606  canvas.cd(3);
607  dataAll->plotOn(frames.back());
608  pdf.plotOn(frames.back(), ProjWData(*dataAll), LineColor(kBlue));
609  pdf.plotOn(frames.back(), ProjWData(*dataAll), LineColor(kBlue), Components("backgroundPass,backgroundFail"), LineStyle(kDashed));
610  frames.back()->Draw();
611  // plot the Fit Results
612  canvas.cd(4);
613  frames.push_back(mass->frame(Name("Fit Results"), Title("Fit Results")));
614  pdf.paramOn(frames.back(), dataAll, "", 0, "NELU", 0.1, 0.9, 0.9);
615  // draw only the parameter box not the whole frame
616  frames.back()->findObject(Form("%s_paramBox",pdf.GetName()))->Draw();
617  //save and clean up
618  canvas.Write();
619  for (size_t i=0; i<frames.size(); i++) {
620  delete frames[i];
621  }
622  delete dataPass;
623  delete dataFail;
624 }
int i
Definition: DBlmapReader.cc:9
def canvas
Definition: svgfig.py:481
int massBins
number of bins to use in mass shape plots; 0 = automatic
int binnedFit
do binned fit; 0 = automatic, 1 = yes, -1 = no. d
tuple mass
Definition: scaleCards.py:27
mathSSE::Vec4< T > v
T w() const
void TagProbeFitter::setBinnedFit ( bool  binned,
int  bins = 0 
)
inline

set if to do a binned fit

Definition at line 48 of file TagProbeFitter.h.

References binnedFit, combineCards::bins, and massBins.

Referenced by TagProbeFitTreeAnalyzer::TagProbeFitTreeAnalyzer().

48 { binnedFit = binned; massBins = bins; }
int massBins
number of bins to use in mass shape plots; 0 = automatic
int binnedFit
do binned fit; 0 = automatic, 1 = yes, -1 = no. d
void TagProbeFitter::setBinsForMassPlots ( int  bins)

set number of bins to use when making the plots; 0 = automatic

Definition at line 117 of file TagProbeFitter.cc.

References combineCards::bins, and jptDQMConfig_cff::massBins.

Referenced by TagProbeFitTreeAnalyzer::TagProbeFitTreeAnalyzer().

117  {
118  massBins = bins;
119 }
int massBins
number of bins to use in mass shape plots; 0 = automatic
void TagProbeFitter::setInitialValues ( RooWorkspace *  w)
protected

sets initial values of the PDF parameters based on the data available in the workspace

Definition at line 521 of file TagProbeFitter.cc.

521  {
522  // calculate initial values
523  double signalEfficiency = w->var("efficiency")->getVal();
524  double signalFractionInPassing = w->var("signalFractionInPassing")->getVal();
525  double totPassing = w->data("data")->sumEntries("_efficiencyCategory_==_efficiencyCategory_::Passed");
526  double totFailinging = w->data("data")->sumEntries("_efficiencyCategory_==_efficiencyCategory_::Failed");
527  double numSignalAll = totPassing*signalFractionInPassing/signalEfficiency;
528  // check if this value is inconsistent on the failing side
529  if(numSignalAll*(1-signalEfficiency) > totFailinging)
530  numSignalAll = totFailinging;
531  // now set the values
532  w->var("numSignalAll")->setVal(numSignalAll);
533  w->var("numBackgroundPass")->setVal(totPassing - numSignalAll*signalEfficiency);
534  w->var("numBackgroundFail")->setVal(totFailinging - numSignalAll*(1-signalEfficiency));
535 
536  if (totPassing == 0) {
537  w->var("efficiency")->setVal(0.0);
538  w->var("efficiency")->setAsymError(0,1);
539  w->var("efficiency")->setConstant(false);
540  w->var("numBackgroundPass")->setVal(0.0);
541  w->var("numBackgroundPass")->setConstant(true);
542  w->var("numBackgroundFail")->setConstant(false);
543  } else if (totFailinging == 0) {
544  w->var("efficiency")->setVal(1.0);
545  w->var("efficiency")->setAsymError(-1,0);
546  w->var("efficiency")->setConstant(false);
547  w->var("numBackgroundPass")->setConstant(false);
548  w->var("numBackgroundFail")->setVal(0.0);
549  w->var("numBackgroundFail")->setConstant(true);
550  } else {
551  w->var("efficiency")->setConstant(false);
552  w->var("numBackgroundPass")->setConstant(false);
553  w->var("numBackgroundFail")->setConstant(false);
554  }
555 
556  // if signal fraction is 1 then set the number of background events to 0.
557  RooRealVar* fBkgPass = w->var("numBackgroundPass");
558  if(signalFractionInPassing==1.0) { fBkgPass->setVal(0.0); fBkgPass->setConstant(true); }
559 
560  // save initial state for reference
561  w->saveSnapshot("initialState",w->components());
562 }
T w() const
void TagProbeFitter::setQuiet ( bool  quiet_ = true)

suppress most of the output from RooFit and Minuit

Definition at line 77 of file TagProbeFitter.cc.

References dqm::qstatus::ERROR, instance, and dqm::qstatus::WARNING.

Referenced by TagProbeFitTreeAnalyzer::TagProbeFitTreeAnalyzer().

77  {
78  quiet = quiet_;
79  if (quiet) {
80  RooMsgService::instance().setGlobalKillBelow(RooFit::ERROR);
81  } else {
82  RooMsgService::instance().setGlobalKillBelow(RooFit::WARNING);
83  }
84 }
static PFTauRenderPlugin instance
static const int WARNING
bool quiet
suppress most printout
static const int ERROR
void TagProbeFitter::setWeightVar ( const std::string &  weight)

set a variable to be used as weight for a dataset. empty string means no weights.

Definition at line 121 of file TagProbeFitter.cc.

Referenced by TagProbeFitTreeAnalyzer::TagProbeFitTreeAnalyzer().

121  {
122  weightVar = var;
123 }
std::string weightVar
weight variable (or empy string for no weights)
void TagProbeFitter::varFixer ( RooWorkspace *  w,
bool  fix 
)
protected

fix or release variables selected by user

Definition at line 777 of file TagProbeFitter.cc.

References gather_cfg::cout.

777  {
778  std::vector<std::string>::const_iterator it;
779  for(it=fixVars.begin(); it<fixVars.end(); it++){
780  if(w->var((*it).c_str()))
781  w->var((*it).c_str())->setAttribute("Constant",fix);
782  else{
783  std::cout << "TagProbeFitter: " << "Can't find a variable to fix: " << *it;
784  }
785  }
786 }
std::vector< std::string > fixVars
list of variables fo fix (see below)
tuple cout
Definition: gather_cfg.py:121
T w() const
void TagProbeFitter::varRestorer ( RooWorkspace *  w)
protected

restore variables's values for fit starting point

Definition at line 800 of file TagProbeFitter.cc.

References gather_cfg::cout, and i.

800  {
801  if(fixVarValues.size()==fixVars.size())
802  for(unsigned int i=0; i< fixVars.size(); i++){
803  std::cout << "setting variable " << fixVars[i].c_str() << std::endl;
804  w->var(fixVars[i].c_str())->setVal(fixVarValues[i]);
805  }
806  else{
807  std::cout << "fixVars and fixVarValues are not of the same size!" << std::endl;
808  }
809 }
int i
Definition: DBlmapReader.cc:9
std::vector< std::string > fixVars
list of variables fo fix (see below)
std::vector< double > fixVarValues
tuple cout
Definition: gather_cfg.py:121
T w() const
void TagProbeFitter::varSaver ( RooWorkspace *  w)
protected

store values in the vector

Definition at line 788 of file TagProbeFitter.cc.

References gather_cfg::cout.

788  {
789  if(!fixVarValues.empty()){
790  std::cout << "attempt to save variables more than once!" << std::endl;
791  return;
792  }
793  std::vector<std::string>::const_iterator it;
794  for(it=fixVars.begin(); it<fixVars.end(); it++){
795  fixVarValues.push_back(w->var((*it).c_str())->getVal());
796  }
797 
798 }
std::vector< std::string > fixVars
list of variables fo fix (see below)
std::vector< double > fixVarValues
tuple cout
Definition: gather_cfg.py:121
T w() const

Member Data Documentation

int TagProbeFitter::binnedFit
protected

do binned fit; 0 = automatic, 1 = yes, -1 = no. d

Definition at line 76 of file TagProbeFitter.h.

Referenced by setBinnedFit().

std::vector<std::pair<std::pair<std::string,std::string>, std::pair<std::string, std::vector<std::string> > > > TagProbeFitter::expressionVars
protected

expressions computed almost on the fly

Definition at line 92 of file TagProbeFitter.h.

std::vector<std::string> TagProbeFitter::fixVars
protected

list of variables fo fix (see below)

Definition at line 99 of file TagProbeFitter.h.

std::vector<double> TagProbeFitter::fixVarValues
protected

Definition at line 100 of file TagProbeFitter.h.

bool TagProbeFitter::floatShapeParameters
protected

release some variables before the fit in each bin if set to "false" will fit all dataset to get values of specified variables and then fit all bins having them fixed if set to "true" (default) will not fit all dataset, just each bin with fixed and then released variables

Definition at line 105 of file TagProbeFitter.h.

TChain* TagProbeFitter::inputTree
protected

pointer to the input TTree Chain of data

Definition at line 61 of file TagProbeFitter.h.

int TagProbeFitter::massBins
protected

number of bins to use in mass shape plots; 0 = automatic

Definition at line 79 of file TagProbeFitter.h.

Referenced by setBinnedFit().

int TagProbeFitter::numCPU
protected

number of CPUs to use for the fit

Definition at line 70 of file TagProbeFitter.h.

TDirectory* TagProbeFitter::outputDirectory
protected

pointer to the TDirectory in the output file that is the root directory for this fitter

Definition at line 67 of file TagProbeFitter.h.

TFile* TagProbeFitter::outputFile
protected

pointer to the output file

Definition at line 64 of file TagProbeFitter.h.

RooWorkspace TagProbeFitter::parameterParser
protected

a RooWorkspace object to parse input parameters with ".factory()"

Definition at line 108 of file TagProbeFitter.h.

std::map<std::string, std::vector<std::string> > TagProbeFitter::pdfs
protected

the map of pdf names to the vector of commands to build the pdf

Definition at line 82 of file TagProbeFitter.h.

bool TagProbeFitter::quiet
protected

suppress most printout

Definition at line 111 of file TagProbeFitter.h.

bool TagProbeFitter::saveWorkspace
protected

the default option wether to save the workspace for each bin

Definition at line 73 of file TagProbeFitter.h.

std::vector<std::pair<std::pair<std::string,std::string>, std::pair<std::string, double> > > TagProbeFitter::thresholdCategories
protected

Definition at line 96 of file TagProbeFitter.h.

RooArgSet TagProbeFitter::variables
protected

the set of variables describing the data in the input TTree

Definition at line 85 of file TagProbeFitter.h.

std::string TagProbeFitter::weightVar
protected

weight variable (or empy string for no weights)

Definition at line 88 of file TagProbeFitter.h.