73 #define alpha_Low "-5"
74 #define alpha_High "5"
75 #define NoHistConst_Low "0"
76 #define NoHistConst_High "2000"
79 using namespace RooFit ;
80 using namespace RooStats ;
84 ClassImp(RooStats::HistFactory::HistoToWorkspaceFactory);
87 namespace HistFactory{
89 HistoToWorkspaceFactory::HistoToWorkspaceFactory() :
99 HistoToWorkspaceFactory::~HistoToWorkspaceFactory(){
103 HistoToWorkspaceFactory::HistoToWorkspaceFactory(
string filePrefix,
string row, vector<string> syst,
double nomL,
double lumiE,
int low,
int high, TFile* f):
104 fFileNamePrefix(filePrefix),
114 fResultsPrefixStr<<
"_" << fRowTitle;
115 while(fRowTitle.find(
"\\ ")!=string::npos){
116 int pos=fRowTitle.find(
"\\ ");
117 fRowTitle.replace(pos, 1,
"");
119 pFile = fopen ((filePrefix+
"_results.table").c_str(),
"a");
124 string HistoToWorkspaceFactory::FilePrefixStr(
string prefix){
127 ss << prefix <<
"_" << fNomLumi<<
"_" << fLumiError<<
"_" << fLowBin<<
"_" << fHighBin<<
"_"<<fRowTitle;
132 void HistoToWorkspaceFactory::ProcessExpectedHisto(TH1* hist,RooWorkspace* proto,
string prefix,
string productPrefix,
string systTerm,
double low,
double high,
int lowBin,
int highBin){
134 cout <<
"processing hist " << hist->GetName() << endl;
136 cout <<
"hist is empty" << endl;
137 RooArgSet argset(prefix.c_str());
138 string highStr =
"inf";
139 for(Int_t i=lowBin; i<highBin; ++i){
140 std::stringstream str;
141 std::stringstream range;
144 range<<
"["<<hist->GetBinContent(i+1) <<
"," << low <<
"," << highStr <<
"]";
146 range<<
"["<< low <<
"," << high <<
"]";
147 cout <<
"for bin N"+str.str() <<
" var " << prefix+str.str()+
" with range " << range.str() << endl;
148 RooRealVar* var = (RooRealVar*) proto->factory((prefix+str.str()+range.str()).c_str());
151 if(! (productPrefix.empty() || systTerm.empty()) )
152 proto->factory((
"prod:"+productPrefix+str.str()+
"("+prefix+str.str()+
","+systTerm+
")").c_str() );
157 proto->defineSet(prefix.c_str(),argset);
161 void HistoToWorkspaceFactory::AddMultiVarGaussConstraint(RooWorkspace* proto,
string prefix,
int lowBin,
int highBin, vector<string>& likelihoodTermNames){
164 TVectorD mean(highBin-lowBin);
166 for(Int_t i=lowBin; i<highBin; ++i){
167 std::stringstream str;
169 RooRealVar* temp = proto->var((prefix+str.str()).c_str());
170 mean(i) = temp->getVal();
173 TMatrixDSym Cov(highBin-lowBin);
174 for(
int i=lowBin; i<highBin; ++i){
175 for(
int j=0; j<highBin-lowBin; ++j){
177 Cov(i,j) = sqrt(mean(i));
183 RooArgList floating( *(proto->set(prefix.c_str() ) ) );
184 RooMultiVarGaussian constraint((prefix+
"Constraint").c_str(),
"",
185 floating, mean, Cov);
187 proto->import(constraint);
189 likelihoodTermNames.push_back(constraint.GetName());
194 void HistoToWorkspaceFactory::LinInterpWithConstraint(RooWorkspace* proto, TH1* nominal, vector<TH1*> lowHist, vector<TH1*> highHist,
195 vector<string> sourceName,
string prefix,
string productPrefix,
string systTerm,
196 int lowBin,
int highBin, vector<string>& likelihoodTermNames){
201 RooArgList params( (
"alpha_Hist") );
203 string range=string(
"[")+alpha_Low+
","+alpha_High+
"]";
204 for(
unsigned int j=0; j<lowHist.size(); ++j){
205 std::stringstream str;
208 RooRealVar* temp = (RooRealVar*) proto->var((
"alpha_"+sourceName.at(j)).c_str());
210 temp = (RooRealVar*) proto->factory((
"alpha_"+sourceName.at(j)+range).c_str());
213 string command=(
"Gaussian::alpha_"+sourceName.at(j)+
"Constraint(alpha_"+sourceName.at(j)+
",nom_"+sourceName.at(j)+
"[0.,-10,10],1.)");
214 cout << command << endl;
215 likelihoodTermNames.push_back( proto->factory( command.c_str() )->GetName() );
216 proto->var((
"nom_"+sourceName.at(j)).c_str())->setConstant();
217 const_cast<RooArgSet*
>(proto->set(
"globalObservables"))->add(*proto->var((
"nom_"+sourceName.at(j)).c_str()));
226 for(Int_t i=lowBin; i<highBin; ++i){
227 std::stringstream str;
231 vector<double> low, high;
232 for(
unsigned int j=0; j<lowHist.size(); ++j){
233 low.push_back( lowHist.at(j)->GetBinContent(i+1) );
234 high.push_back( highHist.at(j)->GetBinContent(i+1) );
235 cout <<
"for "+prefix+
" bin "+str.str()+
" creating linear interp of nominal " << nominal->GetBinContent(i+1)
236 <<
" in parameter " << sourceName.at(j)
237 <<
" between " << low.back() <<
" - " << high.back()
238 <<
" about " << 100.*fabs(low.back() - high.back() )/nominal->GetBinContent(i+1) <<
" % error"
243 LinInterpVar interp( (prefix+str.str()).c_str(),
"", params, nominal->GetBinContent(i+1), low, high);
246 proto->import(interp);
249 proto->factory((
"prod:"+productPrefix+str.str()+
"("+prefix+str.str()+
","+systTerm+
")").c_str() );
255 string HistoToWorkspaceFactory::AddNormFactor(RooWorkspace * proto,
string & channel,
string & sigmaEpsilon, EstimateSummary & es,
bool doRatio){
256 string overallNorm_times_sigmaEpsilon ;
258 vector<EstimateSummary::NormFactor> norm=es.normFactor;
260 for(vector<EstimateSummary::NormFactor>::iterator itr=norm.begin(); itr!=norm.end(); ++itr){
261 cout <<
"making normFactor: " << itr->name << endl;
263 std::stringstream range;
264 range<<
"["<<itr->val<<
","<<itr->low<<
","<<itr->high<<
"]";
268 if(!prodNames.empty()) prodNames+=
",";
270 varname=itr->name+
"_"+channel;
275 proto->factory((varname+range.str()).c_str());
279 cout <<
"WARNING: Const attribute to <NormFactor> tag is deprecated, will ignore."<<
280 " Instead, add \n\t<ParamSetting Const=\"True\">"<<varname<<
"</ParamSetting>\n"<<
281 " to your top-level XML's <Measurment> entry"<< endl;
285 overallNorm_times_sigmaEpsilon = es.name+
"_"+channel+
"_overallNorm_x_sigma_epsilon";
286 proto->factory((
"prod::"+overallNorm_times_sigmaEpsilon+
"("+prodNames+
","+sigmaEpsilon+
")").c_str());
289 if(!overallNorm_times_sigmaEpsilon.empty())
290 return overallNorm_times_sigmaEpsilon;
296 void HistoToWorkspaceFactory::AddEfficiencyTerms(RooWorkspace* proto,
string prefix,
string interpName,
297 map<
string,pair<double,double> > systMap,
298 vector<string>& likelihoodTermNames, vector<string>& totSystTermNames){
302 string range=string(
"[0,")+alpha_Low+
","+alpha_High+
"]";
304 totSystTermNames.push_back(prefix);
306 RooArgSet params(prefix.c_str());
307 vector<double> lowVec, highVec;
308 for(map<
string,pair<double,double> >::iterator it=systMap.begin(); it!=systMap.end(); ++it){
310 RooRealVar* temp = (RooRealVar*) proto->var((prefix+ it->first).c_str());
312 temp = (RooRealVar*) proto->factory((prefix+ it->first +range).c_str());
314 string command=(
"Gaussian::"+prefix+it->first+
"Constraint("+prefix+it->first+
",nom_"+prefix+it->first+
"[0.,-10,10],1.)");
315 cout << command << endl;
316 likelihoodTermNames.push_back( proto->factory( command.c_str() )->GetName() );
317 proto->var((
"nom_"+prefix+it->first).c_str())->setConstant();
318 const_cast<RooArgSet*
>(proto->set(
"globalObservables"))->add(*proto->var((
"nom_"+prefix+it->first).c_str()));
324 std::stringstream lowhigh;
325 double low = it->second.first;
326 double high = it->second.second;
327 lowVec.push_back(low);
328 highVec.push_back(high);
331 if(systMap.size()>0){
333 LinInterpVar interp( (interpName).c_str(),
"", params, 1., lowVec, highVec);
334 proto->import(interp);
338 RooConstVar interp( (interpName).c_str(),
"", 1.);
339 proto->import(interp);
345 void HistoToWorkspaceFactory::MakeTotalExpected(RooWorkspace* proto,
string totName,
string ,
string ,
346 int lowBin,
int highBin, vector<string>& syst_x_expectedPrefixNames,
347 vector<string>& normByNames){
351 for(Int_t i=lowBin; i<highBin; ++i){
352 std::stringstream str;
354 string command=
"sum::"+totName+str.str()+
"(";
357 for(
unsigned int j=0; j<syst_x_expectedPrefixNames.size();++j){
358 command+=prepend+normByNames.at(j)+
"*"+syst_x_expectedPrefixNames.at(j)+str.str();
362 cout <<
"function to calculate total: " << command << endl;
363 proto->factory(command.c_str());
367 void HistoToWorkspaceFactory::AddPoissonTerms(RooWorkspace* proto,
string prefix,
string obsPrefix,
string expPrefix,
int lowBin,
int highBin,
368 vector<string>& likelihoodTermNames){
372 RooArgSet Pois(prefix.c_str());
373 for(Int_t i=lowBin; i<highBin; ++i){
374 std::stringstream str;
377 string command(
"Poisson::"+prefix+str.str()+
"("+obsPrefix+str.str()+
","+expPrefix+str.str()+
",1)");
378 RooAbsArg* temp = (proto->factory( command.c_str() ) );
381 cout <<
"Poisson Term " << command << endl;
382 ((RooAbsPdf*) temp)->setEvalErrorLoggingMode(RooAbsReal::PrintErrors);
385 likelihoodTermNames.push_back( temp->GetName() );
388 proto->defineSet(prefix.c_str(),Pois);
391 void HistoToWorkspaceFactory::SetObsToExpected(RooWorkspace* proto,
string obsPrefix,
string expPrefix,
int lowBin,
int highBin){
394 TTree* tree =
new TTree();
395 Double_t* obsForTree =
new Double_t[highBin-lowBin];
396 RooArgList obsList(
"obsList");
398 for(Int_t i=lowBin; i<highBin; ++i){
399 std::stringstream str;
401 RooRealVar* obs = (RooRealVar*) proto->var((obsPrefix+str.str()).c_str());
402 cout <<
"expected number of events called: " << expPrefix << endl;
403 RooAbsReal* exp = proto->function((expPrefix+str.str()).c_str());
407 obs->setVal( exp->getVal() );
408 cout <<
"setting obs"+str.str()+
" to expected = " << exp->getVal() <<
" check: " << obs->getVal() << endl;
411 obsForTree[i] = exp->getVal();
412 tree->Branch((obsPrefix+str.str()).c_str(), obsForTree+i ,(obsPrefix+str.str()+
"/D").c_str());
415 cout <<
"problem retrieving obs or exp " << obsPrefix+str.str() << obs <<
" " << expPrefix+str.str() << exp << endl;
419 RooDataSet* data =
new RooDataSet(
"expData",
"", tree, obsList);
421 proto->import(*data);
423 obsForTree =
nullptr;
426 void HistoToWorkspaceFactory::Customize(RooWorkspace* proto,
const char* pdfNameChar, map<string,string> renameMap) {
427 cout <<
"in customizations" << endl;
428 string pdfName(pdfNameChar);
429 map<string,string>::iterator it;
430 string edit=
"EDIT::customized("+pdfName+
",";
432 for(it=renameMap.begin(); it!=renameMap.end(); ++it) {
433 cout << it->first +
"=" + it->second << endl;
434 edit+=precede + it->first +
"=" + it->second;
439 proto->factory( edit.c_str() );
445 void HistoToWorkspaceFactory::EditSyst(RooWorkspace* proto,
const char* pdfNameChar, map<string,double> gammaSyst, map<string,double> uniformSyst,map<string,double> logNormSyst) {
446 string pdfName(pdfNameChar);
448 ModelConfig * combined_config = (ModelConfig *) proto->obj(
"ModelConfig");
451 string edit=
"EDIT::newSimPdf("+pdfName+
",";
453 string lastPdf=pdfName;
455 unsigned int numReplacements = 0;
456 unsigned int nskipped = 0;
457 map<string,double>::iterator it;
460 for(it=gammaSyst.begin(); it!=gammaSyst.end(); ++it) {
462 if(! proto->var((
"alpha_"+it->first).c_str())){
469 double relativeUncertainty = it->second;
470 double scale = 1/sqrt((1+1/pow(relativeUncertainty,2)));
473 proto->factory(Form(
"beta_%s[1,0,10]",it->first.c_str()));
474 proto->factory(Form(
"y_%s[%f]",it->first.c_str(),1./pow(relativeUncertainty,2))) ;
475 proto->factory(Form(
"theta_%s[%f]",it->first.c_str(),pow(relativeUncertainty,2))) ;
476 proto->factory(Form(
"Gamma::beta_%sConstraint(beta_%s,sum::k_%s(y_%s,one[1]),theta_%s,zero[0])",
481 it->first.c_str())) ;
497 proto->factory(Form(
"PolyVar::alphaOfBeta_%s(beta_%s,{%f,%f})",it->first.c_str(),it->first.c_str(),-1./scale,1./scale));
500 if(proto->var(Form(
"alpha_%s",it->first.c_str()))->isConstant())
501 proto->var(Form(
"beta_%s",it->first.c_str()))->setConstant(
true);
503 proto->var(Form(
"beta_%s",it->first.c_str()))->setConstant(
false);
509 editList+=precede +
"alpha_"+it->first+
"Constraint=beta_" + it->first+
"Constraint";
512 editList+=precede +
"alpha_"+it->first+
"=alphaOfBeta_"+ it->first;
522 if(numReplacements%10 == 0 && numReplacements+nskipped!=gammaSyst.size()){
523 edit=
"EDIT::"+lastPdf+
"_("+lastPdf+
","+editList+
")";
527 cout <<
"Going to issue this edit command\n" << edit<< endl;
528 proto->factory( edit.c_str() );
529 RooAbsPdf* newOne = proto->pdf(lastPdf.c_str());
531 cout <<
"\n\n ---------------------\n WARNING: failed to make EDIT\n\n" << endl;
537 for(it=uniformSyst.begin(); it!=uniformSyst.end(); ++it) {
538 cout <<
"edit for " << it->first <<
"with rel uncert = " << it->second << endl;
539 if(! proto->var((
"alpha_"+it->first).c_str())){
540 cout <<
"systematic not there" << endl;
547 proto->factory(Form(
"beta_%s[1,0,10]",it->first.c_str()));
548 proto->factory(Form(
"Uniform::beta_%sConstraint(beta_%s)",it->first.c_str(),it->first.c_str()));
549 proto->factory(Form(
"PolyVar::alphaOfBeta_%s(beta_%s,{-1,1})",it->first.c_str(),it->first.c_str()));
552 if(proto->var(Form(
"alpha_%s",it->first.c_str()))->isConstant())
553 proto->var(Form(
"beta_%s",it->first.c_str()))->setConstant(
true);
555 proto->var(Form(
"beta_%s",it->first.c_str()))->setConstant(
false);
560 cout <<
"alpha_"+it->first+
"Constraint=beta_" + it->first+
"Constraint" << endl;
561 editList+=precede +
"alpha_"+it->first+
"Constraint=beta_" + it->first+
"Constraint";
563 cout <<
"alpha_"+it->first+
"=alphaOfBeta_"+ it->first << endl;
564 editList+=precede +
"alpha_"+it->first+
"=alphaOfBeta_"+ it->first;
566 if( proto->pdf((
"alpha_"+it->first+
"Constraint").c_str()) && proto->var((
"alpha_"+it->first).c_str()) )
567 cout <<
" checked they are there" << proto->pdf((
"alpha_"+it->first+
"Constraint").c_str()) <<
" " << proto->var((
"alpha_"+it->first).c_str()) << endl;
569 cout <<
"NOT THERE" << endl;
572 if(numReplacements%10 == 0 && numReplacements+nskipped!=gammaSyst.size()){
573 edit=
"EDIT::"+lastPdf+
"_("+lastPdf+
","+editList+
")";
578 proto->factory( edit.c_str() );
579 RooAbsPdf* newOne = proto->pdf(lastPdf.c_str());
581 cout <<
"\n\n ---------------------\n WARNING: failed to make EDIT\n\n" << endl;
591 for(it=logNormSyst.begin(); it!=logNormSyst.end(); ++it) {
592 cout <<
"edit for " << it->first <<
"with rel uncert = " << it->second << endl;
593 if(! proto->var((
"alpha_"+it->first).c_str())){
594 cout <<
"systematic not there" << endl;
600 double relativeUncertainty = it->second;
601 double kappa = 1+relativeUncertainty;
605 double scale = relativeUncertainty;
609 proto->factory(Form(
"beta_%s[1,0,10]",it->first.c_str()));
610 proto->factory(Form(
"kappa_%s[%f]",it->first.c_str(),kappa));
611 proto->factory(Form(
"Lognormal::beta_%sConstraint(beta_%s,one[1],kappa_%s)",
614 it->first.c_str())) ;
615 proto->factory(Form(
"PolyVar::alphaOfBeta_%s(beta_%s,{%f,%f})",it->first.c_str(),it->first.c_str(),-1./scale,1./scale));
619 if(proto->var(Form(
"alpha_%s",it->first.c_str()))->isConstant())
620 proto->var(Form(
"beta_%s",it->first.c_str()))->setConstant(
true);
622 proto->var(Form(
"beta_%s",it->first.c_str()))->setConstant(
false);
627 cout <<
"alpha_"+it->first+
"Constraint=beta_" + it->first+
"Constraint" << endl;
628 editList+=precede +
"alpha_"+it->first+
"Constraint=beta_" + it->first+
"Constraint";
630 cout <<
"alpha_"+it->first+
"=alphaOfBeta_"+ it->first << endl;
631 editList+=precede +
"alpha_"+it->first+
"=alphaOfBeta_"+ it->first;
633 if( proto->pdf((
"alpha_"+it->first+
"Constraint").c_str()) && proto->var((
"alpha_"+it->first).c_str()) )
634 cout <<
" checked they are there" << proto->pdf((
"alpha_"+it->first+
"Constraint").c_str()) <<
" " << proto->var((
"alpha_"+it->first).c_str()) << endl;
636 cout <<
"NOT THERE" << endl;
639 if(numReplacements%10 == 0 && numReplacements+nskipped!=gammaSyst.size()){
640 edit=
"EDIT::"+lastPdf+
"_("+lastPdf+
","+editList+
")";
645 proto->factory( edit.c_str() );
646 RooAbsPdf* newOne = proto->pdf(lastPdf.c_str());
648 cout <<
"\n\n ---------------------\n WARNING: failed to make EDIT\n\n" << endl;
657 edit=
"EDIT::newSimPdf("+lastPdf+
","+editList+
")";
659 proto->factory( edit.c_str() );
661 RooAbsPdf* newOne = proto->pdf(
"newSimPdf");
664 combined_config->SetPdf(*newOne);
667 cout <<
"\n\n ---------------------\n WARNING: failed to make EDIT\n\n" << endl;
671 void HistoToWorkspaceFactory::PrintCovarianceMatrix(RooFitResult* result, RooArgSet* params,
string filename){
673 pFile = fopen ((filename).c_str(),
"w");
676 TIter iti = params->createIterator();
677 TIter itj = params->createIterator();
678 RooRealVar *myargi, *myargj;
680 while ((myargi = (RooRealVar *)iti.Next())) {
681 if(myargi->isConstant())
continue;
682 fprintf(pFile,
" & %s", myargi->GetName());
684 fprintf(pFile,
"\\\\ \\hline \n" );
686 while ((myargi = (RooRealVar *)iti.Next())) {
687 if(myargi->isConstant())
continue;
688 fprintf(pFile,
"%s", myargi->GetName());
690 while ((myargj = (RooRealVar *)itj.Next())) {
691 if(myargj->isConstant())
continue;
692 cout << myargi->GetName() <<
"," << myargj->GetName();
693 fprintf(pFile,
" & %.2f", result->correlation(*myargi, *myargj));
696 fprintf(pFile,
" \\\\\n");
704 RooWorkspace* HistoToWorkspaceFactory::MakeSingleChannelModel(vector<EstimateSummary> summary, vector<string> systToFix,
bool doRatio)
707 if (summary.empty() ) {
708 Error(
"MakeSingleChannelModel",
"vector of EstimateSummry is empty - return a nullptr");
715 string channel=summary[0].channel;
716 cout <<
"\n\n-------------------\nStarting to process " << channel <<
" channel" << endl;
721 RooWorkspace* proto =
new RooWorkspace(
"proto",
"proto workspace");
722 ModelConfig * proto_config =
new ModelConfig(
"ModelConfig", proto);
723 proto_config->SetWorkspace(*proto);
725 RooArgSet likelihoodTerms(
"likelihoodTerms");
726 vector<string> likelihoodTermNames, totSystTermNames,syst_x_expectedPrefixNames, normalizationNames;
728 string prefix, range;
733 if (summary.at(0).name==
"Data") {
734 ProcessExpectedHisto(summary.at(0).nominal,proto,
"obsN",
"",
"",0,100000,fLowBin,fHighBin);
736 cout <<
"Will use expected (\"Asimov\") data set" << endl;
737 ProcessExpectedHisto(NULL,proto,
"obsN",
"",
"",0,100000,fLowBin,fHighBin);
745 std::stringstream lumiStr;
747 lumiStr<<
"["<<fNomLumi<<
",0,"<<10.*fNomLumi<<
"]";
748 proto->factory((
"Lumi"+lumiStr.str()).c_str());
749 cout <<
"lumi str = " << lumiStr.str() << endl;
751 std::stringstream lumiErrorStr;
753 lumiErrorStr <<
"nominalLumi["<<fNomLumi <<
",0,"<<fNomLumi+10*fLumiError<<
"]," << fLumiError ;
754 proto->factory((
"Gaussian::lumiConstraint(Lumi,"+lumiErrorStr.str()+
")").c_str());
755 proto->var(
"nominalLumi")->setConstant();
756 proto->defineSet(
"globalObservables",
"nominalLumi");
757 likelihoodTermNames.push_back(
"lumiConstraint");
758 cout <<
"lumi Error str = " << lumiErrorStr.str() << endl;
764 vector<EstimateSummary>::iterator it = summary.begin();
765 for(; it!=summary.end(); ++it){
766 if(it->name==
"Data")
continue;
768 string overallSystName = it->name+
"_"+it->channel+
"_epsilon";
769 string systSourcePrefix =
"alpha_";
770 AddEfficiencyTerms(proto,systSourcePrefix, overallSystName,
772 likelihoodTermNames, totSystTermNames);
774 overallSystName=AddNormFactor(proto, channel, overallSystName, *it, doRatio);
776 TH1* nominal = it->nominal;
777 if(it->lowHists.size() == 0){
778 cout << it->name+
"_"+it->channel+
" has no variation histograms " <<endl;
779 string expPrefix=it->name+
"_"+it->channel+
"_expN";
780 string syst_x_expectedPrefix=it->name+
"_"+it->channel+
"_overallSyst_x_Exp";
781 ProcessExpectedHisto(nominal,proto,expPrefix,syst_x_expectedPrefix,overallSystName,atoi(NoHistConst_Low),atoi(NoHistConst_High),fLowBin,fHighBin);
782 syst_x_expectedPrefixNames.push_back(syst_x_expectedPrefix);
783 }
else if(it->lowHists.size() != it->highHists.size()){
784 cout <<
"problem in "+it->name+
"_"+it->channel
785 <<
" number of low & high variation histograms don't match" << endl;
788 string constraintPrefix = it->name+
"_"+it->channel+
"_Hist_alpha";
789 string syst_x_expectedPrefix = it->name+
"_"+it->channel+
"_overallSyst_x_HistSyst";
790 LinInterpWithConstraint(proto, nominal, it->lowHists, it->highHists, it->systSourceForHist,
791 constraintPrefix, syst_x_expectedPrefix, overallSystName,
792 fLowBin, fHighBin, likelihoodTermNames);
793 syst_x_expectedPrefixNames.push_back(syst_x_expectedPrefix);
799 normalizationNames.push_back(
"Lumi" );
801 normalizationNames.push_back( it->normName);
807 MakeTotalExpected(proto,channel+
"_totN",channel+
"_expN",
"Lumi",fLowBin,fHighBin,
808 syst_x_expectedPrefixNames, normalizationNames);
812 AddPoissonTerms(proto,
"Pois_"+channel,
"obsN", channel+
"_totN", fLowBin, fHighBin, likelihoodTermNames);
816 if(summary.at(0).name!=
"Data"){
817 SetObsToExpected(proto,
"obsN",channel+
"_totN", fLowBin, fHighBin);
818 cout <<
" using asimov data" << endl;
820 SetObsToExpected(proto,
"obsN",
"obsN", fLowBin, fHighBin);
821 cout <<
" using input data histogram" << endl;
826 for(
unsigned int i=0; i<systToFix.size(); ++i){
827 RooRealVar* temp = proto->var((systToFix.at(i)).c_str());
828 if(temp) temp->setConstant();
829 else cout <<
"could not find variable " << systToFix.at(i) <<
" could not set it to constant" << endl;
834 for(
unsigned int i=0; i<likelihoodTermNames.size(); ++i){
836 likelihoodTerms.add(* (proto->arg(likelihoodTermNames[i].c_str())) );
840 proto->defineSet(
"likelihoodTerms",likelihoodTerms);
843 cout <<
"-----------------------------------------"<<endl;
844 cout <<
"import model into workspace" << endl;
845 RooProdPdf* model =
new RooProdPdf((
"model_"+channel).c_str(),
846 "product of Poissons accross bins for a single channel",
848 proto->import(*model,RecycleConflictNodes());
850 proto_config->SetPdf(*model);
851 proto_config->SetGlobalObservables(*proto->set(
"globalObservables"));
853 proto->import(*proto_config,proto_config->GetName());
854 proto->importClassCode();
860 RooWorkspace* HistoToWorkspaceFactory::MakeCombinedModel(vector<string> ch_names, vector<RooWorkspace*> chs)
876 if (ch_names.empty() || chs.empty() ) {
877 Error(
"MakeCombinedModel",
"Input vectors are empty - return a nullptr");
880 if (chs.size() < ch_names.size() ) {
881 Error(
"MakeCombinedModel",
"Input vector of workspace has an invalid size - return a nullptr");
885 map<string, RooAbsPdf*> pdfMap;
886 vector<RooAbsPdf*> models;
890 for(
unsigned int i = 0; i< ch_names.size(); ++i){
891 string channel_name=ch_names[i];
893 if (ss.str().empty()) ss << channel_name ;
894 else ss <<
',' << channel_name ;
895 RooWorkspace * ch=chs[i];
897 RooAbsPdf* model = ch->pdf((
"model_"+channel_name).c_str());
898 models.push_back(model);
899 globalObs.add(*ch->set(
"globalObservables"));
902 pdfMap[channel_name]=model;
906 cout <<
"\n\n------------------\n Entering combination" << endl;
907 RooWorkspace* combined =
new RooWorkspace(
"combined");
909 RooCategory* channelCat = (RooCategory*) combined->factory((
"channelCat["+ss.str()+
"]").c_str());
910 RooSimultaneous * simPdf=
new RooSimultaneous(
"simPdf",
"",pdfMap, *channelCat);
911 ModelConfig * combined_config =
new ModelConfig(
"ModelConfig", combined);
912 combined_config->SetWorkspace(*combined);
914 combined->import(globalObs);
915 combined->defineSet(
"globalObservables",globalObs);
916 combined_config->SetGlobalObservables(*combined->set(
"globalObservables"));
920 cout <<
"-----------------------------------------"<<endl;
921 cout <<
"create toy data for " << ss.str() << endl;
923 const RooArgSet* obsN = chs[0]->set(
"obsN");
925 RooDataSet * simData=
new RooDataSet(
"simData",
"master dataset", *obsN,
926 Index(*channelCat), Import(ch_names[0].c_str(),*((RooDataSet*)chs[0]->data(
"expData"))));
927 for(
unsigned int i = 1; i< ch_names.size(); ++i){
928 RooDataSet * simData_ch=
new RooDataSet(
"simData",
"master dataset", *obsN,
929 Index(*channelCat), Import(ch_names[i].c_str(),*((RooDataSet*)chs[i]->data(
"expData"))));
930 simData->append(*simData_ch);
935 combined->import(*simData,RecycleConflictNodes());
937 cout <<
"\n\n----------------\n Importing combined model" << endl;
938 combined->import(*simPdf,RecycleConflictNodes());
940 cout <<
"check pointer " << simPdf << endl;
942 for(
unsigned int i=0; i<fSystToFix.size(); ++i){
944 RooRealVar* temp = combined->var((fSystToFix.at(i)).c_str());
947 cout <<
"setting " << fSystToFix.at(i) <<
" constant" << endl;
949 else cout <<
"could not find variable " << fSystToFix.at(i) <<
" could not set it to constant" << endl;
957 combined_config->SetPdf(*simPdf);
959 combined->import(*combined_config,combined_config->GetName());
960 combined->importClassCode();
967 void HistoToWorkspaceFactory::FitModel(RooWorkspace * combined,
string channel,
string ,
string data_name,
bool )
970 ModelConfig * combined_config = (ModelConfig *) combined->obj(
"ModelConfig");
971 RooDataSet * simData = (RooDataSet *) combined->obj(data_name.c_str());
973 const RooArgSet * POIs=combined_config->GetParametersOfInterest();
990 RooAbsPdf* model=combined_config->GetPdf();
996 cout <<
"\n\n---------------" << endl;
997 cout <<
"---------------- Doing "<< channel <<
" Fit" << endl;
998 cout <<
"---------------\n\n" << endl;
1000 model->fitTo(*simData, Minos(kTRUE), PrintLevel(1));
1006 RooRealVar* poi = 0;
1008 TIterator* params_itr=POIs->createIterator();
1009 TObject* params_obj=0;
1010 while((params_obj=params_itr->Next())){
1011 poi = (RooRealVar*) params_obj;
1012 cout <<
"printing results for " << poi->GetName() <<
" at " << poi->getVal()<<
" high " << poi->getErrorLo() <<
" low " << poi->getErrorHi()<<endl;
1014 fprintf(pFile,
" %.4f / %.4f ", poi->getErrorLo(), poi->getErrorHi());
1016 RooAbsReal* nll = model->createNLL(*simData);
1017 RooAbsReal* profile = nll->createProfile(*poi);
1018 RooPlot* frame = poi->frame();
1019 FormatFrameForLikelihood(frame);
1020 TCanvas* c1 =
new TCanvas( channel.c_str(),
"",800,600);
1021 nll->plotOn(frame, ShiftToZero(), LineColor(kRed), LineStyle(kDashed));
1022 profile->plotOn(frame);
1023 frame->SetMinimum(0);
1024 frame->SetMaximum(2.);
1027 c1->SaveAs( (fFileNamePrefix+
"_"+channel+
"_"+fRowTitle+
"_profileLR.eps").c_str() );
1029 fOut_f->mkdir(channel.c_str())->mkdir(
"Summary")->cd();
1049 RooCurve* curve=frame->getCurve();
1050 Int_t curve_N=curve->GetN();
1051 Double_t* curve_x=curve->GetX();
1052 delete frame;
delete c1;
1066 Double_t * x_arr =
new Double_t[curve_N];
1067 Double_t * y_arr_nll =
new Double_t[curve_N];
1071 for(
int i=0; i<curve_N; i++){
1072 double f=curve_x[i];
1075 y_arr_nll[i]=nll->getVal();
1077 TGraph * g =
new TGraph(curve_N, x_arr, y_arr_nll);
1078 g->SetName((FilePrefixStr(channel)+
"_nll").c_str());
1082 delete [] y_arr_nll;
1090 void HistoToWorkspaceFactory::FormatFrameForLikelihood(RooPlot* frame,
string ,
string YTitle){
1092 gStyle->SetCanvasBorderMode(0);
1093 gStyle->SetPadBorderMode(0);
1094 gStyle->SetPadColor(0);
1095 gStyle->SetCanvasColor(255);
1096 gStyle->SetTitleFillColor(255);
1097 gStyle->SetFrameFillColor(0);
1098 gStyle->SetStatColor(255);
1100 RooAbsRealLValue* var = frame->getPlotVar();
1101 double xmin = var->getMin();
1102 double xmax = var->getMax();
1104 frame->SetTitle(
"");
1106 frame->GetXaxis()->SetTitle(var->GetTitle());
1107 frame->GetYaxis()->SetTitle(YTitle.c_str());
1108 frame->SetMaximum(2.);
1109 frame->SetMinimum(0.);
1110 TLine * line =
new TLine(xmin,.5,xmax,.5);
1111 line->SetLineColor(kGreen);
1112 TLine * line90 =
new TLine(xmin,2.71/2.,xmax,2.71/2.);
1113 line90->SetLineColor(kGreen);
1114 TLine * line95 =
new TLine(xmin,3.84/2.,xmax,3.84/2.);
1115 line95->SetLineColor(kGreen);
1116 frame->addObject(line);
1117 frame->addObject(line90);
1118 frame->addObject(line95);
1121 TDirectory * HistoToWorkspaceFactory::Makedirs( TDirectory * file, vector<string> names ){
1122 if(! file)
return file;
1125 for(vector<string>::iterator itr=names.begin(); itr != names.end(); ++itr){
1126 if( ! path.empty() ) path+=
"/";
1128 ptr=file->GetDirectory(path.c_str());
1129 if( ! ptr ) ptr=file->mkdir((*itr).c_str());
1130 file=file->GetDirectory(path.c_str());
1134 TDirectory * HistoToWorkspaceFactory::Mkdir( TDirectory * file,
string name ){
1135 if(! file)
return file;
1137 ptr=file->GetDirectory(name.c_str());
1138 if( ! ptr ) ptr=file->mkdir(name.c_str());