Logo ROOT   6.14/05
Reference Guide
HistoToWorkspaceFactoryFast.cxx
Go to the documentation of this file.
1 // @(#)root/roostats:$Id: cranmer $
2 // Author: Kyle Cranmer, Akira Shibata
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 
22 #ifndef __CINT__
23 #include "RooGlobalFunc.h"
24 #endif
25 
26 #include "RooDataSet.h"
27 #include "RooRealVar.h"
28 #include "RooConstVar.h"
29 #include "RooAddition.h"
30 #include "RooProduct.h"
31 #include "RooProdPdf.h"
32 #include "RooAddPdf.h"
33 #include "RooGaussian.h"
34 #include "RooPoisson.h"
35 #include "RooExponential.h"
36 #include "RooRandom.h"
37 #include "RooCategory.h"
38 #include "RooSimultaneous.h"
39 #include "RooMultiVarGaussian.h"
40 #include "RooNumIntConfig.h"
41 #include "RooMinuit.h"
42 #include "RooNLLVar.h"
43 #include "RooProfileLL.h"
44 #include "RooFitResult.h"
45 #include "RooDataHist.h"
46 #include "RooHistFunc.h"
47 #include "RooHistPdf.h"
48 #include "RooRealSumPdf.h"
49 #include "RooProduct.h"
50 #include "RooWorkspace.h"
51 #include "RooCustomizer.h"
52 #include "RooPlot.h"
53 #include "RooMsgService.h"
54 #include "RooStats/RooStatsUtils.h"
55 #include "RooStats/ModelConfig.h"
59 
60 #include "TH2F.h"
61 #include "TH3F.h"
62 #include "TFile.h"
63 #include "TCanvas.h"
64 #include "TH1.h"
65 #include "TLine.h"
66 #include "TTree.h"
67 #include "TMarker.h"
68 #include "TStopwatch.h"
69 #include "TROOT.h"
70 #include "TStyle.h"
71 #include "TVectorD.h"
72 #include "TMatrixDSym.h"
73 
74 // specific to this package
79 #include "Helper.h"
80 
81 #include <algorithm>
82 
83 #define VERBOSE
84 
85 #define alpha_Low "-5"
86 #define alpha_High "5"
87 #define NoHistConst_Low "0"
88 #define NoHistConst_High "2000"
89 
90 // use this order for safety on library loading
91 using namespace RooFit ;
92 using namespace RooStats ;
93 using namespace std ;
94 
96 
97 namespace RooStats{
98 namespace HistFactory{
99 
100  HistoToWorkspaceFactoryFast::HistoToWorkspaceFactoryFast() :
101  fNomLumi(1.0), fLumiError(0),
102  fLowBin(0), fHighBin(0)
103  {}
104 
106  }
107 
109  fSystToFix( measurement.GetConstantParams() ),
110  fParamValues( measurement.GetParamValues() ),
111  fNomLumi( measurement.GetLumi() ),
112  fLumiError( measurement.GetLumi()*measurement.GetLumiRelErr() ),
113  fLowBin( measurement.GetBinLow() ),
114  fHighBin( measurement.GetBinHigh() ) {
115 
116  // Set Preprocess functions
118 
119  }
120 
121  void HistoToWorkspaceFactoryFast::ConfigureWorkspaceForMeasurement( const std::string& ModelName, RooWorkspace* ws_single, Measurement& measurement ) {
122 
123  // Configure a workspace by doing any
124  // necessary post-processing and by
125  // creating a ModelConfig
126 
127  // Make a ModelConfig and configure it
128  ModelConfig * proto_config = (ModelConfig *) ws_single->obj("ModelConfig");
129  if( proto_config == NULL ) {
130  std::cout << "Error: Did not find 'ModelConfig' object in file: " << ws_single->GetName()
131  << std::endl;
132  throw hf_exc();
133  }
134 
135  std::vector<std::string> poi_list = measurement.GetPOIList();
136  if( poi_list.size()==0 ) {
137  std::cout << "Warining: No Parametetrs of interest are set" << std::endl;
138  }
139 
140  cout << "Setting Parameter(s) of Interest as: ";
141  for(unsigned int i = 0; i < poi_list.size(); ++i) {
142  cout << poi_list.at(i) << " ";
143  }
144  cout << endl;
145 
146  RooArgSet * params= new RooArgSet;
147  for( unsigned int i = 0; i < poi_list.size(); ++i ) {
148  std::string poi_name = poi_list.at(i);
149  RooRealVar* poi = (RooRealVar*) ws_single->var( poi_name.c_str() );
150  if(poi){
151  params->add(*poi);
152  }
153  else {
154  std::cout << "WARNING: Can't find parameter of interest: " << poi_name
155  << " in Workspace. Not setting in ModelConfig." << std::endl;
156  //throw hf_exc();
157  }
158  }
159  proto_config->SetParametersOfInterest(*params);
160 
161  // Name of an 'edited' model, if necessary
162  std::string NewModelName = "newSimPdf"; // <- This name is hard-coded in HistoToWorkspaceFactoryFast::EditSyt. Probably should be changed to : std::string("new") + ModelName;
163 
164 #ifdef DO_EDIT_WS
165  // Activate Additional Constraint Terms
166  if( measurement.GetGammaSyst().size() > 0
167  || measurement.GetUniformSyst().size() > 0
168  || measurement.GetLogNormSyst().size() > 0
169  || measurement.GetNoSyst().size() > 0) {
170  HistoToWorkspaceFactoryFast::EditSyst( ws_single, (ModelName).c_str(),
171  measurement.GetGammaSyst(),
172  measurement.GetUniformSyst(),
173  measurement.GetLogNormSyst(),
174  measurement.GetNoSyst());
175 
176  proto_config->SetPdf( *ws_single->pdf( "newSimPdf" ) );
177  }
178 #endif
179 
180  // Set the ModelConfig's Params of Interest
181  RooAbsData* expData = ws_single->data("asimovData");
182  if( !expData ) {
183  std::cout << "Error: Failed to find dataset: " << expData
184  << " in workspace" << std::endl;
185  throw hf_exc();
186  }
187  if(poi_list.size()!=0){
188  proto_config->GuessObsAndNuisance(*expData);
189  }
190 
191  // Now, let's loop over any additional asimov datasets
192  // that we need to make
193 
194  // Get the pdf
195  // Notice that we get the "new" pdf, this is the one that is
196  // used in the creation of these asimov datasets since they
197  // are fitted (or may be, at least).
198  RooAbsPdf* pdf = ws_single->pdf(NewModelName.c_str());
199  if( !pdf ) pdf = ws_single->pdf( ModelName.c_str() );
200  const RooArgSet* observables = ws_single->set("observables");
201 
202  // Create a SnapShot of the nominal values
203  std::string SnapShotName = "NominalParamValues";
204  ws_single->saveSnapshot(SnapShotName.c_str(), ws_single->allVars());
205 
206  for( unsigned int i=0; i<measurement.GetAsimovDatasets().size(); ++i) {
207 
208  // Set the variable values and "const" ness with the workspace
209  RooStats::HistFactory::Asimov& asimov = measurement.GetAsimovDatasets().at(i);
210  std::string AsimovName = asimov.GetName();
211 
212  std::cout << "Generating additional Asimov Dataset: " << AsimovName << std::endl;
213  asimov.ConfigureWorkspace(ws_single);
214  RooDataSet* asimov_dataset =
216 
217  std::cout << "Importing Asimov dataset" << std::endl;
218  bool failure = ws_single->import(*asimov_dataset, Rename(AsimovName.c_str()));
219  if( failure ) {
220  std::cout << "Error: Failed to import Asimov dataset: " << AsimovName
221  << std::endl;
222  delete asimov_dataset;
223  throw hf_exc();
224  }
225 
226  // Load the snapshot at the end of every loop iteration
227  // so we start each loop with a "clean" snapshot
228  ws_single->loadSnapshot(SnapShotName.c_str());
229 
230  // we can now deleted the data set after having imported it
231  delete asimov_dataset;
232 
233  }
234 
235  // Cool, we're done
236  return; // ws_single;
237  }
238 
239 
240  // We want to eliminate this interface and use the measurment directly
242 
243  // This is a pretty light-weight wrapper function
244  //
245  // Take a fully configured measurement as well as
246  // one of its channels
247  //
248  // Return a workspace representing that channel
249  // Do this by first creating a vector of EstimateSummary's
250  // and this by configuring the workspace with any post-processing
251 
252  // Get the channel's name
253  string ch_name = channel.GetName();
254 
255  // Create a workspace for a SingleChannel from the Measurement Object
256  RooWorkspace* ws_single = this->MakeSingleChannelWorkspace(measurement, channel);
257  if( ws_single == NULL ) {
258  std::cout << "Error: Failed to make Single-Channel workspace for channel: " << ch_name
259  << " and measurement: " << measurement.GetName() << std::endl;
260  throw hf_exc();
261  }
262 
263  // Finally, configure that workspace based on
264  // properties of the measurement
265  HistoToWorkspaceFactoryFast::ConfigureWorkspaceForMeasurement( "model_"+ch_name, ws_single, measurement );
266 
267  return ws_single;
268 
269  }
270 
272 
273  // This function takes a fully configured measurement
274  // which may contain several channels and returns
275  // a workspace holding the combined model
276  //
277  // This can be used, for example, within a script to produce
278  // a combined workspace on-the-fly
279  //
280  // This is a static function (for now) to make
281  // it a one-liner
282 
283  // First, we create an instance of a HistFactory
284  HistoToWorkspaceFactoryFast factory( measurement );
285 
286  // Loop over the channels and create the individual workspaces
287  vector<RooWorkspace*> channel_workspaces;
288  vector<string> channel_names;
289 
290  for( unsigned int chanItr = 0; chanItr < measurement.GetChannels().size(); ++chanItr ) {
291 
292  HistFactory::Channel& channel = measurement.GetChannels().at( chanItr );
293 
294  if( ! channel.CheckHistograms() ) {
295  std::cout << "MakeModelAndMeasurementsFast: Channel: " << channel.GetName()
296  << " has uninitialized histogram pointers" << std::endl;
297  throw hf_exc();
298  }
299 
300  string ch_name = channel.GetName();
301  channel_names.push_back(ch_name);
302 
303  // GHL: Renaming to 'MakeSingleChannelWorkspace'
304  RooWorkspace* ws_single = factory.MakeSingleChannelModel( measurement, channel );
305 
306  channel_workspaces.push_back(ws_single);
307 
308  }
309 
310 
311  // Now, combine the individual channel workspaces to
312  // form the combined workspace
313  RooWorkspace* ws = factory.MakeCombinedModel( channel_names, channel_workspaces );
314 
315 
316  // Configure the workspace
318 
319  // Delete channel workspaces
320  for (vector<RooWorkspace*>::iterator iter = channel_workspaces.begin() ; iter != channel_workspaces.end() ; ++iter) {
321  delete *iter ;
322  }
323 
324  // Done. Return the pointer
325  return ws;
326 
327  }
328 
330  string prefix, string productPrefix,
331  string systTerm ) {
332  if(hist) {
333  cout << "processing hist " << hist->GetName() << endl;
334  } else {
335  cout << "hist is empty" << endl;
336  R__ASSERT(hist != 0);
337  return;
338  }
339 
340  /// require dimension >=1 or <=3
341  if (fObsNameVec.empty() && !fObsName.empty()) { fObsNameVec.push_back(fObsName); }
342  R__ASSERT( fObsNameVec.size()>=1 && fObsNameVec.size()<=3 );
343 
344  /// determine histogram dimensionality
345  unsigned int histndim(1);
346  std::string classname = hist->ClassName();
347  if (classname.find("TH1")==0) { histndim=1; }
348  else if (classname.find("TH2")==0) { histndim=2; }
349  else if (classname.find("TH3")==0) { histndim=3; }
350  R__ASSERT( histndim==fObsNameVec.size() );
351 
352  /// create roorealvar observables
353  RooArgList observables;
354  std::vector<std::string>::iterator itr = fObsNameVec.begin();
355  for (int idx=0; itr!=fObsNameVec.end(); ++itr, ++idx ) {
356  if ( !proto->var(itr->c_str()) ) {
357  TAxis* axis(0);
358  if (idx==0) { axis = hist->GetXaxis(); }
359  if (idx==1) { axis = hist->GetYaxis(); }
360  if (idx==2) { axis = hist->GetZaxis(); }
361  Int_t nbins = axis->GetNbins();
362  Double_t xmin = axis->GetXmin();
363  Double_t xmax = axis->GetXmax();
364  // create observable
365  proto->factory(Form("%s[%f,%f]",itr->c_str(),xmin,xmax));
366  proto->var(itr->c_str())->setBins(nbins);
367  }
368  observables.add( *proto->var(itr->c_str()) );
369  }
370 
371  RooDataHist* histDHist = new RooDataHist((prefix+"nominalDHist").c_str(),"",observables,hist);
372  RooHistFunc* histFunc = new RooHistFunc((prefix+"_nominal").c_str(),"",observables,*histDHist,0) ;
373 
374  proto->import(*histFunc);
375 
376  /// now create the product of the overall efficiency times the sigma(params) for this estimate
377  proto->factory(("prod:"+productPrefix+"("+prefix+"_nominal,"+systTerm+")").c_str() );
378 
379  delete histDHist;
380  delete histFunc;
381 
382  }
383 
384  void HistoToWorkspaceFactoryFast::AddMultiVarGaussConstraint(RooWorkspace* proto, string prefix,int lowBin, int highBin, vector<string>& constraintTermNames){
385  // these are the nominal predictions: eg. the mean of some space of variations
386  // later fill these in a loop over histogram bins
387 
388  TVectorD mean(highBin); //-lowBin); // MB: fix range
389  cout << "a" << endl;
390  for(Int_t i=lowBin; i<highBin; ++i){
391  std::stringstream str;
392  str<<"_"<<i;
393  RooRealVar* temp = proto->var((prefix+str.str()).c_str());
394  mean(i) = temp->getVal();
395  }
396 
397  TMatrixDSym Cov(highBin-lowBin);
398  for(int i=lowBin; i<highBin; ++i){
399  for(int j=0; j<highBin-lowBin; ++j){
400  if(i==j) { Cov(i,j) = sqrt(mean(i)); } // MB : this doesn't make sense to me if lowBin!=0 (?)
401  else { Cov(i,j) = 0; }
402  }
403  }
404 
405  // can't make MultiVarGaussian with factory yet, do it by hand
406  RooArgList floating( *(proto->set(prefix.c_str() ) ) );
407  RooMultiVarGaussian constraint((prefix+"Constraint").c_str(),"",
408  floating, mean, Cov);
409 
410  proto->import(constraint);
411 
412  constraintTermNames.push_back(constraint.GetName());
413  }
414 
416  std::vector<HistoSys> histoSysList,
417  string prefix, string productPrefix,
418  string systTerm,
419  vector<string>& constraintTermNames){
420 
421  // these are the nominal predictions: eg. the mean of some space of variations
422  // later fill these in a loop over histogram bins
423 
424  // require dimension >=1 or <=3
425  if (fObsNameVec.empty() && !fObsName.empty()) { fObsNameVec.push_back(fObsName); }
426  R__ASSERT( fObsNameVec.size()>=1 && fObsNameVec.size()<=3 );
427 
428  // determine histogram dimensionality
429  unsigned int histndim(1);
430  std::string classname = nominal->ClassName();
431  if (classname.find("TH1")==0) { histndim=1; }
432  else if (classname.find("TH2")==0) { histndim=2; }
433  else if (classname.find("TH3")==0) { histndim=3; }
434  R__ASSERT( histndim==fObsNameVec.size() );
435  // cout <<"In LinInterpWithConstriants and histndim = " << histndim <<endl;
436 
437  // create roorealvar observables
438  RooArgList observables;
439  std::vector<std::string>::iterator itr = fObsNameVec.begin();
440  for (int idx=0; itr!=fObsNameVec.end(); ++itr, ++idx ) {
441  if ( !proto->var(itr->c_str()) ) {
442  TAxis* axis(NULL);
443  if (idx==0) { axis = nominal->GetXaxis(); }
444  else if (idx==1) { axis = nominal->GetYaxis(); }
445  else if (idx==2) { axis = nominal->GetZaxis(); }
446  else {
447  std::cout << "Error: Too many observables. "
448  << "HistFactory only accepts up to 3 observables (3d) "
449  << std::endl;
450  throw hf_exc();
451  }
452  Int_t nbins = axis->GetNbins();
453  Double_t xmin = axis->GetXmin();
454  Double_t xmax = axis->GetXmax();
455  // create observable
456  proto->factory(Form("%s[%f,%f]",itr->c_str(),xmin,xmax));
457  proto->var(itr->c_str())->setBins(nbins);
458  }
459  observables.add( *proto->var(itr->c_str()) );
460  }
461 
462  RooDataHist* nominalDHist = new RooDataHist((prefix+"nominalDHist").c_str(),"",observables,nominal);
463  RooHistFunc* nominalFunc = new RooHistFunc((prefix+"nominal").c_str(),"",observables,*nominalDHist,0) ;
464 
465  // make list of abstract parameters that interpolate in space of variations
466  RooArgList params( ("alpha_Hist") );
467  // range is set using defined macro (see top of the page)
468  string range=string("[")+alpha_Low+","+alpha_High+"]";
469 
470  // Loop over the HistoSys list
471  for(unsigned int j=0; j<histoSysList.size(); ++j){
472  std::stringstream str;
473  str<<"_"<<j;
474 
475  HistoSys& histoSys = histoSysList.at(j);
476  string histoSysName = histoSys.GetName();
477 
478  RooRealVar* temp = (RooRealVar*) proto->var(("alpha_" + histoSysName).c_str());
479  if(!temp){
480 
481  temp = (RooRealVar*) proto->factory(("alpha_" + histoSysName + range).c_str());
482 
483  // now add a constraint term for these parameters
484  string command=("Gaussian::alpha_"+histoSysName+"Constraint(alpha_"+histoSysName+",nom_alpha_"+histoSysName+"[0.,-10,10],1.)");
485  cout << command << endl;
486  constraintTermNames.push_back( proto->factory( command.c_str() )->GetName() );
487  proto->var(("nom_alpha_"+histoSysName).c_str())->setConstant();
488  const_cast<RooArgSet*>(proto->set("globalObservables"))->add(*proto->var(("nom_alpha_"+histoSysName).c_str()));
489  }
490  params.add(* temp );
491  }
492 
493  // now make function that linearly interpolates expectation between variations
494  // get low/high variations to interpolate between
495  vector<double> low, high;
496  RooArgSet lowSet, highSet;
497  //ES// for(unsigned int j=0; j<lowHist.size(); ++j){
498  for(unsigned int j=0; j<histoSysList.size(); ++j){
499  std::stringstream str;
500  str<<"_"<<j;
501 
502  HistoSys& histoSys = histoSysList.at(j);
503  RooDataHist* lowDHist = new RooDataHist((prefix+str.str()+"lowDHist").c_str(),"",observables, histoSys.GetHistoLow());
504  RooDataHist* highDHist = new RooDataHist((prefix+str.str()+"highDHist").c_str(),"",observables, histoSys.GetHistoHigh());
505  RooHistFunc* lowFunc = new RooHistFunc((prefix+str.str()+"low").c_str(),"",observables,*lowDHist,0) ;
506  RooHistFunc* highFunc = new RooHistFunc((prefix+str.str()+"high").c_str(),"",observables,*highDHist,0) ;
507  lowSet.add(*lowFunc);
508  highSet.add(*highFunc);
509  }
510 
511  // this is sigma(params), a piece-wise linear interpolation
512  PiecewiseInterpolation interp(prefix.c_str(),"",*nominalFunc,lowSet,highSet,params);
513  interp.setPositiveDefinite();
514  interp.setAllInterpCodes(4); // LM: change to 4 (piece-wise linear to 6th order polynomial interpolation + linear extrapolation )
515  // KC: interpo codes 1 etc. don't have proper analytic integral.
516  RooArgSet observableSet(observables);
517  interp.setBinIntegrator(observableSet);
518  interp.forceNumInt();
519 
520  proto->import(interp); // individual params have already been imported in first loop of this function
521 
522  // now create the product of the overall efficiency times the sigma(params) for this estimate
523  proto->factory(("prod:"+productPrefix+"("+prefix+","+systTerm+")").c_str() );
524 
525  }
526 
527  // GHL: Consider passing the NormFactor list instead of the entire sample
528  string HistoToWorkspaceFactoryFast::AddNormFactor(RooWorkspace* proto, string& channel, string& sigmaEpsilon, Sample& sample, bool doRatio){
529  string overallNorm_times_sigmaEpsilon ;
530  string prodNames;
531 
532  vector<NormFactor> normList = sample.GetNormFactorList();
533  vector<string> normFactorNames, rangeNames;
534 
535  if(normList.size() > 0){
536 
537  for(vector<NormFactor>::iterator itr = normList.begin(); itr != normList.end(); ++itr){
538 
539  NormFactor& norm = *itr;
540 
541  string varname;
542  if(!prodNames.empty()) prodNames += ",";
543  if(doRatio) {
544  varname = norm.GetName() + "_" + channel;
545  }
546  else {
547  varname=norm.GetName();
548  }
549 
550  // GHL: Check that the NormFactor doesn't already exist
551  // (it may have been created as a function expression
552  // during preprocessing)
553  std::stringstream range;
554  range << "[" << norm.GetVal() << "," << norm.GetLow() << "," << norm.GetHigh() << "]";
555 
556  if( proto->obj(varname.c_str()) == NULL) {
557  cout << "making normFactor: " << norm.GetName() << endl;
558  // remove "doRatio" and name can be changed when ws gets imported to the combined model.
559  proto->factory((varname + range.str()).c_str());
560  }
561 
562  if(norm.GetConst()) {
563  // proto->var(varname.c_str())->setConstant();
564  // cout <<"setting " << varname << " constant"<<endl;
565  cout << "WARNING: Const attribute to <NormFactor> tag is deprecated, will ignore." <<
566  " Instead, add \n\t<ParamSetting Const=\"True\">" << varname << "</ParamSetting>\n" <<
567  " to your top-level XML's <Measurment> entry" << endl;
568  }
569  prodNames+=varname;
570  rangeNames.push_back(range.str());
571  normFactorNames.push_back(varname);
572  }
573 
574  overallNorm_times_sigmaEpsilon = sample.GetName() + "_" + channel + "_overallNorm_x_sigma_epsilon";
575  proto->factory(("prod::" + overallNorm_times_sigmaEpsilon + "(" + prodNames + "," + sigmaEpsilon + ")").c_str());
576  }
577 
578  unsigned int rangeIndex=0;
579  for( vector<string>::iterator nit = normFactorNames.begin(); nit!=normFactorNames.end(); ++nit){
580  if( count (normFactorNames.begin(), normFactorNames.end(), *nit) > 1 ){
581  cout <<"WARNING: <NormFactor Name =\""<<*nit<<"\"> is duplicated for <Sample Name=\""
582  << sample.GetName() << "\">, but only one factor will be included. \n Instead, define something like"
583  << "\n\t<Function Name=\""<<*nit<<"Squared\" Expresion=\""<<*nit<<"*"<<*nit<<"\" Var=\""<<*nit<<rangeNames.at(rangeIndex)
584  << "\"> \nin your top-level XML's <Measurment> entry and use <NormFactor Name=\""<<*nit<<"Squared\" in your channel XML file."<< endl;
585  }
586  ++rangeIndex;
587  }
588 
589  if(!overallNorm_times_sigmaEpsilon.empty())
590  return overallNorm_times_sigmaEpsilon;
591  else
592  return sigmaEpsilon;
593  }
594 
596  string interpName,
597  std::vector<OverallSys>& systList,
598  vector<string>& constraintTermNames,
599  vector<string>& totSystTermNames) {
600 
601  // add variables for all the relative overall uncertainties we expect
602  // range is set using defined macro (see top of the page)
603 
604  string range=string("[0,")+alpha_Low+","+alpha_High+"]";
605  totSystTermNames.push_back(prefix);
606 
607  RooArgSet params(prefix.c_str());
608  vector<double> lowVec, highVec;
609 
610  std::map<std::string, double>::iterator itconstr;
611  for(unsigned int i = 0; i < systList.size(); ++i) {
612 
613  OverallSys& sys = systList.at(i);
614  std::string strname = sys.GetName();
615  const char * name = strname.c_str();
616 
617  // case of no systematic (is it possible)
618  if (meas.GetNoSyst().count(sys.GetName()) > 0 ) {
619  std::cout << "HistoToWorkspaceFast::AddConstraintTerm - skip systematic " << sys.GetName() << std::endl;
620  continue;
621  }
622  // case systematic is a gamma constraint
623  if (meas.GetGammaSyst().count(sys.GetName()) > 0 ) {
624  double relerr = meas.GetGammaSyst().find(sys.GetName() )->second;
625  if (relerr <= 0) {
626  std::cout << "HistoToWorkspaceFast::AddConstraintTerm - zero uncertainty assigned - skip systematic " << sys.GetName() << std::endl;
627  continue;
628  }
629  double tauVal = 1./(relerr*relerr);
630  double sqtau = 1./relerr;
631  RooAbsArg * beta = proto->factory(TString::Format("beta_%s[1,0,10]",name) );
632  // the global observable (y_s)
633  RooAbsArg * yvar = proto->factory(TString::Format("nom_%s[%f,0,10]",beta->GetName(),tauVal)) ;
634  // the rate of the gamma distribution (theta)
635  RooAbsArg * theta = proto->factory(TString::Format("theta_%s[%f]",name,1./tauVal));
636  // find alpha as function of beta
637  RooAbsArg* alphaOfBeta = proto->factory(TString::Format("PolyVar::alphaOfBeta_%s(beta_%s,{%f,%f})",name,name,-sqtau,sqtau));
638 
639  // add now the constraint itself Gamma_beta_constraint(beta, y+1, tau, 0 )
640  // build the gamma parameter k = as y_s + 1
641  RooAbsArg * kappa = proto->factory(TString::Format("sum::k_%s(%s,1.)",name,yvar->GetName()) );
642  RooAbsArg * gamma = proto->factory(TString::Format("Gamma::%sConstraint(%s, %s, %s, 0.0)",beta->GetName(),beta->GetName(), kappa->GetName(), theta->GetName() ) );
643  alphaOfBeta->Print("t");
644  gamma->Print("t");
645  constraintTermNames.push_back(gamma->GetName());
646  // set global observables
647  RooRealVar * gobs = dynamic_cast<RooRealVar*>(yvar); assert(gobs);
648  gobs->setConstant(true);
649  const_cast<RooArgSet*>(proto->set("globalObservables"))->add(*yvar);
650 
651  // add alphaOfBeta in the list of params to interpolate
652  params.add(*alphaOfBeta);
653  std::cout << "Added a gamma constraint for " << name << std::endl;
654 
655  }
656  else {
657 
658  // add the Gaussian constraint part
659 
660  // case systematic is uniform (asssume they are like a gauaaian bbut with a large width
661  // (100 instead of 1)
662  double gaussSigma = 1;
663  if (meas.GetUniformSyst().count(sys.GetName()) > 0 ) {
664  gaussSigma = 100;
665  std::cout << "Added a uniform constraint for " << name << " as a gaussian constraint with a very large sigma " << std::endl;
666  }
667 
668  // add Gaussian constraint terms (normal + log-normal case)
669  RooRealVar* alpha = (RooRealVar*) proto->var((prefix + sys.GetName()).c_str());
670  if(!alpha) {
671 
672  alpha = (RooRealVar*) proto->factory((prefix + sys.GetName() + range).c_str());
673  RooAbsArg * nomAlpha = proto->factory(TString::Format("nom_%s[0.,-10,10]",alpha->GetName() ) );
674  RooAbsArg * gausConstraint = proto->factory(TString::Format("Gaussian::%sConstraint(%s,%s,%f)",alpha->GetName(),alpha->GetName(), nomAlpha->GetName(), gaussSigma) );
675  //cout << command << endl;
676  constraintTermNames.push_back( gausConstraint->GetName() );
677  proto->var(("nom_" + prefix + sys.GetName()).c_str())->setConstant();
678  const_cast<RooArgSet*>(proto->set("globalObservables"))->add(*nomAlpha);
679  }
680 
681 
682  // add constraint in terms of bifrucated gauss with low/high as sigmas
683  //std::stringstream lowhigh;
684 
685  // check if exists a log-normal constraint
686  if (meas.GetLogNormSyst().count(sys.GetName()) == 0 && meas.GetGammaSyst().count(sys.GetName()) == 0 ) {
687  // just add the alpha for the parameters of the FlexibleInterpVar function
688  params.add(*alpha);
689  }
690  // case systematic is a log-normal constraint
691  if (meas.GetLogNormSyst().count(sys.GetName()) > 0 ) {
692  // log normal constraint for parameter
693  double relerr = meas.GetLogNormSyst().find(sys.GetName() )->second;
694  double tauVal = 1./relerr;
695  std::string tauName = "tau_" + sys.GetName();
696  proto->factory(TString::Format("%s[%f]",tauName.c_str(),tauVal ) );
697  double kappaVal = 1. + relerr;
698  std::string kappaName = "kappa_" + sys.GetName();
699  proto->factory(TString::Format("%s[%f]",kappaName.c_str(),kappaVal ) );
700  const char * alphaName = alpha->GetName();
701 
702  std::string alphaOfBetaName = "alphaOfBeta_" + sys.GetName();
703  RooAbsArg * alphaOfBeta = proto->factory(TString::Format("expr::%s('%s*(pow(%s,%s)-1.)',%s,%s,%s)",alphaOfBetaName.c_str(),
704  tauName.c_str(),kappaName.c_str(),alphaName,
705  tauName.c_str(),kappaName.c_str(),alphaName ) );
706 
707  std::cout << "Added a log-normal constraint for " << name << std::endl;
708  alphaOfBeta->Print("t");
709  params.add(*alphaOfBeta);
710  }
711 
712  }
713  // add low/high vectors
714  double low = sys.GetLow();
715  double high = sys.GetHigh();
716  lowVec.push_back(low);
717  highVec.push_back(high);
718 
719  } // end sys loop
720 
721  if(systList.size() > 0){
722  // this is epsilon(alpha_j), a piece-wise linear interpolation
723  // LinInterpVar interp( (interpName).c_str(), "", params, 1., lowVec, highVec);
724 
725  assert( params.getSize() > 0);
726  assert(int(lowVec.size()) == params.getSize() );
727 
728  FlexibleInterpVar interp( (interpName).c_str(), "", params, 1., lowVec, highVec);
729  interp.setAllInterpCodes(4); // LM: change to 4 (piece-wise linear to 6th order polynomial interpolation + linear extrapolation )
730  //interp.setAllInterpCodes(0); // simple linear interpolation
731  proto->import(interp); // params have already been imported in first loop of this function
732  } else{
733  // some strange behavior if params,lowVec,highVec are empty.
734  //cout << "WARNING: No OverallSyst terms" << endl;
735  RooConstVar interp( (interpName).c_str(), "", 1.);
736  proto->import(interp); // params have already been imported in first loop of this function
737  }
738 
739  // std::cout << "after creating FlexibleInterpVar " << std::endl;
740  // proto->Print();
741 
742  }
743 
744 
746  vector<string>& syst_x_expectedPrefixNames,
747  vector<string>& normByNames){
748 
749  // for ith bin calculate totN_i = lumi * sum_j expected_j * syst_j
750  string command;
751  string coeffList="";
752  string shapeList="";
753  string prepend="";
754 
755  if (fObsNameVec.empty() && !fObsName.empty()) { fObsNameVec.push_back(fObsName); }
756 
757  double binWidth(1.0);
758  std::string obsNameVecStr;
759  std::vector<std::string>::iterator itr = fObsNameVec.begin();
760  for (; itr!=fObsNameVec.end(); ++itr) {
761  std::string obsName = *itr;
762  binWidth *= proto->var(obsName.c_str())->numBins()/(proto->var(obsName.c_str())->getMax() - proto->var(obsName.c_str())->getMin()) ; // MB: Note: requires fixed bin sizes
763  if (obsNameVecStr.size()>0) { obsNameVecStr += "_"; }
764  obsNameVecStr += obsName;
765  }
766 
767  //vector<string>::iterator it=syst_x_expectedPrefixNames.begin();
768  for(unsigned int j=0; j<syst_x_expectedPrefixNames.size();++j){
769  std::stringstream str;
770  str<<"_"<<j;
771  // repatative, but we need one coeff for each term in the sum
772  // maybe can be avoided if we don't use bin width as coefficient
773  command=string(Form("binWidth_%s_%d[%e]",obsNameVecStr.c_str(),j,binWidth));
774  proto->factory(command.c_str());
775  proto->var(Form("binWidth_%s_%d",obsNameVecStr.c_str(),j))->setConstant();
776  coeffList+=prepend+"binWidth_"+obsNameVecStr+str.str();
777 
778  command="prod::L_x_"+syst_x_expectedPrefixNames.at(j)+"("+normByNames.at(j)+","+syst_x_expectedPrefixNames.at(j)+")";
779  /*RooAbsReal* tempFunc =(RooAbsReal*) */
780  proto->factory(command.c_str());
781  shapeList+=prepend+"L_x_"+syst_x_expectedPrefixNames.at(j);
782  prepend=",";
783 
784  // add to num int to product
785  // tempFunc->specialIntegratorConfig(kTRUE)->method1D().setLabel("RooBinIntegrator") ;
786  // tempFunc->forceNumInt();
787 
788  }
789 
790  proto->defineSet("coefList",coeffList.c_str());
791  proto->defineSet("shapeList",shapeList.c_str());
792  // proto->factory(command.c_str());
793  RooRealSumPdf tot(totName.c_str(),totName.c_str(),*proto->set("shapeList"),*proto->set("coefList"),kTRUE);
794  tot.specialIntegratorConfig(kTRUE)->method1D().setLabel("RooBinIntegrator") ;
795  tot.specialIntegratorConfig(kTRUE)->method2D().setLabel("RooBinIntegrator") ;
796  tot.specialIntegratorConfig(kTRUE)->methodND().setLabel("RooBinIntegrator") ;
797  tot.forceNumInt();
798 
799  // for mixed generation in RooSimultaneous
800  tot.setAttribute("GenerateBinned"); // for use with RooSimultaneous::generate in mixed mode
801  // tot.setAttribute("GenerateUnbinned"); // we don't want that
802 
803  /*
804  // Use binned numeric integration
805  int nbins = 0;
806  if( fObsNameVec.size() == 1 ) {
807  nbins = proto->var(fObsNameVec.at(0).c_str())->numBins();
808 
809  cout <<"num bis for RooRealSumPdf = "<<nbins <<endl;
810  //int nbins = ((RooRealVar*) allVars.first())->numBins();
811  tot.specialIntegratorConfig(kTRUE)->getConfigSection("RooBinIntegrator").setRealValue("numBins",nbins);
812  tot.forceNumInt();
813 
814  } else {
815  cout << "Bin Integrator only supports 1-d. Will be slow." << std::endl;
816  }
817  */
818 
819 
820  proto->import(tot);
821 
822  }
823 
824  void HistoToWorkspaceFactoryFast::AddPoissonTerms(RooWorkspace* proto, string prefix, string obsPrefix, string expPrefix, int lowBin, int highBin,
825  vector<string>& likelihoodTermNames){
826  /////////////////////////////////
827  // Relate observables to expected for each bin
828  // later modify variable named expPrefix_i to be product of terms
829  RooArgSet Pois(prefix.c_str());
830  for(Int_t i=lowBin; i<highBin; ++i){
831  std::stringstream str;
832  str<<"_"<<i;
833  //string command("Poisson::"+prefix+str.str()+"("+obsPrefix+str.str()+","+expPrefix+str.str()+")");
834  string command("Poisson::"+prefix+str.str()+"("+obsPrefix+str.str()+","+expPrefix+str.str()+",1)");//for no rounding
835  RooAbsArg* temp = (proto->factory( command.c_str() ) );
836 
837  // output
838  cout << "Poisson Term " << command << endl;
839  ((RooAbsPdf*) temp)->setEvalErrorLoggingMode(RooAbsReal::PrintErrors);
840  //cout << temp << endl;
841 
842  likelihoodTermNames.push_back( temp->GetName() );
843  Pois.add(* temp );
844  }
845  proto->defineSet(prefix.c_str(),Pois); // add argset to workspace
846  }
847 
848  void HistoToWorkspaceFactoryFast::SetObsToExpected(RooWorkspace* proto, string obsPrefix, string expPrefix, int lowBin, int highBin){
849  /////////////////////////////////
850  // set observed to expected
851  TTree* tree = new TTree();
852  Double_t* obsForTree = new Double_t[highBin-lowBin];
853  RooArgList obsList("obsList");
854 
855  for(Int_t i=lowBin; i<highBin; ++i){
856  std::stringstream str;
857  str<<"_"<<i;
858  RooRealVar* obs = (RooRealVar*) proto->var((obsPrefix+str.str()).c_str());
859  cout << "expected number of events called: " << expPrefix << endl;
860  RooAbsReal* exp = proto->function((expPrefix+str.str()).c_str());
861  if(obs && exp){
862 
863  //proto->Print();
864  obs->setVal( exp->getVal() );
865  cout << "setting obs"+str.str()+" to expected = " << exp->getVal() << " check: " << obs->getVal() << endl;
866 
867  // add entry to array and attach to tree
868  obsForTree[i] = exp->getVal();
869  tree->Branch((obsPrefix+str.str()).c_str(), obsForTree+i ,(obsPrefix+str.str()+"/D").c_str());
870  obsList.add(*obs);
871  }else{
872  cout << "problem retrieving obs or exp " << obsPrefix+str.str() << obs << " " << expPrefix+str.str() << exp << endl;
873  }
874  }
875  tree->Fill();
876  RooDataSet* data = new RooDataSet("expData","", tree, obsList); // one experiment
877 
878  delete tree;
879  delete [] obsForTree;
880 
881  proto->import(*data);
882 
883  delete data;
884 
885  }
886 
887  //////////////////////////////////////////////////////////////////////////////
888 
890  map<string,double> gammaSyst,
891  map<string,double> uniformSyst,
892  map<string,double> logNormSyst,
893  map<string,double> noSyst) {
894  string pdfName(pdfNameChar);
895 
896  ModelConfig * combined_config = (ModelConfig *) proto->obj("ModelConfig");
897  if( combined_config==NULL ) {
898  std::cout << "Error: Failed to find object 'ModelConfig' in workspace: "
899  << proto->GetName() << std::endl;
900  throw hf_exc();
901  }
902  // const RooArgSet * constrainedParams=combined_config->GetNuisanceParameters();
903  // RooArgSet temp(*constrainedParams);
904  string edit="EDIT::newSimPdf("+pdfName+",";
905  string editList;
906  string lastPdf=pdfName;
907  string precede="";
908  unsigned int numReplacements = 0;
909  unsigned int nskipped = 0;
910  map<string,double>::iterator it;
911 
912 
913  // add gamma terms and their constraints
914  for(it=gammaSyst.begin(); it!=gammaSyst.end(); ++it) {
915  //cout << "edit for " << it->first << "with rel uncert = " << it->second << endl;
916  if(! proto->var(("alpha_"+it->first).c_str())){
917  //cout << "systematic not there" << endl;
918  nskipped++;
919  continue;
920  }
921  numReplacements++;
922 
923  double relativeUncertainty = it->second;
924  double scale = 1/sqrt((1+1/pow(relativeUncertainty,2)));
925 
926  // this is the Gamma PDF and in a form that doesn't have roundoff problems like the Poisson does
927  proto->factory(Form("beta_%s[1,0,10]",it->first.c_str()));
928  proto->factory(Form("y_%s[%f]",it->first.c_str(),1./pow(relativeUncertainty,2))) ;
929  proto->factory(Form("theta_%s[%f]",it->first.c_str(),pow(relativeUncertainty,2))) ;
930  proto->factory(Form("Gamma::beta_%sConstraint(beta_%s,sum::k_%s(y_%s,one[1]),theta_%s,zero[0])",
931  it->first.c_str(),
932  it->first.c_str(),
933  it->first.c_str(),
934  it->first.c_str(),
935  it->first.c_str())) ;
936 
937  /*
938  // this has some problems because N in poisson is rounded to nearest integer
939  proto->factory(Form("Poisson::beta_%sConstraint(y_%s[%f],prod::taub_%s(taus_%s[%f],beta_%s[1,0,5]))",
940  it->first.c_str(),
941  it->first.c_str(),
942  1./pow(relativeUncertainty,2),
943  it->first.c_str(),
944  it->first.c_str(),
945  1./pow(relativeUncertainty,2),
946  it->first.c_str()
947  ) ) ;
948  */
949  // combined->factory(Form("expr::alphaOfBeta('(beta-1)/%f',beta)",scale));
950  // combined->factory(Form("expr::alphaOfBeta_%s('(beta_%s-1)/%f',beta_%s)",it->first.c_str(),it->first.c_str(),scale,it->first.c_str()));
951  proto->factory(Form("PolyVar::alphaOfBeta_%s(beta_%s,{%f,%f})",it->first.c_str(),it->first.c_str(),-1./scale,1./scale));
952 
953  // set beta const status to be same as alpha
954  if(proto->var(Form("alpha_%s",it->first.c_str()))->isConstant()) {
955  proto->var(Form("beta_%s",it->first.c_str()))->setConstant(true);
956  }
957  else {
958  proto->var(Form("beta_%s",it->first.c_str()))->setConstant(false);
959  }
960  // set alpha const status to true
961  // proto->var(Form("alpha_%s",it->first.c_str()))->setConstant(true);
962 
963  // replace alphas with alphaOfBeta and replace constraints
964  editList+=precede + "alpha_"+it->first+"Constraint=beta_" + it->first+ "Constraint";
965  precede=",";
966  editList+=precede + "alpha_"+it->first+"=alphaOfBeta_"+ it->first;
967 
968  /*
969  if( proto->pdf(("alpha_"+it->first+"Constraint").c_str()) && proto->var(("alpha_"+it->first).c_str()) )
970  cout << " checked they are there" << proto->pdf(("alpha_"+it->first+"Constraint").c_str()) << " " << proto->var(("alpha_"+it->first).c_str()) << endl;
971  else
972  cout << "NOT THERE" << endl;
973  */
974 
975  // EDIT seems to die if the list of edits is too long. So chunck them up.
976  if(numReplacements%10 == 0 && numReplacements+nskipped!=gammaSyst.size()){
977  edit="EDIT::"+lastPdf+"_("+lastPdf+","+editList+")";
978  lastPdf+="_"; // append an underscore for the edit
979  editList=""; // reset edit list
980  precede="";
981  cout << "Going to issue this edit command\n" << edit<< endl;
982  proto->factory( edit.c_str() );
983  RooAbsPdf* newOne = proto->pdf(lastPdf.c_str());
984  if(!newOne)
985  cout << "\n\n ---------------------\n WARNING: failed to make EDIT\n\n" << endl;
986 
987  }
988  }
989 
990  // add uniform terms and their constraints
991  for(it=uniformSyst.begin(); it!=uniformSyst.end(); ++it) {
992  cout << "edit for " << it->first << "with rel uncert = " << it->second << endl;
993  if(! proto->var(("alpha_"+it->first).c_str())){
994  cout << "systematic not there" << endl;
995  nskipped++;
996  continue;
997  }
998  numReplacements++;
999 
1000  // this is the Uniform PDF
1001  proto->factory(Form("beta_%s[1,0,10]",it->first.c_str()));
1002  proto->factory(Form("Uniform::beta_%sConstraint(beta_%s)",it->first.c_str(),it->first.c_str()));
1003  proto->factory(Form("PolyVar::alphaOfBeta_%s(beta_%s,{-1,1})",it->first.c_str(),it->first.c_str()));
1004 
1005  // set beta const status to be same as alpha
1006  if(proto->var(Form("alpha_%s",it->first.c_str()))->isConstant())
1007  proto->var(Form("beta_%s",it->first.c_str()))->setConstant(true);
1008  else
1009  proto->var(Form("beta_%s",it->first.c_str()))->setConstant(false);
1010  // set alpha const status to true
1011  // proto->var(Form("alpha_%s",it->first.c_str()))->setConstant(true);
1012 
1013  // replace alphas with alphaOfBeta and replace constraints
1014  cout << "alpha_"+it->first+"Constraint=beta_" + it->first+ "Constraint" << endl;
1015  editList+=precede + "alpha_"+it->first+"Constraint=beta_" + it->first+ "Constraint";
1016  precede=",";
1017  cout << "alpha_"+it->first+"=alphaOfBeta_"+ it->first << endl;
1018  editList+=precede + "alpha_"+it->first+"=alphaOfBeta_"+ it->first;
1019 
1020  if( proto->pdf(("alpha_"+it->first+"Constraint").c_str()) && proto->var(("alpha_"+it->first).c_str()) )
1021  cout << " checked they are there" << proto->pdf(("alpha_"+it->first+"Constraint").c_str()) << " " << proto->var(("alpha_"+it->first).c_str()) << endl;
1022  else
1023  cout << "NOT THERE" << endl;
1024 
1025  // EDIT seems to die if the list of edits is too long. So chunck them up.
1026  if(numReplacements%10 == 0 && numReplacements+nskipped!=gammaSyst.size()){
1027  edit="EDIT::"+lastPdf+"_("+lastPdf+","+editList+")";
1028  lastPdf+="_"; // append an underscore for the edit
1029  editList=""; // reset edit list
1030  precede="";
1031  cout << edit<< endl;
1032  proto->factory( edit.c_str() );
1033  RooAbsPdf* newOne = proto->pdf(lastPdf.c_str());
1034  if(!newOne)
1035  cout << "\n\n ---------------------\n WARNING: failed to make EDIT\n\n" << endl;
1036 
1037  }
1038  }
1039 
1040  /////////////////////////////////////////
1041  ////////////////////////////////////
1042 
1043 
1044  // add lognormal terms and their constraints
1045  for(it=logNormSyst.begin(); it!=logNormSyst.end(); ++it) {
1046  cout << "edit for " << it->first << "with rel uncert = " << it->second << endl;
1047  if(! proto->var(("alpha_"+it->first).c_str())){
1048  cout << "systematic not there" << endl;
1049  nskipped++;
1050  continue;
1051  }
1052  numReplacements++;
1053 
1054  double relativeUncertainty = it->second;
1055  double kappa = 1+relativeUncertainty;
1056  // when transforming beta -> alpha, need alpha=1 to be +1sigma value.
1057  // the P(beta>kappa*\hat(beta)) = 16%
1058  // and \hat(beta) is 1, thus
1059  double scale = relativeUncertainty;
1060  //double scale = kappa;
1061 
1062  const char * cname = it->first.c_str();
1063 
1064  // this is the LogNormal
1065  proto->factory(TString::Format("beta_%s[1,0,10]",cname));
1066  proto->factory(TString::Format("nom_beta_%s[1]",cname));
1067  proto->factory(TString::Format("kappa_%s[%f]",cname,kappa));
1068  proto->factory(TString::Format("Lognormal::beta_%sConstraint(beta_%s,nom_beta_%s,kappa_%s)",
1069  cname, cname, cname, cname)) ;
1070  proto->factory(TString::Format("PolyVar::alphaOfBeta_%s(beta_%s,{%f,%f})",cname,cname,-1./scale,1./scale));
1071 
1072 
1073  // set beta const status to be same as alpha
1074  if(proto->var(TString::Format("alpha_%s",cname))->isConstant())
1075  proto->var(TString::Format("beta_%s",cname))->setConstant(true);
1076  else
1077  proto->var(TString::Format("beta_%s",cname))->setConstant(false);
1078  // set alpha const status to true
1079  // proto->var(TString::Format("alpha_%s",cname))->setConstant(true);
1080 
1081  // replace alphas with alphaOfBeta and replace constraints
1082  cout << "alpha_"+it->first+"Constraint=beta_" + it->first+ "Constraint" << endl;
1083  editList+=precede + "alpha_"+it->first+"Constraint=beta_" + it->first+ "Constraint";
1084  precede=",";
1085  cout << "alpha_"+it->first+"=alphaOfBeta_"+ it->first << endl;
1086  editList+=precede + "alpha_"+it->first+"=alphaOfBeta_"+ it->first;
1087 
1088  if( proto->pdf(("alpha_"+it->first+"Constraint").c_str()) && proto->var(("alpha_"+it->first).c_str()) )
1089  cout << " checked they are there" << proto->pdf(("alpha_"+it->first+"Constraint").c_str()) << " " << proto->var(("alpha_"+it->first).c_str()) << endl;
1090  else
1091  cout << "NOT THERE" << endl;
1092 
1093  // EDIT seems to die if the list of edits is too long. So chunck them up.
1094  if(numReplacements%10 == 0 && numReplacements+nskipped!=gammaSyst.size()){
1095  edit="EDIT::"+lastPdf+"_("+lastPdf+","+editList+")";
1096  lastPdf+="_"; // append an underscore for the edit
1097  editList=""; // reset edit list
1098  precede="";
1099  cout << edit<< endl;
1100  proto->factory( edit.c_str() );
1101  RooAbsPdf* newOne = proto->pdf(lastPdf.c_str());
1102  if(!newOne)
1103  cout << "\n\n ---------------------\n WARNING: failed to make EDIT\n\n" << endl;
1104 
1105  }
1106  // add global observables
1107  const RooArgSet * gobs = proto->set("globalObservables");
1108  RooArgSet gobsNew(*gobs);
1109  gobsNew.add(*proto->var(TString::Format("nom_beta_%s",cname)) );
1110  proto->removeSet("globalObservables");
1111  proto->defineSet("globalObservables",gobsNew);
1112  gobsNew.Print();
1113 
1114  }
1115 
1116  /////////////////////////////////////////
1117 
1118  // MB: remove a systematic constraint
1119  for(it=noSyst.begin(); it!=noSyst.end(); ++it) {
1120 
1121  cout << "remove constraint for parameter" << it->first << endl;
1122  if(! proto->var(("alpha_"+it->first).c_str()) || ! proto->pdf(("alpha_"+it->first+"Constraint").c_str()) ) {
1123  cout << "systematic not there" << endl;
1124  nskipped++;
1125  continue;
1126  }
1127  numReplacements++;
1128 
1129  // dummy replacement pdf
1130  if ( !proto->var("one") ) { proto->factory("one[1.0]"); }
1131  proto->var("one")->setConstant();
1132 
1133  // replace constraints
1134  cout << "alpha_"+it->first+"Constraint=one" << endl;
1135  editList+=precede + "alpha_"+it->first+"Constraint=one";
1136  precede=",";
1137 
1138  // EDIT seems to die if the list of edits is too long. So chunck them up.
1139  if(numReplacements%10 == 0 && numReplacements+nskipped!=gammaSyst.size()){
1140  edit="EDIT::"+lastPdf+"_("+lastPdf+","+editList+")";
1141  lastPdf+="_"; // append an underscore for the edit
1142  editList=""; // reset edit list
1143  precede="";
1144  cout << edit << endl;
1145  proto->factory( edit.c_str() );
1146  RooAbsPdf* newOne = proto->pdf(lastPdf.c_str());
1147  if(!newOne) { cout << "\n\n ---------------------\n WARNING: failed to make EDIT\n\n" << endl; }
1148  }
1149  }
1150 
1151  /////////////////////////////////////////
1152 
1153  // commit last bunch of edits
1154  edit="EDIT::newSimPdf("+lastPdf+","+editList+")";
1155  cout << edit<< endl;
1156  proto->factory( edit.c_str() );
1157  // proto->writeToFile(("results/model_"+fRowTitle+"_edited.root").c_str());
1158  RooAbsPdf* newOne = proto->pdf("newSimPdf");
1159  if(newOne){
1160  // newOne->graphVizTree(("results/"+pdfName+"_"+fRowTitle+"newSimPdf.dot").c_str());
1161  combined_config->SetPdf(*newOne);
1162  }
1163  else{
1164  cout << "\n\n ---------------------\n WARNING: failed to make EDIT\n\n" << endl;
1165  }
1166  }
1167 
1169  // Change-> Now a static utility
1170 
1171  FILE* covFile = fopen ((filename).c_str(),"w");
1172 
1173  TIter iti = params->createIterator();
1174  TIter itj = params->createIterator();
1175  RooRealVar *myargi, *myargj;
1176  fprintf(covFile," ") ;
1177  while ((myargi = (RooRealVar *)iti.Next())) {
1178  if(myargi->isConstant()) continue;
1179  fprintf(covFile," & %s", myargi->GetName());
1180  }
1181  fprintf(covFile,"\\\\ \\hline \n" );
1182  iti.Reset();
1183  while ((myargi = (RooRealVar *)iti.Next())) {
1184  if(myargi->isConstant()) continue;
1185  fprintf(covFile,"%s", myargi->GetName());
1186  itj.Reset();
1187  while ((myargj = (RooRealVar *)itj.Next())) {
1188  if(myargj->isConstant()) continue;
1189  cout << myargi->GetName() << "," << myargj->GetName();
1190  fprintf(covFile, " & %.2f", result->correlation(*myargi, *myargj));
1191  }
1192  cout << endl;
1193  fprintf(covFile, " \\\\\n");
1194  }
1195  fclose(covFile);
1196 
1197  }
1198 
1199 
1200  ///////////////////////////////////////////////
1202 
1203  // check inputs (see JIRA-6890 )
1204 
1205  if (channel.GetSamples().empty()) {
1206  Error("MakeSingleChannelWorkspace","The input Channel does not contain any sample - return a nullptr");
1207  return 0;
1208  }
1209 
1210  // Set these by hand inside the function
1211  vector<string> systToFix = measurement.GetConstantParams();
1212  bool doRatio=false;
1213 
1214  // to time the macro
1215  TStopwatch t;
1216  t.Start();
1217  //ES// string channel_name=summary[0].channel;
1218  string channel_name = channel.GetName();
1219 
1220  /// MB: reset observable names for each new channel.
1221  fObsNameVec.clear();
1222 
1223  /// MB: label observables x,y,z, depending on histogram dimensionality
1224  /// GHL: Give it the first sample's nominal histogram as a template
1225  /// since the data histogram may not be present
1226  TH1* channel_hist_template = channel.GetSamples().at(0).GetHisto();
1227  if (fObsNameVec.empty()) { GuessObsNameVec(channel_hist_template); }
1228 
1229  for ( unsigned int idx=0; idx<fObsNameVec.size(); ++idx ) {
1230  fObsNameVec[idx] = "obs_" + fObsNameVec[idx] + "_" + channel_name ;
1231  }
1232 
1233  if (fObsNameVec.empty()) {
1234  fObsName= "obs_" + channel_name; // set name ov observable
1235  fObsNameVec.push_back( fObsName );
1236  }
1237 
1238  R__ASSERT( fObsNameVec.size()>=1 && fObsNameVec.size()<=3 );
1239 
1240  cout << "\n\n-------------------\nStarting to process " << channel_name << " channel with " << fObsNameVec.size() << " observables" << endl;
1241 
1242  //
1243  // our main workspace that we are using to construct the model
1244  //
1245  RooWorkspace* proto = new RooWorkspace(channel_name.c_str(), (channel_name+" workspace").c_str());
1246  auto proto_config = make_unique<ModelConfig>("ModelConfig", proto);
1247  proto_config->SetWorkspace(*proto);
1248 
1249  // preprocess functions
1250  vector<string>::iterator funcIter = fPreprocessFunctions.begin();
1251  for(;funcIter!= fPreprocessFunctions.end(); ++funcIter){
1252  cout <<"will preprocess this line: " << *funcIter <<endl;
1253  proto->factory(funcIter->c_str());
1254  proto->Print();
1255  }
1256 
1257  RooArgSet likelihoodTerms("likelihoodTerms"), constraintTerms("constraintTerms");
1258  vector<string> likelihoodTermNames, constraintTermNames, totSystTermNames, syst_x_expectedPrefixNames, normalizationNames;
1259 
1260  vector< pair<string,string> > statNamePairs;
1261  vector< pair<TH1*,TH1*> > statHistPairs; // <nominal, error>
1262  std::string statFuncName; // the name of the ParamHistFunc
1263  std::string statNodeName; // the name of the McStat Node
1264  // Constraint::Type statConstraintType=Constraint::Gaussian;
1265  // Double_t statRelErrorThreshold=0.0;
1266 
1267  string prefix, range;
1268 
1269  /////////////////////////////
1270  // shared parameters
1271  // this is ratio of lumi to nominal lumi. We will include relative uncertainty in model
1272  std::stringstream lumiStr;
1273  // lumi range
1274  lumiStr<<"["<<fNomLumi<<",0,"<<10.*fNomLumi<<"]";
1275  proto->factory(("Lumi"+lumiStr.str()).c_str());
1276  cout << "lumi str = " << lumiStr.str() << endl;
1277 
1278  std::stringstream lumiErrorStr;
1279  lumiErrorStr << "nominalLumi["<<fNomLumi << ",0,"<<fNomLumi+10*fLumiError<<"]," << fLumiError ;
1280  proto->factory(("Gaussian::lumiConstraint(Lumi,"+lumiErrorStr.str()+")").c_str());
1281  proto->var("nominalLumi")->setConstant();
1282  proto->defineSet("globalObservables","nominalLumi");
1283  //likelihoodTermNames.push_back("lumiConstraint");
1284  constraintTermNames.push_back("lumiConstraint");
1285  cout << "lumi Error str = " << lumiErrorStr.str() << endl;
1286 
1287  //proto->factory((string("SigXsecOverSM[1.,0.5,1..8]").c_str()));
1288  ///////////////////////////////////
1289  // loop through estimates, add expectation, floating bin predictions,
1290  // and terms that constrain floating to expectation via uncertainties
1291  // GHL: Loop over samples instead, which doesn't contain the data
1292  vector<Sample>::iterator it = channel.GetSamples().begin();
1293  for(; it!=channel.GetSamples().end(); ++it) {
1294 
1295  //ES// string overallSystName = it->name+"_"+it->channel+"_epsilon";
1296  Sample& sample = (*it);
1297  string overallSystName = sample.GetName() + "_" + channel_name + "_epsilon";
1298 
1299  string systSourcePrefix = "alpha_";
1300 
1301  // constraintTermNames and totSystTermNames are vectors that are passed
1302  // by reference and filled by this method
1303  AddConstraintTerms(proto,measurement, systSourcePrefix, overallSystName,
1304  sample.GetOverallSysList(), constraintTermNames , totSystTermNames);
1305 
1306  // GHL: Consider passing the NormFactor list instead of the entire sample
1307  overallSystName = AddNormFactor(proto, channel_name, overallSystName, sample, doRatio);
1308 
1309  // Create the string for the object
1310  // that is added to the RooRealSumPdf
1311  // for this channel
1312  string syst_x_expectedPrefix = "";
1313 
1314  // get histogram
1315  //ES// TH1* nominal = it->nominal;
1316  TH1* nominal = sample.GetHisto();
1317 
1318  // MB : HACK no option to have both non-hist variations and hist variations ?
1319  // get histogram
1320  // GHL: Okay, this is going to be non-trivial.
1321  // We will loop over histosys's, which contain both
1322  // the low hist and the high hist together.
1323 
1324  // Logic:
1325  // - If we have no HistoSys's, do part A
1326  // - else, if the histo syst's don't match, return (we ignore this case)
1327  // - finally, we take the syst's and apply the linear interpolation w/ constraint
1328 
1329  if(sample.GetHistoSysList().size() == 0) {
1330 
1331  // If no HistoSys
1332  cout << sample.GetName() + "_" + channel_name + " has no variation histograms " << endl;
1333  string expPrefix = sample.GetName() + "_" + channel_name; //+"_expN";
1334  syst_x_expectedPrefix = sample.GetName() + "_" + channel_name + "_overallSyst_x_Exp";
1335 
1336  ProcessExpectedHisto(sample.GetHisto(), proto, expPrefix, syst_x_expectedPrefix,
1337  overallSystName);
1338  }
1339  else {
1340  // If there ARE HistoSys(s)
1341  // name of source for variation
1342  string constraintPrefix = sample.GetName() + "_" + channel_name + "_Hist_alpha";
1343  syst_x_expectedPrefix = sample.GetName() + "_" + channel_name + "_overallSyst_x_HistSyst";
1344  // constraintTermNames are passed by reference and appended to,
1345  // overallSystName is a std::string for this sample
1346 
1347  LinInterpWithConstraint(proto, nominal, sample.GetHistoSysList(),
1348  constraintPrefix, syst_x_expectedPrefix, overallSystName,
1349  constraintTermNames);
1350  }
1351 
1352  ////////////////////////////////////
1353  // Add StatErrors to this Channel //
1354  ////////////////////////////////////
1355 
1356  if( sample.GetStatError().GetActivate() ) {
1357 
1358  if( fObsNameVec.size() > 3 ) {
1359  std::cout << "Cannot include Stat Error for histograms of more than 3 dimensions."
1360  << std::endl;
1361  throw hf_exc();
1362  } else {
1363 
1364  // If we are using StatUncertainties, we multiply this object
1365  // by the ParamHistFunc and then pass that to the
1366  // RooRealSumPdf by appending it's name to the list
1367 
1368  std::cout << "Sample: " << sample.GetName() << " to be included in Stat Error "
1369  << "for channel " << channel_name
1370  << std::endl;
1371 
1372  /*
1373  Constraint::Type type = channel.GetStatErrorConfig().GetConstraintType();
1374  statConstraintType = Constraint::Gaussian;
1375  if( type == Constraint::Gaussian) {
1376  std::cout << "Using Gaussian StatErrors" << std::endl;
1377  statConstraintType = Constraint::Gaussian;
1378  }
1379  if( type == Constraint::Poisson ) {
1380  std::cout << "Using Poisson StatErrors" << std::endl;
1381  statConstraintType = Constraint::Poisson;
1382  }
1383  */
1384 
1385  //statRelErrorThreshold = channel.GetStatErrorConfig().GetRelErrorThreshold();
1386 
1387  // First, get the uncertainty histogram
1388  // and push it back to our vectors
1389 
1390  //if( sample.GetStatError().GetErrorHist() ) {
1391  //statErrorHist = (TH1*) sample.GetStatError().GetErrorHist()->Clone();
1392  //}
1393  //if( statErrorHist == NULL ) {
1394 
1395  // We need to get the *ABSOLUTE* uncertainty for use in Stat Uncertainties
1396  // This can be done in one of two ways:
1397  // - Use the built-in Errors in the TH1 itself (they are aboslute)
1398  // - Take the supplied *RELATIVE* error and multiply by the nominal
1399  string UncertName = syst_x_expectedPrefix + "_StatAbsolUncert";
1400  TH1* statErrorHist = NULL;
1401 
1402  if( sample.GetStatError().GetErrorHist() == NULL ) {
1403  // Make the absolute stat error
1404  std::cout << "Making Statistical Uncertainty Hist for "
1405  << " Channel: " << channel_name
1406  << " Sample: " << sample.GetName()
1407  << std::endl;
1408  statErrorHist = MakeAbsolUncertaintyHist( UncertName, nominal );
1409  } else {
1410  // clone the error histograms because in case the sample has not error hist
1411  // it is created in MakeAbsolUncertainty
1412  // we need later to clean statErrorHist
1413  statErrorHist = (TH1*) sample.GetStatError().GetErrorHist()->Clone();
1414  // We assume the (relative) error is provided.
1415  // We must turn it into an absolute error
1416  // using the nominal histogram
1417  std::cout << "Using external histogram for Stat Errors for "
1418  << " Channel: " << channel_name
1419  << " Sample: " << sample.GetName()
1420  << std::endl;
1421  std::cout << "Error Histogram: " << statErrorHist->GetName() << std::endl;
1422  // Multiply the relative stat uncertainty by the
1423  // nominal to get the overall stat uncertainty
1424  statErrorHist->Multiply( nominal );
1425  statErrorHist->SetName( UncertName.c_str() );
1426  }
1427 
1428  // Save the nominal and error hists
1429  // for the building of constraint terms
1430  statHistPairs.push_back( pair<TH1*,TH1*>(nominal, statErrorHist) );
1431 
1432  // To do the 'conservative' version, we would need to do some
1433  // intervention here. We would probably need to create a different
1434  // ParamHistFunc for each sample in the channel. The would nominally
1435  // use the same gamma's, so we haven't increased the number of parameters
1436  // However, if a bin in the 'nominal' histogram is 0, we simply need to
1437  // change the parameter in that bin in the ParamHistFunc for this sample.
1438  // We also need to add a constraint term.
1439  // Actually, we'd probably not use the ParamHistFunc...?
1440  // we could remove the dependence in this ParamHistFunc on the ith gamma
1441  // and then create the poisson term: Pois(tau | n_exp)Pois(data | n_exp)
1442 
1443 
1444  // Next, try to get the ParamHistFunc (it may have been
1445  // created by another sample in this channel)
1446  // or create it if it doesn't yet exist:
1447  statFuncName = "mc_stat_" + channel_name;
1448  ParamHistFunc* paramHist = (ParamHistFunc*) proto->function( statFuncName.c_str() );
1449  if( paramHist == NULL ) {
1450 
1451  // Get a RooArgSet of the observables:
1452  // Names in the list fObsNameVec:
1453  RooArgList observables;
1454  std::vector<std::string>::iterator itr = fObsNameVec.begin();
1455  for (int idx=0; itr!=fObsNameVec.end(); ++itr, ++idx ) {
1456  observables.add( *proto->var(itr->c_str()) );
1457  }
1458 
1459  // Create the list of terms to
1460  // control the bin heights:
1461  std::string ParamSetPrefix = "gamma_stat_" + channel_name;
1462  Double_t gammaMin = 0.0;
1463  Double_t gammaMax = 10.0;
1464  RooArgList statFactorParams = ParamHistFunc::createParamSet(*proto,
1465  ParamSetPrefix.c_str(),
1466  observables,
1467  gammaMin, gammaMax);
1468 
1469  ParamHistFunc statUncertFunc(statFuncName.c_str(), statFuncName.c_str(),
1470  observables, statFactorParams );
1471 
1472  proto->import( statUncertFunc, RecycleConflictNodes() );
1473 
1474  paramHist = (ParamHistFunc*) proto->function( statFuncName.c_str() );
1475 
1476  } // END: If Statement: Create ParamHistFunc
1477 
1478  // Create the node as a product
1479  // of this function and the
1480  // expected value from MC
1481  statNodeName = sample.GetName() + "_" + channel_name + "_overallSyst_x_StatUncert";
1482 
1483  RooAbsReal* expFunc = (RooAbsReal*) proto->function( syst_x_expectedPrefix.c_str() );
1484  RooProduct nodeWithMcStat(statNodeName.c_str(), statNodeName.c_str(),
1485  RooArgSet(*paramHist, *expFunc) );
1486 
1487  proto->import( nodeWithMcStat, RecycleConflictNodes() );
1488 
1489  // Push back the final name of the node
1490  // to be used in the RooRealSumPdf
1491  // (node to be created later)
1492  syst_x_expectedPrefix = nodeWithMcStat.GetName();
1493 
1494  }
1495  } // END: if DoMcStat
1496 
1497 
1498  ///////////////////////////////////////////
1499  // Create a ShapeFactor for this channel //
1500  ///////////////////////////////////////////
1501 
1502  if( sample.GetShapeFactorList().size() > 0 ) {
1503 
1504  if( fObsNameVec.size() > 3 ) {
1505  std::cout << "Cannot include Stat Error for histograms of more than 3 dimensions."
1506  << std::endl;
1507  throw hf_exc();
1508  } else {
1509 
1510  std::cout << "Sample: " << sample.GetName() << " in channel: " << channel_name
1511  << " to be include a ShapeFactor."
1512  << std::endl;
1513 
1514  std::vector<ParamHistFunc*> paramHistFuncList;
1515  std::vector<std::string> shapeFactorNameList;
1516 
1517  for(unsigned int i=0; i < sample.GetShapeFactorList().size(); ++i) {
1518 
1519  ShapeFactor& shapeFactor = sample.GetShapeFactorList().at(i);
1520 
1521  std::string funcName = channel_name + "_" + shapeFactor.GetName() + "_shapeFactor";
1522  ParamHistFunc* paramHist = (ParamHistFunc*) proto->function( funcName.c_str() );
1523  if( paramHist == NULL ) {
1524 
1525  RooArgList observables;
1526  std::vector<std::string>::iterator itr = fObsNameVec.begin();
1527  for (int idx=0; itr!=fObsNameVec.end(); ++itr, ++idx ) {
1528  observables.add( *proto->var(itr->c_str()) );
1529  }
1530 
1531  // Create the Parameters
1532  std::string funcParams = "gamma_" + shapeFactor.GetName();
1533 
1534  // GHL: Again, we are putting hard ranges on the gamma's
1535  // We should change this to range from 0 to /inf
1536  RooArgList shapeFactorParams = ParamHistFunc::createParamSet(*proto,
1537  funcParams.c_str(),
1538  observables, 0, 1000);
1539 
1540  // Create the Function
1541  ParamHistFunc shapeFactorFunc( funcName.c_str(), funcName.c_str(),
1542  observables, shapeFactorParams );
1543 
1544  // Set an initial shape, if requested
1545  if( shapeFactor.GetInitialShape() != NULL ) {
1546  TH1* initialShape = shapeFactor.GetInitialShape();
1547  std::cout << "Setting Shape Factor: " << shapeFactor.GetName()
1548  << " to have initial shape from hist: "
1549  << initialShape->GetName()
1550  << std::endl;
1551  shapeFactorFunc.setShape( initialShape );
1552  }
1553 
1554  // Set the variables constant, if requested
1555  if( shapeFactor.IsConstant() ) {
1556  std::cout << "Setting Shape Factor: " << shapeFactor.GetName()
1557  << " to be constant" << std::endl;
1558  shapeFactorFunc.setConstant(true);
1559  }
1560 
1561  proto->import( shapeFactorFunc, RecycleConflictNodes() );
1562  paramHist = (ParamHistFunc*) proto->function( funcName.c_str() );
1563 
1564  } // End: Create ShapeFactor ParamHistFunc
1565 
1566  paramHistFuncList.push_back(paramHist);
1567  shapeFactorNameList.push_back(funcName);
1568 
1569  } // End loop over ShapeFactor Systematics
1570 
1571  // Now that we have the right ShapeFactor,
1572  // we multiply the expected function
1573 
1574  //std::string shapeFactorNodeName = syst_x_expectedPrefix + "_x_" + funcName;
1575  // Dynamically build the name as a long product
1576  std::string shapeFactorNodeName = syst_x_expectedPrefix;
1577  for( unsigned int i=0; i < shapeFactorNameList.size(); ++i) {
1578  shapeFactorNodeName += "_x_" + shapeFactorNameList.at(i);
1579  }
1580 
1581  RooAbsReal* expFunc = (RooAbsReal*) proto->function( syst_x_expectedPrefix.c_str() );
1582  RooArgSet nodesForProduct(*expFunc);
1583  for( unsigned int i=0; i < paramHistFuncList.size(); ++i) {
1584  nodesForProduct.add( *paramHistFuncList.at(i) );
1585  }
1586  //RooProduct nodeWithShapeFactor(shapeFactorNodeName.c_str(),
1587  // shapeFactorNodeName.c_str(),
1588  //RooArgSet(*paramHist, *expFunc) );
1589  RooProduct nodeWithShapeFactor(shapeFactorNodeName.c_str(),
1590  shapeFactorNodeName.c_str(),
1591  nodesForProduct );
1592 
1593  proto->import( nodeWithShapeFactor, RecycleConflictNodes() );
1594 
1595  // Push back the final name of the node
1596  // to be used in the RooRealSumPdf
1597  // (node to be created later)
1598  syst_x_expectedPrefix = nodeWithShapeFactor.GetName();
1599 
1600  }
1601  } // End: if ShapeFactorName!=""
1602 
1603 
1604  ////////////////////////////////////////
1605  // Create a ShapeSys for this channel //
1606  ////////////////////////////////////////
1607 
1608  if( sample.GetShapeSysList().size() != 0 ) {
1609 
1610  if( fObsNameVec.size() > 3 ) {
1611  std::cout << "Cannot include Stat Error for histograms of more than 3 dimensions."
1612  << std::endl;
1613  throw hf_exc();
1614  } else {
1615 
1616  // List of ShapeSys ParamHistFuncs
1617  std::vector<string> ShapeSysNames;
1618 
1619  for( unsigned int i = 0; i < sample.GetShapeSysList().size(); ++i) {
1620 
1621  // Create the ParamHistFunc's
1622  // Create their constraint terms and add them
1623  // to the list of constraint terms
1624 
1625  // Create a single RooProduct over all of these
1626  // paramHistFunc's
1627 
1628  // Send the name of that product to the RooRealSumPdf
1629 
1630  RooStats::HistFactory::ShapeSys& shapeSys = sample.GetShapeSysList().at(i);
1631 
1632  std::cout << "Sample: " << sample.GetName() << " in channel: " << channel_name
1633  << " to include a ShapeSys." << std::endl;
1634 
1635  std::string funcName = channel_name + "_" + shapeSys.GetName() + "_ShapeSys";
1636  ShapeSysNames.push_back( funcName );
1637  ParamHistFunc* paramHist = (ParamHistFunc*) proto->function( funcName.c_str() );
1638  if( paramHist == NULL ) {
1639 
1640  //std::string funcParams = "gamma_" + it->shapeFactorName;
1641  //paramHist = CreateParamHistFunc( proto, fObsNameVec, funcParams, funcName );
1642 
1643  RooArgList observables;
1644  std::vector<std::string>::iterator itr = fObsNameVec.begin();
1645  for(; itr!=fObsNameVec.end(); ++itr ) {
1646  observables.add( *proto->var(itr->c_str()) );
1647  }
1648 
1649  // Create the Parameters
1650  std::string funcParams = "gamma_" + shapeSys.GetName();
1651  RooArgList shapeFactorParams = ParamHistFunc::createParamSet(*proto,
1652  funcParams.c_str(),
1653  observables, 0, 10);
1654 
1655  // Create the Function
1656  ParamHistFunc shapeFactorFunc( funcName.c_str(), funcName.c_str(),
1657  observables, shapeFactorParams );
1658 
1659  proto->import( shapeFactorFunc, RecycleConflictNodes() );
1660  paramHist = (ParamHistFunc*) proto->function( funcName.c_str() );
1661 
1662  } // End: Create ShapeFactor ParamHistFunc
1663 
1664  // Create the constraint terms and add
1665  // them to the workspace (proto)
1666  // as well as the list of constraint terms (constraintTermNames)
1667 
1668  // The syst should be a fractional error
1669  TH1* shapeErrorHist = shapeSys.GetErrorHist();
1670 
1671  // Constraint::Type shapeConstraintType = Constraint::Gaussian;
1672  Constraint::Type systype = shapeSys.GetConstraintType();
1673  if( systype == Constraint::Gaussian) {
1674  systype = Constraint::Gaussian;
1675  }
1676  if( systype == Constraint::Poisson ) {
1677  systype = Constraint::Poisson;
1678  }
1679 
1680  Double_t minShapeUncertainty = 0.0;
1681  RooArgList shapeConstraints = createStatConstraintTerms(proto, constraintTermNames,
1682  *paramHist, shapeErrorHist,
1683  systype,
1684  minShapeUncertainty);
1685 
1686  } // End: Loop over ShapeSys vector in this EstimateSummary
1687 
1688  // Now that we have the list of ShapeSys ParamHistFunc names,
1689  // we create the total RooProduct
1690  // we multiply the expected functio
1691 
1692  std::string NodeName = syst_x_expectedPrefix;
1693  RooArgList ShapeSysForNode;
1694  RooAbsReal* expFunc = (RooAbsReal*) proto->function( syst_x_expectedPrefix.c_str() );
1695  ShapeSysForNode.add( *expFunc );
1696  for( unsigned int i = 0; i < ShapeSysNames.size(); ++i ) {
1697  std::string ShapeSysName = ShapeSysNames.at(i);
1698  ShapeSysForNode.add( *proto->function(ShapeSysName.c_str()) );
1699  NodeName = NodeName + "_x_" + ShapeSysName;
1700  }
1701 
1702  // Create the name for this NEW Node
1703  RooProduct nodeWithShapeFactor(NodeName.c_str(), NodeName.c_str(), ShapeSysForNode );
1704  proto->import( nodeWithShapeFactor, RecycleConflictNodes() );
1705 
1706  // Push back the final name of the node
1707  // to be used in the RooRealSumPdf
1708  // (node to be created later)
1709  syst_x_expectedPrefix = nodeWithShapeFactor.GetName();
1710 
1711  } // End: NumObsVar == 1
1712 
1713  } // End: GetShapeSysList.size() != 0
1714 
1715  // Append the name of the "node"
1716  // that is to be summed with the
1717  // RooRealSumPdf
1718  syst_x_expectedPrefixNames.push_back(syst_x_expectedPrefix);
1719 
1720  // GHL: This was pretty confusing before,
1721  // hopefully using the measurement directly
1722  // will improve it
1723  if( sample.GetNormalizeByTheory() ) {
1724  normalizationNames.push_back( "Lumi" );
1725  }
1726  else {
1727  TString lumiParamString;
1728  lumiParamString += measurement.GetLumi();
1729  lumiParamString.ReplaceAll(' ', TString());
1730  normalizationNames.push_back(lumiParamString.Data());
1731  }
1732 
1733  } // END: Loop over EstimateSummaries
1734  // proto->Print();
1735 
1736  // If a non-zero number of samples call for
1737  // Stat Uncertainties, create the statFactor functions
1738  if( statHistPairs.size() > 0 ) {
1739 
1740  // Create the histogram of (binwise)
1741  // stat uncertainties:
1742  TH1* fracStatError = MakeScaledUncertaintyHist( statNodeName + "_RelErr", statHistPairs );
1743  if( fracStatError == NULL ) {
1744  std::cout << "Error: Failed to make ScaledUncertaintyHist for: "
1745  << statNodeName << std::endl;
1746  throw hf_exc();
1747  }
1748 
1749  // Using this TH1* of fractinal stat errors,
1750  // create a set of constraint terms:
1751  ParamHistFunc* chanStatUncertFunc = (ParamHistFunc*) proto->function( statFuncName.c_str() );
1752  std::cout << "About to create Constraint Terms from: "
1753  << chanStatUncertFunc->GetName()
1754  << " params: " << chanStatUncertFunc->paramList()
1755  << std::endl;
1756 
1757  // Get the constraint type and the
1758  // rel error threshold from the (last)
1759  // EstimateSummary looped over (but all
1760  // should be the same)
1761 
1762  // Get the type of StatError constraint from the channel
1763  Constraint::Type statConstraintType = channel.GetStatErrorConfig().GetConstraintType();
1764  if( statConstraintType == Constraint::Gaussian) {
1765  std::cout << "Using Gaussian StatErrors in channel: " << channel.GetName() << std::endl;
1766  }
1767  if( statConstraintType == Constraint::Poisson ) {
1768  std::cout << "Using Poisson StatErrors in channel: " << channel.GetName() << std::endl;
1769  }
1770 
1771  double statRelErrorThreshold = channel.GetStatErrorConfig().GetRelErrorThreshold();
1772  RooArgList statConstraints = createStatConstraintTerms(proto, constraintTermNames,
1773  *chanStatUncertFunc, fracStatError,
1774  statConstraintType,
1775  statRelErrorThreshold);
1776 
1777 
1778  // clean stat hist pair (need to delete second histogram)
1779  for (unsigned int i = 0; i < statHistPairs.size() ; ++i )
1780  delete statHistPairs[i].second;
1781 
1782  statHistPairs.clear();
1783  //delete also histogram of stat uncertainties created in MakeScaledUncertaintyHist
1784  delete fracStatError;
1785 
1786  } // END: Loop over stat Hist Pairs
1787 
1788 
1789  ///////////////////////////////////
1790  // for ith bin calculate totN_i = lumi * sum_j expected_j * syst_j
1791  //MakeTotalExpected(proto,channel_name+"_model",channel_name,"Lumi",fLowBin,fHighBin,
1792  // syst_x_expectedPrefixNames, normalizationNames);
1793  MakeTotalExpected(proto, channel_name+"_model", //channel_name,"Lumi",fLowBin,fHighBin,
1794  syst_x_expectedPrefixNames, normalizationNames);
1795  likelihoodTermNames.push_back(channel_name+"_model");
1796 
1797  //////////////////////////////////////
1798  // fix specified parameters
1799  for(unsigned int i=0; i<systToFix.size(); ++i){
1800  RooRealVar* temp = proto->var((systToFix.at(i)).c_str());
1801  if(temp) {
1802  // set the parameter constant
1803  temp->setConstant();
1804 
1805  // remove the corresponding auxiliary observable from the global observables
1806  RooRealVar* auxMeas = NULL;
1807  if(systToFix.at(i)=="Lumi"){
1808  auxMeas = proto->var("nominalLumi");
1809  } else {
1810  auxMeas = proto->var(TString::Format("nom_%s",temp->GetName()));
1811  }
1812 
1813  if(auxMeas){
1814  const_cast<RooArgSet*>(proto->set("globalObservables"))->remove(*auxMeas);
1815  } else{
1816  cout << "could not corresponding auxiliary measurement "
1817  << TString::Format("nom_%s",temp->GetName()) << endl;
1818  }
1819  } else {
1820  cout << "could not find variable " << systToFix.at(i)
1821  << " could not set it to constant" << endl;
1822  }
1823  }
1824 
1825  //////////////////////////////////////
1826  // final proto model
1827  for(unsigned int i=0; i<constraintTermNames.size(); ++i){
1828  RooAbsArg* proto_arg = (proto->arg(constraintTermNames[i].c_str()));
1829  if( proto_arg==NULL ) {
1830  std::cout << "Error: Cannot find arg set: " << constraintTermNames.at(i)
1831  << " in workspace: " << proto->GetName() << std::endl;
1832  throw hf_exc();
1833  }
1834  constraintTerms.add( *proto_arg );
1835  // constraintTerms.add(* proto_arg(proto->arg(constraintTermNames[i].c_str())) );
1836  }
1837  for(unsigned int i=0; i<likelihoodTermNames.size(); ++i){
1838  RooAbsArg* proto_arg = (proto->arg(likelihoodTermNames[i].c_str()));
1839  if( proto_arg==NULL ) {
1840  std::cout << "Error: Cannot find arg set: " << likelihoodTermNames.at(i)
1841  << " in workspace: " << proto->GetName() << std::endl;
1842  throw hf_exc();
1843  }
1844  likelihoodTerms.add( *proto_arg );
1845  }
1846  proto->defineSet("constraintTerms",constraintTerms);
1847  proto->defineSet("likelihoodTerms",likelihoodTerms);
1848  // proto->Print();
1849 
1850  // list of observables
1851  RooArgList observables;
1852  std::string observablesStr;
1853 
1854  std::vector<std::string>::iterator itr = fObsNameVec.begin();
1855  for(; itr!=fObsNameVec.end(); ++itr ) {
1856  observables.add( *proto->var(itr->c_str()) );
1857  if (!observablesStr.empty()) { observablesStr += ","; }
1858  observablesStr += *itr;
1859  }
1860 
1861  // We create two sets, one for backwards compatability
1862  // The other to make a consistent naming convention
1863  // between individual channels and the combined workspace
1864  proto->defineSet("observables", TString::Format("%s",observablesStr.c_str()));
1865  proto->defineSet("observablesSet", TString::Format("%s",observablesStr.c_str()));
1866 
1867  // Create the ParamHistFunc
1868  // after observables have been made
1869  cout <<"-----------------------------------------"<<endl;
1870  cout <<"import model into workspace" << endl;
1871 
1872  auto model = make_unique<RooProdPdf>(
1873  ("model_"+channel_name).c_str(), // MB : have changed this into conditional pdf. Much faster for toys!
1874  "product of Poissons accross bins for a single channel",
1875  constraintTerms, Conditional(likelihoodTerms,observables));
1876  proto->import(*model,RecycleConflictNodes());
1877 
1878  proto_config->SetPdf(*model);
1879  proto_config->SetObservables(observables);
1880  proto_config->SetGlobalObservables(*proto->set("globalObservables"));
1881  // proto->writeToFile(("results/model_"+channel+".root").c_str());
1882  // fill out nuisance parameters in model config
1883  // proto_config->GuessObsAndNuisance(*proto->data("asimovData"));
1884  proto->import(*proto_config,proto_config->GetName());
1885  proto->importClassCode();
1886 
1887  ///////////////////////////
1888  // make data sets
1889  // THis works and is natural, but the memory size of the simultaneous dataset grows exponentially with channels
1890  const char* weightName="weightVar";
1891  proto->factory(TString::Format("%s[0,-1e10,1e10]",weightName));
1892  proto->defineSet("obsAndWeight",TString::Format("%s,%s",weightName,observablesStr.c_str()));
1893 
1894  /* Old code for generating the asimov
1895  Asimov generation is now done later...
1896 
1897  RooAbsData* asimov_data = model->generateBinned(observables,ExpectedData());
1898 
1899  /// Asimov dataset
1900  RooDataSet* asimovDataUnbinned = new RooDataSet("asimovData","",*proto->set("obsAndWeight"),weightName);
1901  for(int i=0; i<asimov_data->numEntries(); ++i){
1902  asimov_data->get(i)->Print("v");
1903  //cout << "GREPME : " << i << " " << data->weight() <<endl;
1904  asimovDataUnbinned->add( *asimov_data->get(i), asimov_data->weight() );
1905  }
1906  proto->import(*asimovDataUnbinned);
1907  */
1908 
1909  // New Asimov Generation: Use the code in the Asymptotic calculator
1910  // Need to get the ModelConfig...
1911  unique_ptr<RooAbsData> asimov_dataset(AsymptoticCalculator::GenerateAsimovData(*model, observables));
1912  proto->import(dynamic_cast<RooDataSet&>(*asimov_dataset), Rename("asimovData"));
1913 
1914  // GHL: Determine to use data if the hist isn't 'NULL'
1915  if(channel.GetData().GetHisto() != NULL) {
1916 
1917  Data& data = channel.GetData();
1918  TH1* mnominal = data.GetHisto();
1919  if( !mnominal ) {
1920  std::cout << "Error: Data histogram for channel: " << channel.GetName()
1921  << " is NULL" << std::endl;
1922  throw hf_exc();
1923  }
1924 
1925  // THis works and is natural, but the memory size of the simultaneous dataset grows exponentially with channels
1926  auto obsDataUnbinned = make_unique<RooDataSet>("obsData","",*proto->set("obsAndWeight"),weightName);
1927 
1928 
1929  ConfigureHistFactoryDataset( obsDataUnbinned.get(), mnominal,
1930  proto, fObsNameVec );
1931 
1932  /*
1933  //ES// TH1* mnominal = summary.at(0).nominal;
1934  TH1* mnominal = data.GetHisto();
1935  TAxis* ax = mnominal->GetXaxis();
1936  TAxis* ay = mnominal->GetYaxis();
1937  TAxis* az = mnominal->GetZaxis();
1938 
1939  for (int i=1; i<=ax->GetNbins(); ++i) { // 1 or more dimension
1940  Double_t xval = ax->GetBinCenter(i);
1941  proto->var( fObsNameVec[0].c_str() )->setVal( xval );
1942  if (fObsNameVec.size()==1) {
1943  Double_t fval = mnominal->GetBinContent(i);
1944  obsDataUnbinned->add( *proto->set("obsAndWeight"), fval );
1945  } else { // 2 or more dimensions
1946  for (int j=1; j<=ay->GetNbins(); ++j) {
1947  Double_t yval = ay->GetBinCenter(j);
1948  proto->var( fObsNameVec[1].c_str() )->setVal( yval );
1949  if (fObsNameVec.size()==2) {
1950  Double_t fval = mnominal->GetBinContent(i,j);
1951  obsDataUnbinned->add( *proto->set("obsAndWeight"), fval );
1952  } else { // 3 dimensions
1953  for (int k=1; k<=az->GetNbins(); ++k) {
1954  Double_t zval = az->GetBinCenter(k);
1955  proto->var( fObsNameVec[2].c_str() )->setVal( zval );
1956  Double_t fval = mnominal->GetBinContent(i,j,k);
1957  obsDataUnbinned->add( *proto->set("obsAndWeight"), fval );
1958  }
1959  }
1960  }
1961  }
1962  }
1963  */
1964 
1965  proto->import(*obsDataUnbinned);
1966  } // End: Has non-null 'data' entry
1967 
1968 
1969  for(unsigned int i=0; i < channel.GetAdditionalData().size(); ++i) {
1970 
1971  Data& data = channel.GetAdditionalData().at(i);
1972  std::string dataName = data.GetName();
1973  TH1* mnominal = data.GetHisto();
1974  if( !mnominal ) {
1975  std::cout << "Error: Additional Data histogram for channel: " << channel.GetName()
1976  << " with name: " << dataName << " is NULL" << std::endl;
1977  throw hf_exc();
1978  }
1979 
1980  // THis works and is natural, but the memory size of the simultaneous dataset grows exponentially with channels
1981  auto obsDataUnbinned = make_unique<RooDataSet>(dataName.c_str(), dataName.c_str(),
1982  *proto->set("obsAndWeight"), weightName);
1983 
1984  ConfigureHistFactoryDataset( obsDataUnbinned.get(), mnominal,
1985  proto, fObsNameVec );
1986 
1987  /*
1988  //ES// TH1* mnominal = summary.at(0).nominal;
1989  TH1* mnominal = data.GetHisto();
1990  TAxis* ax = mnominal->GetXaxis();
1991  TAxis* ay = mnominal->GetYaxis();
1992  TAxis* az = mnominal->GetZaxis();
1993 
1994  for (int i=1; i<=ax->GetNbins(); ++i) { // 1 or more dimension
1995  Double_t xval = ax->GetBinCenter(i);
1996  proto->var( fObsNameVec[0].c_str() )->setVal( xval );
1997  if (fObsNameVec.size()==1) {
1998  Double_t fval = mnominal->GetBinContent(i);
1999  obsDataUnbinned->add( *proto->set("obsAndWeight"), fval );
2000  } else { // 2 or more dimensions
2001  for (int j=1; j<=ay->GetNbins(); ++j) {
2002  Double_t yval = ay->GetBinCenter(j);
2003  proto->var( fObsNameVec[1].c_str() )->setVal( yval );
2004  if (fObsNameVec.size()==2) {
2005  Double_t fval = mnominal->GetBinContent(i,j);
2006  obsDataUnbinned->add( *proto->set("obsAndWeight"), fval );
2007  } else { // 3 dimensions
2008  for (int k=1; k<=az->GetNbins(); ++k) {
2009  Double_t zval = az->GetBinCenter(k);
2010  proto->var( fObsNameVec[2].c_str() )->setVal( zval );
2011  Double_t fval = mnominal->GetBinContent(i,j,k);
2012  obsDataUnbinned->add( *proto->set("obsAndWeight"), fval );
2013  }
2014  }
2015  }
2016  }
2017  }
2018  */
2019 
2020  proto->import(*obsDataUnbinned);
2021 
2022  } // End: Has non-null 'data' entry
2023 
2024  proto->Print();
2025  return proto;
2026  }
2027 
2028 
2030  TH1* mnominal,
2032  std::vector<std::string> ObsNameVec) {
2033 
2034  // Take a RooDataSet and fill it with the entries
2035  // from a TH1*, using the observable names to
2036  // determine the columns
2037 
2038  if (ObsNameVec.empty() ) {
2039  Error("ConfigureHistFactoryDataset","Invalid input - return");
2040  return;
2041  }
2042 
2043  //ES// TH1* mnominal = summary.at(0).nominal;
2044  // TH1* mnominal = data.GetHisto();
2045  TAxis* ax = mnominal->GetXaxis();
2046  TAxis* ay = mnominal->GetYaxis();
2047  TAxis* az = mnominal->GetZaxis();
2048 
2049  for (int i=1; i<=ax->GetNbins(); ++i) { // 1 or more dimension
2050 
2051  Double_t xval = ax->GetBinCenter(i);
2052  proto->var( ObsNameVec[0].c_str() )->setVal( xval );
2053 
2054  if(ObsNameVec.size()==1) {
2055  Double_t fval = mnominal->GetBinContent(i);
2056  obsDataUnbinned->add( *proto->set("obsAndWeight"), fval );
2057  } else { // 2 or more dimensions
2058 
2059  for(int j=1; j<=ay->GetNbins(); ++j) {
2060  Double_t yval = ay->GetBinCenter(j);
2061  proto->var( ObsNameVec[1].c_str() )->setVal( yval );
2062 
2063  if(ObsNameVec.size()==2) {
2064  Double_t fval = mnominal->GetBinContent(i,j);
2065  obsDataUnbinned->add( *proto->set("obsAndWeight"), fval );
2066  } else { // 3 dimensions
2067 
2068  for(int k=1; k<=az->GetNbins(); ++k) {
2069  Double_t zval = az->GetBinCenter(k);
2070  proto->var( ObsNameVec[2].c_str() )->setVal( zval );
2071  Double_t fval = mnominal->GetBinContent(i,j,k);
2072  obsDataUnbinned->add( *proto->set("obsAndWeight"), fval );
2073  }
2074  }
2075  }
2076  }
2077  }
2078  }
2079 
2081  {
2082  fObsNameVec.clear();
2083 
2084  // determine histogram dimensionality
2085  unsigned int histndim(1);
2086  std::string classname = hist->ClassName();
2087  if (classname.find("TH1")==0) { histndim=1; }
2088  else if (classname.find("TH2")==0) { histndim=2; }
2089  else if (classname.find("TH3")==0) { histndim=3; }
2090 
2091  for ( unsigned int idx=0; idx<histndim; ++idx ) {
2092  if (idx==0) { fObsNameVec.push_back("x"); }
2093  if (idx==1) { fObsNameVec.push_back("y"); }
2094  if (idx==2) { fObsNameVec.push_back("z"); }
2095  }
2096  }
2097 
2098 
2099  RooWorkspace* HistoToWorkspaceFactoryFast::MakeCombinedModel(vector<string> ch_names, vector<RooWorkspace*> chs)
2100  {
2101 
2102 
2103  // check first the inputs (see JIRA-6890)
2104  if (ch_names.empty() || chs.empty() ) {
2105  Error("MakeCombinedModel","Input vectors are empty - return a nullptr");
2106  return 0;
2107  }
2108  if (chs.size() < ch_names.size() ) {
2109  Error("MakeCombinedModel","Input vector of workspace has an invalid size - return a nullptr");
2110  return 0;
2111  }
2112 
2113  //
2114  /// These things were used for debugging. Maybe useful in the future
2115  //
2116 
2117  map<string, RooAbsPdf*> pdfMap;
2118  vector<RooAbsPdf*> models;
2119  stringstream ss;
2120 
2121  RooArgList obsList;
2122  for(unsigned int i = 0; i< ch_names.size(); ++i){
2123  ModelConfig * config = (ModelConfig *) chs[i]->obj("ModelConfig");
2124  obsList.add(*config->GetObservables());
2125  }
2126  cout <<"full list of observables:"<<endl;
2127  obsList.Print();
2128 
2129  RooArgSet globalObs;
2130  for(unsigned int i = 0; i< ch_names.size(); ++i){
2131  string channel_name=ch_names[i];
2132 
2133  if (ss.str().empty()) ss << channel_name ;
2134  else ss << ',' << channel_name ;
2135  RooWorkspace * ch=chs[i];
2136 
2137  RooAbsPdf* model = ch->pdf(("model_"+channel_name).c_str());
2138  if(!model) cout <<"failed to find model for channel"<<endl;
2139  // cout << "int = " << model->createIntegral(*obsN)->getVal() << endl;;
2140  models.push_back(model);
2141  globalObs.add(*ch->set("globalObservables"));
2142 
2143  // constrainedParams->add( * ch->set("constrainedParams") );
2144  pdfMap[channel_name]=model;
2145  }
2146  //constrainedParams->Print();
2147 
2148  cout << "\n\n------------------\n Entering combination" << endl;
2149  RooWorkspace* combined = new RooWorkspace("combined");
2150  // RooWorkspace* combined = chs[0];
2151 
2152 
2153  RooCategory* channelCat = (RooCategory*) combined->factory(("channelCat["+ss.str()+"]").c_str());
2154  RooSimultaneous * simPdf= new RooSimultaneous("simPdf","",pdfMap, *channelCat);
2155  ModelConfig * combined_config = new ModelConfig("ModelConfig", combined);
2156  combined_config->SetWorkspace(*combined);
2157  // combined_config->SetNuisanceParameters(*constrainedParams);
2158 
2159  combined->import(globalObs);
2160  combined->defineSet("globalObservables",globalObs);
2161  combined_config->SetGlobalObservables(*combined->set("globalObservables"));
2162 
2163 
2164  ////////////////////////////////////////////
2165  // Make toy simultaneous dataset
2166  cout <<"-----------------------------------------"<<endl;
2167  cout << "create toy data for " << ss.str() << endl;
2168 
2169 
2170  // now with weighted datasets
2171  // First Asimov
2172  //RooDataSet * simData=NULL;
2173  combined->factory("weightVar[0,-1e10,1e10]");
2174  obsList.add(*combined->var("weightVar"));
2175 
2176  // Loop over channels and create the asimov
2177  /*
2178  for(unsigned int i = 0; i< ch_names.size(); ++i){
2179  cout << "merging data for channel " << ch_names[i].c_str() << endl;
2180  RooDataSet * tempData=new RooDataSet(ch_names[i].c_str(),"", obsList, Index(*channelCat),
2181  WeightVar("weightVar"),
2182  Import(ch_names[i].c_str(),*(RooDataSet*)chs[i]->data("asimovData")));
2183  if(simData){
2184  simData->append(*tempData);
2185  delete tempData;
2186  }else{
2187  simData = tempData;
2188  }
2189  }
2190 
2191  if (simData) combined->import(*simData,Rename("asimovData"));
2192  */
2193  RooDataSet* asimov_combined = (RooDataSet*) AsymptoticCalculator::GenerateAsimovData(*simPdf,
2194  obsList);
2195  if( asimov_combined ) {
2196  combined->import( *asimov_combined, Rename("asimovData"));
2197  }
2198  else {
2199  std::cout << "Error: Failed to create combined asimov dataset" << std::endl;
2200  throw hf_exc();
2201  }
2202  delete asimov_combined;
2203 
2204  // Now merge the observable datasets across the channels
2205  if(chs[0]->data("obsData") != NULL) {
2206  MergeDataSets(combined, chs, ch_names, "obsData", obsList, channelCat);
2207  }
2208 
2209  /*
2210  if(chs[0]->data("obsData") != NULL){
2211  RooDataSet * simData=NULL;
2212  //simData=NULL;
2213 
2214  // Loop through channels, get their individual datasets,
2215  // and add them to the combined dataset
2216  for(unsigned int i = 0; i< ch_names.size(); ++i){
2217  cout << "merging data for channel " << ch_names[i].c_str() << endl;
2218 
2219  RooDataSet* obsDataInChannel = (RooDataSet*) chs[i]->data("obsData");
2220  RooDataSet * tempData = new RooDataSet(ch_names[i].c_str(),"", obsList, Index(*channelCat),
2221  WeightVar("weightVar"),
2222  Import(ch_names[i].c_str(),*obsDataInChannel));
2223  // *(RooDataSet*) chs[i]->data("obsData")));
2224  if(simData) {
2225  simData->append(*tempData);
2226  delete tempData;
2227  }
2228  else {
2229  simData = tempData;
2230  }
2231  } // End Loop Over Channels
2232 
2233  // Check that we successfully created the dataset
2234  // and import it into the workspace
2235  if(simData) {
2236  combined->import(*simData, Rename("obsData"));
2237  }
2238  else {
2239  std::cout << "Error: Unable to merge observable datasets" << std::endl;
2240  throw hf_exc();
2241  }
2242 
2243  } // End 'if' on data != NULL
2244  */
2245 
2246  // Now, create any additional Asimov datasets that
2247  // are configured in the measurement
2248 
2249 
2250  // obsList.Print();
2251  // combined->import(obsList);
2252  // combined->Print();
2253 
2254  obsList.add(*channelCat);
2255  combined->defineSet("observables",obsList);
2256  combined_config->SetObservables(*combined->set("observables"));
2257 
2258  combined->Print();
2259 
2260  cout << "\n\n----------------\n Importing combined model" << endl;
2261  combined->import(*simPdf,RecycleConflictNodes());
2262  //combined->import(*simPdf, RenameVariable("SigXsecOverSM","SigXsecOverSM_comb"));
2263  // cout << "check pointer " << simPdf << endl;
2264  // cout << "check val " << simPdf->getVal() << endl;
2265 
2266  std::map< std::string, double>::iterator param_itr = fParamValues.begin();
2267  for( ; param_itr != fParamValues.end(); ++param_itr ){
2268  // make sure they are fixed
2269  std::string paramName = param_itr->first;
2270  double paramVal = param_itr->second;
2271 
2272  RooRealVar* temp = combined->var( paramName.c_str() );
2273  if(temp) {
2274  temp->setVal( paramVal );
2275  cout <<"setting " << paramName << " to the value: " << paramVal << endl;
2276  } else
2277  cout << "could not find variable " << paramName << " could not set its value" << endl;
2278  }
2279 
2280 
2281  for(unsigned int i=0; i<fSystToFix.size(); ++i){
2282  // make sure they are fixed
2283  RooRealVar* temp = combined->var((fSystToFix.at(i)).c_str());
2284  if(temp) {
2285  temp->setConstant();
2286  cout <<"setting " << fSystToFix.at(i) << " constant" << endl;
2287  } else
2288  cout << "could not find variable " << fSystToFix.at(i) << " could not set it to constant" << endl;
2289  }
2290 
2291  ///
2292  /// writing out the model in graphViz
2293  ///
2294  // RooAbsPdf* customized=combined->pdf("simPdf");
2295  //combined_config->SetPdf(*customized);
2296  combined_config->SetPdf(*simPdf);
2297  // combined_config->GuessObsAndNuisance(*simData);
2298  // customized->graphVizTree(("results/"+fResultsPrefixStr.str()+"_simul.dot").c_str());
2299  combined->import(*combined_config,combined_config->GetName());
2300  combined->importClassCode();
2301  // combined->writeToFile("results/model_combined.root");
2302 
2303  //clean up
2304  delete combined_config;
2305  delete simPdf;
2306 
2307  return combined;
2308  }
2309 
2310 
2312  std::vector<RooWorkspace*> wspace_vec,
2313  std::vector<std::string> channel_names,
2314  std::string dataSetName,
2315  RooArgList obsList,
2316  RooCategory* channelCat) {
2317 
2318  // Create the total dataset
2319  RooDataSet* simData=NULL;
2320 
2321  // Loop through channels, get their individual datasets,
2322  // and add them to the combined dataset
2323  for(unsigned int i = 0; i< channel_names.size(); ++i){
2324 
2325  // Grab the dataset for the existing channel
2326  std::cout << "Merging data for channel " << channel_names[i].c_str() << std::endl;
2327  RooDataSet* obsDataInChannel = (RooDataSet*) wspace_vec[i]->data(dataSetName.c_str());
2328  if( !obsDataInChannel ) {
2329  std::cout << "Error: Can't find DataSet: " << dataSetName
2330  << " in channel: " << channel_names.at(i)
2331  << std::endl;
2332  throw hf_exc();
2333  }
2334 
2335  // Create the new Dataset
2336  RooDataSet * tempData = new RooDataSet(channel_names[i].c_str(),"",
2337  obsList, Index(*channelCat),
2338  WeightVar("weightVar"),
2339  Import(channel_names[i].c_str(),*obsDataInChannel));
2340  if(simData) {
2341  simData->append(*tempData);
2342  delete tempData;
2343  }
2344  else {
2345  simData = tempData;
2346  }
2347  } // End Loop Over Channels
2348 
2349  // Check that we successfully created the dataset
2350  // and import it into the workspace
2351  if(simData) {
2352  combined->import(*simData, Rename(dataSetName.c_str()));
2353  delete simData;
2354  simData = (RooDataSet*) combined->data(dataSetName.c_str() );
2355  }
2356  else {
2357  std::cout << "Error: Unable to merge observable datasets" << std::endl;
2358  throw hf_exc();
2359  }
2360 
2361  return simData;
2362 
2363  }
2364 
2365 
2366  TH1* HistoToWorkspaceFactoryFast::MakeAbsolUncertaintyHist( const std::string& Name, const TH1* Nominal ) {
2367 
2368  // Take a nominal TH1* and create
2369  // a TH1 representing the binwise
2370  // errors (taken from the nominal TH1)
2371 
2372  TH1* ErrorHist = (TH1*) Nominal->Clone( Name.c_str() );
2373  ErrorHist->Reset();
2374 
2375  Int_t numBins = Nominal->GetNbinsX()*Nominal->GetNbinsY()*Nominal->GetNbinsZ();
2376  Int_t binNumber = 0;
2377 
2378  // Loop over bins
2379  for( Int_t i_bin = 0; i_bin < numBins; ++i_bin) {
2380 
2381  binNumber++;
2382  // Ignore underflow / overflow
2383  while( Nominal->IsBinUnderflow(binNumber) || Nominal->IsBinOverflow(binNumber) ){
2384  binNumber++;
2385  }
2386 
2387  Double_t histError = Nominal->GetBinError( binNumber );
2388 
2389  // Check that histError != NAN
2390  if( histError != histError ) {
2391  std::cout << "Warning: In histogram " << Nominal->GetName()
2392  << " bin error for bin " << i_bin
2393  << " is NAN. Not using Error!!!"
2394  << std::endl;
2395  throw hf_exc();
2396  //histError = sqrt( histContent );
2397  //histError = 0;
2398  }
2399 
2400  // Check that histError ! < 0
2401  if( histError < 0 ) {
2402  std::cout << "Warning: In histogram " << Nominal->GetName()
2403  << " bin error for bin " << binNumber
2404  << " is < 0. Setting Error to 0"
2405  << std::endl;
2406  //histError = sqrt( histContent );
2407  histError = 0;
2408  }
2409 
2410  ErrorHist->SetBinContent( binNumber, histError );
2411 
2412  }
2413 
2414  return ErrorHist;
2415 
2416  }
2417 
2418  TH1* HistoToWorkspaceFactoryFast::MakeScaledUncertaintyHist( const std::string& Name, std::vector< std::pair<TH1*, TH1*> > HistVec ) {
2419 
2420  // Take a list of < nominal, absolError > TH1* pairs
2421  // and construct a single histogram representing the
2422  // total fractional error as:
2423 
2424  // UncertInQuad(bin i) = Sum: absolUncert*absolUncert
2425  // Total(bin i) = Sum: Value
2426  //
2427  // TotalFracError(bin i) = Sqrt( UncertInQuad(i) ) / TotalBin(i)
2428 
2429 
2430  unsigned int numHists = HistVec.size();
2431 
2432  if( numHists == 0 ) {
2433  std::cout << "Warning: Empty Hist Vector, cannot create total uncertainty" << std::endl;
2434  return NULL;
2435  }
2436 
2437  TH1* HistTemplate = HistVec.at(0).first;
2438  Int_t numBins = HistTemplate->GetNbinsX()*HistTemplate->GetNbinsY()*HistTemplate->GetNbinsZ();
2439 
2440  // Check that all histograms
2441  // have the same bins
2442  for( unsigned int i = 0; i < HistVec.size(); ++i ) {
2443 
2444  TH1* nominal = HistVec.at(i).first;
2445  TH1* error = HistVec.at(i).second;
2446 
2447  if( nominal->GetNbinsX()*nominal->GetNbinsY()*nominal->GetNbinsZ() != numBins ) {
2448  std::cout << "Error: Provided hists have unequal bins" << std::endl;
2449  return NULL;
2450  }
2451  if( error->GetNbinsX()*error->GetNbinsY()*error->GetNbinsZ() != numBins ) {
2452  std::cout << "Error: Provided hists have unequal bins" << std::endl;
2453  return NULL;
2454  }
2455  }
2456 
2457  std::vector<double> TotalBinContent( numBins, 0.0);
2458  std::vector<double> HistErrorsSqr( numBins, 0.0);
2459 
2460  Int_t binNumber = 0;
2461 
2462  // Loop over bins
2463  for( Int_t i_bins = 0; i_bins < numBins; ++i_bins) {
2464 
2465  binNumber++;
2466  while( HistTemplate->IsBinUnderflow(binNumber) || HistTemplate->IsBinOverflow(binNumber) ){
2467  binNumber++;
2468  }
2469 
2470  for( unsigned int i_hist = 0; i_hist < numHists; ++i_hist ) {
2471 
2472  TH1* nominal = HistVec.at(i_hist).first;
2473  TH1* error = HistVec.at(i_hist).second;
2474 
2475  //Int_t binNumber = i_bins + 1;
2476 
2477  Double_t histValue = nominal->GetBinContent( binNumber );
2478  Double_t histError = error->GetBinContent( binNumber );
2479  /*
2480  std::cout << " Getting Bin content for Stat Uncertainty"
2481  << " Nom name: " << nominal->GetName()
2482  << " Err name: " << error->GetName()
2483  << " HistNumber: " << i_hist << " bin: " << binNumber
2484  << " Value: " << histValue << " Error: " << histError
2485  << std::endl;
2486  */
2487 
2488  if( histError != histError ) {
2489  std::cout << "Warning: In histogram " << error->GetName()
2490  << " bin error for bin " << binNumber
2491  << " is NAN. Not using error!!"
2492  << std::endl;
2493  throw hf_exc();
2494  //histError = 0;
2495  }
2496 
2497  TotalBinContent.at(i_bins) += histValue;
2498  HistErrorsSqr.at(i_bins) += histError*histError; // Add in quadrature
2499 
2500  }
2501  }
2502 
2503  binNumber = 0;
2504 
2505  // Creat the output histogram
2506  TH1* ErrorHist = (TH1*) HistTemplate->Clone( Name.c_str() );
2507  ErrorHist->Reset();
2508 
2509  // Fill the output histogram
2510  for( Int_t i = 0; i < numBins; ++i) {
2511 
2512  // Int_t binNumber = i + 1;
2513  binNumber++;
2514  while( ErrorHist->IsBinUnderflow(binNumber) || ErrorHist->IsBinOverflow(binNumber) ){
2515  binNumber++;
2516  }
2517 
2518  Double_t ErrorsSqr = HistErrorsSqr.at(i);
2519  Double_t TotalVal = TotalBinContent.at(i);
2520 
2521  if( TotalVal <= 0 ) {
2522  std::cout << "Warning: Sum of histograms for bin: " << binNumber
2523  << " is <= 0. Setting error to 0"
2524  << std::endl;
2525 
2526  ErrorHist->SetBinContent( binNumber, 0.0 );
2527  continue;
2528  }
2529 
2530  Double_t RelativeError = sqrt(ErrorsSqr) / TotalVal;
2531 
2532  // If we otherwise get a NAN
2533  // it's an error
2534  if( RelativeError != RelativeError ) {
2535  std::cout << "Error: bin " << i << " error is NAN" << std::endl;
2536  std::cout << " HistErrorsSqr: " << ErrorsSqr
2537  << " TotalVal: " << TotalVal
2538  << std::endl;
2539  throw hf_exc();
2540  }
2541 
2542  // 0th entry in vector is
2543  // the 1st bin in TH1
2544  // (we ignore underflow)
2545 
2546  ErrorHist->SetBinContent( binNumber, RelativeError );
2547 
2548  std::cout << "Making Total Uncertainty for bin " << binNumber
2549  << " Error = " << sqrt(ErrorsSqr)
2550  << " Val = " << TotalVal
2551  << " RelativeError = " << RelativeError
2552  << std::endl;
2553 
2554  }
2555 
2556  return ErrorHist;
2557 
2558 }
2559 
2560 
2561 
2563  createStatConstraintTerms( RooWorkspace* proto, vector<string>& constraintTermNames,
2564  ParamHistFunc& paramHist, TH1* uncertHist,
2565  Constraint::Type type, Double_t minSigma ) {
2566 
2567 
2568  // Take a RooArgList of RooAbsReal's and
2569  // create N constraint terms (one for
2570  // each gamma) whose relative uncertainty
2571  // is the value of the ith RooAbsReal
2572  //
2573  // The integer "type" controls the type
2574  // of constraint term:
2575  //
2576  // type == 0 : NONE
2577  // type == 1 : Gaussian
2578  // type == 2 : Poisson
2579  // type == 3 : LogNormal
2580 
2581  RooArgList ConstraintTerms;
2582 
2583  RooArgList paramSet = paramHist.paramList();
2584 
2585  // Must get the full size of the TH1
2586  // (No direct method to do this...)
2587  Int_t numBins = uncertHist->GetNbinsX()*uncertHist->GetNbinsY()*uncertHist->GetNbinsZ();
2588  Int_t numParams = paramSet.getSize();
2589  // Int_t numBins = uncertHist->GetNbinsX()*uncertHist->GetNbinsY()*uncertHist->GetNbinsZ();
2590 
2591  // Check that there are N elements
2592  // in the RooArgList
2593  if( numBins != numParams ) {
2594  std::cout << "Error: In createStatConstraintTerms, encountered bad number of bins" << std::endl;
2595  std::cout << "Given histogram with " << numBins << " bins,"
2596  << " but require exactly " << numParams << std::endl;
2597  throw hf_exc();
2598  }
2599 
2600  Int_t TH1BinNumber = 0;
2601  for( Int_t i = 0; i < paramSet.getSize(); ++i) {
2602 
2603  TH1BinNumber++;
2604 
2605  while( uncertHist->IsBinUnderflow(TH1BinNumber) || uncertHist->IsBinOverflow(TH1BinNumber) ){
2606  TH1BinNumber++;
2607  }
2608 
2609  RooRealVar& gamma = (RooRealVar&) (paramSet[i]);
2610 
2611  std::cout << "Creating constraint for: " << gamma.GetName()
2612  << ". Type of constraint: " << type << std::endl;
2613 
2614  // Get the sigma from the hist
2615  // (the relative uncertainty)
2616  Double_t sigma = uncertHist->GetBinContent( TH1BinNumber );
2617 
2618  // If the sigma is <= 0,
2619  // do cont create the term
2620  if( sigma <= 0 ){
2621  std::cout << "Not creating constraint term for "
2622  << gamma.GetName()
2623  << " because sigma = " << sigma
2624  << " (sigma<=0)"
2625  << " (TH1 bin number = " << TH1BinNumber << ")"
2626  << std::endl;
2627  gamma.setConstant(kTRUE);
2628  continue;
2629  }
2630 
2631  // set reasonable ranges for gamma parameters
2632  gamma.setMax( 1 + 5*sigma );
2633  // gamma.setMin( TMath::Max(1. - 5*sigma, 0.) );
2634  gamma.setMin( 0. );
2635 
2636  // Make Constraint Term
2637  std::string constrName = string(gamma.GetName()) + "_constraint";
2638  std::string nomName = string("nom_") + gamma.GetName();
2639  std::string sigmaName = string(gamma.GetName()) + "_sigma";
2640  std::string poisMeanName = string(gamma.GetName()) + "_poisMean";
2641 
2642  if( type == Constraint::Gaussian ) {
2643 
2644  // Type 1 : RooGaussian
2645 
2646  // Make sigma
2647 
2648  RooConstVar constrSigma( sigmaName.c_str(), sigmaName.c_str(), sigma );
2649  //proto->import( constrSigma, RecycleConflictNodes() );
2650  //proto->import( constrSigma );
2651 
2652  // Make "observed" value
2653  RooRealVar constrNom(nomName.c_str(), nomName.c_str(), 1.0,0,10);
2654  constrNom.setConstant( true );
2655 
2656  // Make the constraint:
2657  RooGaussian gauss( constrName.c_str(), constrName.c_str(),
2658  constrNom, gamma, constrSigma );
2659 
2660  proto->import( gauss, RecycleConflictNodes() );
2661  //proto->import( gauss );
2662 
2663  } else if( type == Constraint::Poisson ) {
2664 
2665  Double_t tau = 1/sigma/sigma; // this is correct Poisson equivalent to a Gaussian with mean 1 and stdev sigma
2666 
2667  // Make nominal "observed" value
2668  RooRealVar constrNom(nomName.c_str(), nomName.c_str(), tau);
2669  constrNom.setMin(0);
2670  constrNom.setConstant( true );
2671 
2672  // Make the scaling term
2673  std::string scalingName = string(gamma.GetName()) + "_tau";
2674  RooConstVar poissonScaling( scalingName.c_str(), scalingName.c_str(), tau);
2675 
2676  // Make mean for scaled Poisson
2677  RooProduct constrMean( poisMeanName.c_str(), poisMeanName.c_str(), RooArgSet(gamma, poissonScaling) );
2678  //proto->import( constrSigma, RecycleConflictNodes() );
2679  //proto->import( constrSigma );
2680 
2681  // Type 2 : RooPoisson
2682  RooPoisson pois(constrName.c_str(), constrName.c_str(), constrNom, constrMean);
2683  pois.setNoRounding(true);
2684  proto->import( pois, RecycleConflictNodes() );
2685 
2686  } else {
2687 
2688  std::cout << "Error: Did not recognize Stat Error constraint term type: "
2689  << type << " for : " << paramHist.GetName() << std::endl;
2690  throw hf_exc();
2691  }
2692 
2693  // If the sigma value is less
2694  // than a supplied threshold,
2695  // set the variable to constant
2696  if( sigma < minSigma ) {
2697  std::cout << "Warning: Bin " << i << " = " << sigma
2698  << " and is < " << minSigma
2699  << ". Setting: " << gamma.GetName() << " to constant"
2700  << std::endl;
2701  gamma.setConstant(kTRUE);
2702  }
2703 
2704  constraintTermNames.push_back( constrName );
2705  ConstraintTerms.add( *proto->pdf(constrName.c_str()) );
2706 
2707  // Add the "observed" value to the
2708  // list of global observables:
2709  RooArgSet* globalSet = const_cast<RooArgSet*>(proto->set("globalObservables"));
2710 
2711  RooRealVar* nomVarInWorkspace = proto->var(nomName.c_str());
2712  if( ! globalSet->contains(*nomVarInWorkspace) ) {
2713  globalSet->add( *nomVarInWorkspace );
2714  }
2715 
2716  } // end loop over parameters
2717 
2718  return ConstraintTerms;
2719 
2720 }
2721 
2722 } // namespace RooStats
2723 } // namespace HistFactory
2724 
std::vector< RooStats::HistFactory::Asimov > & GetAsimovDatasets()
Definition: Measurement.h:79
static RooAbsData * GenerateAsimovData(const RooAbsPdf &pdf, const RooArgSet &observables)
generate the asimov data for the observables (not the global ones) need to deal with the case of a si...
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
Bool_t saveSnapshot(const char *name, const char *paramNames)
Save snapshot of values and attributes (including "Constant") of parameters &#39;params&#39; If importValues ...
Poisson pdf.
Definition: RooPoisson.h:19
RooStats::HistFactory::StatError & GetStatError()
Definition: Sample.h:117
TIterator * createIterator(Bool_t dir=kIterForward) const
RooWorkspace * MakeCombinedModel(std::vector< std::string >, std::vector< RooWorkspace *>)
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
Add the specified argument to list.
float xmin
Definition: THbookFile.cxx:93
std::map< std::string, double > & GetNoSyst()
Definition: Measurement.h:124
ModelConfig is a simple class that holds configuration information specifying how a model should be u...
Definition: ModelConfig.h:30
Bool_t IsBinUnderflow(Int_t bin, Int_t axis=0) const
Return true if the bin is underflow.
Definition: TH1.cxx:4923
const RooArgSet * GetObservables() const
get RooArgSet for observables (return NULL if not existing)
Definition: ModelConfig.h:237
void AddMultiVarGaussConstraint(RooWorkspace *proto, std::string prefix, int lowBin, int highBin, std::vector< std::string > &likelihoodTermNames)
const RooArgList & paramList() const
Definition: ParamHistFunc.h:39
virtual Bool_t add(const RooAbsCollection &col, Bool_t silent=kFALSE)
Add a collection of arguments to this collection by calling add() for each element in the source coll...
Definition: RooArgSet.h:86
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
Definition: TStopwatch.cxx:58
std::vector< RooStats::HistFactory::NormFactor > & GetNormFactorList()
Definition: Sample.h:109
void AddPoissonTerms(RooWorkspace *proto, std::string prefix, std::string obsPrefix, std::string expPrefix, int lowBin, int highBin, std::vector< std::string > &likelihoodTermNames)
Bool_t IsBinOverflow(Int_t bin, Int_t axis=0) const
Return true if the bin is overflow.
Definition: TH1.cxx:4891
static void EditSyst(RooWorkspace *proto, const char *pdfNameChar, std::map< std::string, double > gammaSyst, std::map< std::string, double > uniformSyst, std::map< std::string, double > logNormSyst, std::map< std::string, double > noSyst)
void ws()
Definition: ws.C:62
virtual void SetWorkspace(RooWorkspace &ws)
Definition: ModelConfig.h:66
Double_t getVal(const RooArgSet *set=0) const
Definition: RooAbsReal.h:64
void GuessObsAndNuisance(const RooAbsData &data)
guesses Observables and ParametersOfInterest if not already set
Definition: ModelConfig.cxx:50
RooWorkspace * MakeSingleChannelWorkspace(Measurement &measurement, Channel &channel)
virtual Double_t GetBinContent(Int_t bin) const
Return content of bin number bin.
Definition: TH1.cxx:4770
RooDataSet * MergeDataSets(RooWorkspace *combined, std::vector< RooWorkspace *> wspace_vec, std::vector< std::string > channel_names, std::string dataSetName, RooArgList obsList, RooCategory *channelCat)
std::string GetName()
Definition: Data.h:34
void ConfigureWorkspace(RooWorkspace *)
Definition: Asimov.cxx:26
virtual Int_t GetNbinsZ() const
Definition: TH1.h:293
#define R__ASSERT(e)
Definition: TError.h:96
RooCmdArg Conditional(const RooArgSet &pdfSet, const RooArgSet &depSet, Bool_t depsAreCond=kFALSE)
std::vector< std::string > & GetPOIList()
Definition: Measurement.h:51
RooAbsReal * function(const char *name) const
Retrieve function (RooAbsReal) with given name. Note that all RooAbsPdfs are also RooAbsReals...
int Int_t
Definition: RtypesCore.h:41
RooAbsArg * arg(const char *name) const
Return RooAbsArg with given name. A null pointer is returned if none is found.
STL namespace.
Class RooRealSumPdf implements a PDF constructed from a sum of functions:
Definition: RooRealSumPdf.h:24
RooCmdArg RecycleConflictNodes(Bool_t flag=kTRUE)
virtual void SetObservables(const RooArgSet &set)
specify the observables
Definition: ModelConfig.h:134
std::vector< RooStats::HistFactory::HistoSys > & GetHistoSysList()
Definition: Sample.h:111
void setMax(const char *name, Double_t value)
Set maximum of name range to given value.
Definition: RooRealVar.cxx:417
void Reset()
Definition: TCollection.h:252
static void ConfigureWorkspaceForMeasurement(const std::string &ModelName, RooWorkspace *ws_single, Measurement &measurement)
std::string AddNormFactor(RooWorkspace *proto, std::string &channel, std::string &sigmaEpsilon, Sample &sample, bool doRatio)
virtual void Reset(Option_t *option="")
Reset this histogram: contents, errors, etc.
Definition: TH1.cxx:6583
double beta(double x, double y)
Calculates the beta function.
virtual Bool_t setLabel(const char *label, Bool_t printError=kTRUE)
Set value by specifying the name of the desired state If printError is set, a message will be printed...
const char * Name
Definition: TXMLSetup.cxx:66
Bool_t importClassCode(const char *pat="*", Bool_t doReplace=kFALSE)
Inport code of all classes in the workspace that have a class name that matches pattern &#39;pat&#39; and whi...
std::vector< std::string > GetPreprocessFunctions()
void SetFunctionsToPreprocess(std::vector< std::string > lines)
double sqrt(double)
Double_t GetXmin() const
Definition: TAxis.h:133
RooCategory & method1D()
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition: TObject.cxx:128
RooDataSet is a container class to hold N-dimensional binned data.
Definition: RooDataHist.h:40
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString...
Definition: TString.cxx:2286
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t tau
Definition: TRolke.cxx:630
RooHistFunc implements a real-valued function sampled from a multidimensional histogram.
Definition: RooHistFunc.h:29
void ProcessExpectedHisto(TH1 *hist, RooWorkspace *proto, std::string prefix, std::string productPrefix, std::string systTerm)
Plain Gaussian p.d.f.
Definition: RooGaussian.h:25
double pow(double, double)
std::vector< RooStats::HistFactory::Channel > & GetChannels()
Definition: Measurement.h:105
virtual Double_t GetBinCenter(Int_t bin) const
Return center of bin.
Definition: TAxis.cxx:464
virtual void Print(Option_t *options=0) const
Print TNamed name and title.
Definition: RooAbsArg.h:227
virtual void SetPdf(const RooAbsPdf &pdf)
Set the Pdf, add to the the workspace if not already there.
Definition: ModelConfig.h:75
RooConstVar represent a constant real-valued object.
Definition: RooConstVar.h:25
const Double_t sigma
void AddConstraintTerms(RooWorkspace *proto, Measurement &measurement, std::string prefix, std::string interpName, std::vector< OverallSys > &systList, std::vector< std::string > &likelihoodTermNames, std::vector< std::string > &totSystTermNames)
std::string GetName()
Definition: Sample.h:82
virtual void Print(Option_t *options=0) const
This method must be overridden when a class wants to print itself.
static RooArgList createParamSet(RooWorkspace &w, const std::string &, const RooArgList &Vars)
std::map< std::string, double > & GetUniformSyst()
Definition: Measurement.h:122
static constexpr double second
RooNumIntConfig * specialIntegratorConfig() const
Returns the specialized integrator configuration for this RooAbsReal.
std::string GetName()
Definition: Asimov.h:32
void LinInterpWithConstraint(RooWorkspace *proto, TH1 *nominal, std::vector< HistoSys >, std::string prefix, std::string productPrefix, std::string systTerm, std::vector< std::string > &likelihoodTermNames)
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:36
RooAbsData * data(const char *name) const
Retrieve dataset (binned or unbinned) with given name. A null pointer is returned if not found...
virtual void setVal(Double_t value)
Set value of variable to &#39;value&#39;.
Definition: RooRealVar.cxx:204
#define alpha_High
double gamma(double x)
Int_t getSize() const
Class to manage histogram axis.
Definition: TAxis.h:30
void setConstant(Bool_t value=kTRUE)
void setMin(const char *name, Double_t value)
Set minimum of name range to given value.
Definition: RooRealVar.cxx:387
Bool_t defineSet(const char *name, const RooArgSet &aset, Bool_t importMissing=kFALSE)
Define a named RooArgSet with given constituents.
TObject * Next()
Definition: TCollection.h:249
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:8514
std::vector< RooStats::HistFactory::ShapeSys > & GetShapeSysList()
Definition: Sample.h:114
virtual Bool_t Multiply(TF1 *f1, Double_t c1=1)
Performs the operation:
Definition: TH1.cxx:5517
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:880
char * Form(const char *fmt,...)
TAxis * GetYaxis()
Definition: TH1.h:316
RooAbsData is the common abstract base class for binned and unbinned datasets.
Definition: RooAbsData.h:37
float xmax
Definition: THbookFile.cxx:93
Bool_t loadSnapshot(const char *name)
Load the values and attributes of the parameters in the snapshot saved with the given name...
void setNoRounding(bool flag=kTRUE)
Definition: RooPoisson.h:33
RooCmdArg Rename(const char *suffix)
RooArgSet allVars() const
Return set with all variable objects.
void SetObsToExpected(RooWorkspace *proto, std::string obsPrefix, std::string expPrefix, int lowBin, int highBin)
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:29
RooCategory represents a fundamental (non-derived) discrete value object.
Definition: RooCategory.h:24
std::vector< RooStats::HistFactory::OverallSys > & GetOverallSysList()
Definition: Sample.h:108
RooProduct a RooAbsReal implementation that represent the product of a given set of other RooAbsReal ...
Definition: RooProduct.h:32
virtual void add(const RooArgSet &row, Double_t weight=1.0, Double_t weightError=0)
Add a data point, with its coordinates specified in the &#39;data&#39; argset, to the data set...
TObject * obj(const char *name) const
Return any type of object (RooAbsArg, RooAbsData or generic object) with given name) ...
std::vector< std::string > & GetConstantParams()
Definition: Measurement.h:60
RooCmdArg Import(const char *state, TH1 &histo)
virtual void SetName(const char *name)
Change the name of this histogram.
Definition: TH1.cxx:8254
RooWorkspace * MakeSingleChannelModel(Measurement &measurement, Channel &channel)
Multivariate Gaussian p.d.f.
RooCmdArg Index(RooCategory &icat)
static constexpr double gauss
Namespace for the RooStats classes.
Definition: Asimov.h:20
#define alpha_Low
#define ClassImp(name)
Definition: Rtypes.h:359
HistFactory::StatErrorConfig & GetStatErrorConfig()
Definition: Channel.h:67
Constraint::Type GetConstraintType()
Definition: Systematics.h:242
RooAbsPdf * pdf(const char *name) const
Retrieve p.d.f (RooAbsPdf) with given name. A null pointer is returned if not found.
double Double_t
Definition: RtypesCore.h:55
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:53
Bool_t removeSet(const char *name)
Remove a named set from the workspace.
void append(RooDataSet &data)
Add all data points of given data set to this data set.
int type
Definition: TGX11.cxx:120
RooCmdArg WeightVar(const char *name, Bool_t reinterpretAsWeight=kFALSE)
std::map< std::string, double > & GetLogNormSyst()
Definition: Measurement.h:123
The TH1 histogram class.
Definition: TH1.h:56
std::vector< RooStats::HistFactory::Sample > & GetSamples()
Definition: Channel.h:71
static void PrintCovarianceMatrix(RooFitResult *result, RooArgSet *params, std::string filename)
RooRealVar * var(const char *name) const
Retrieve real-valued variable (RooRealVar) with given name. A null pointer is returned if not found...
std::vector< RooStats::HistFactory::Data > & GetAdditionalData()
Definition: Channel.h:60
TAxis * GetZaxis()
Definition: TH1.h:317
RooFactoryWSTool & factory()
Return instance to factory tool.
TH1 * MakeScaledUncertaintyHist(const std::string &Name, std::vector< std::pair< TH1 *, TH1 *> > HistVec)
RooAbsPdf is the abstract interface for all probability density functions The class provides hybrid a...
Definition: RooAbsPdf.h:41
Bool_t import(const RooAbsArg &arg, const RooCmdArg &arg1=RooCmdArg(), const RooCmdArg &arg2=RooCmdArg(), const RooCmdArg &arg3=RooCmdArg(), const RooCmdArg &arg4=RooCmdArg(), const RooCmdArg &arg5=RooCmdArg(), const RooCmdArg &arg6=RooCmdArg(), const RooCmdArg &arg7=RooCmdArg(), const RooCmdArg &arg8=RooCmdArg(), const RooCmdArg &arg9=RooCmdArg())
Import a RooAbsArg object, e.g.
const RooArgSet * set(const char *name)
Return pointer to previously defined named set with given nmame If no such set is found a null pointe...
TObject * Clone(const char *newname=0) const
Make a complete copy of the underlying object.
Definition: TH1.cxx:2657
void setPositiveDefinite(bool flag=true)
const char * proto
Definition: civetweb.c:15049
Definition: tree.py:1
virtual void SetParametersOfInterest(const RooArgSet &set)
Definition: ModelConfig.h:93
Bool_t contains(const RooAbsArg &var) const
virtual Int_t GetNbinsX() const
Definition: TH1.h:291
Int_t GetNbins() const
Definition: TAxis.h:121
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
Definition: RooAbsArg.h:66
virtual void SetGlobalObservables(const RooArgSet &set)
specify the global observables
Definition: ModelConfig.h:160
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:357
double exp(double)
TH1 * MakeAbsolUncertaintyHist(const std::string &Name, const TH1 *Hist)
void Print(Option_t *opts=0) const
Print contents of the workspace.
RooStats::HistFactory::Data & GetData()
Definition: Channel.h:55
RooSimultaneous facilitates simultaneous fitting of multiple PDFs to subsets of a given dataset...
const Bool_t kTRUE
Definition: RtypesCore.h:87
Double_t GetXmax() const
Definition: TAxis.h:134
void ConfigureHistFactoryDataset(RooDataSet *obsData, TH1 *nominal, RooWorkspace *proto, std::vector< std::string > obsNameVec)
void MakeTotalExpected(RooWorkspace *proto, std::string totName, std::vector< std::string > &syst_x_expectedPrefixNames, std::vector< std::string > &normByNames)
RooArgList createStatConstraintTerms(RooWorkspace *proto, std::vector< std::string > &constraintTerms, ParamHistFunc &paramHist, TH1 *uncertHist, Constraint::Type type, Double_t minSigma)
Double_t correlation(const RooAbsArg &par1, const RooAbsArg &par2) const
Definition: RooFitResult.h:117
char name[80]
Definition: TGX11.cxx:109
std::vector< RooStats::HistFactory::ShapeFactor > & GetShapeFactorList()
Definition: Sample.h:115
Bool_t isConstant() const
Definition: RooAbsArg.h:266
TAxis * GetXaxis()
Get the behaviour adopted by the object about the statoverflows. See EStatOverflows for more informat...
Definition: TH1.h:315
The RooWorkspace is a persistable container for RooFit projects.
Definition: RooWorkspace.h:43
virtual Int_t GetNbinsY() const
Definition: TH1.h:292
std::map< std::string, double > & GetGammaSyst()
Definition: Measurement.h:121
virtual Double_t GetBinError(Int_t bin) const
Return value of error associated to bin number bin.
Definition: TH1.cxx:8356
Stopwatch class.
Definition: TStopwatch.h:28