67 #define alpha_Low "-5"
68 #define alpha_High "5"
69 #define NoHistConst_Low "0"
70 #define NoHistConst_High "2000"
83 HistoToWorkspaceFactory::HistoToWorkspaceFactory() :
98 fFileNamePrefix(filePrefix),
109 while(
fRowTitle.find(
"\\ ")!=string::npos){
113 pFile = fopen ((filePrefix+
"_results.table").c_str(),
"a");
128 cout <<
"processing hist " << hist->
GetName() << endl;
130 cout <<
"hist is empty" << endl;
132 string highStr =
"inf";
133 for(
Int_t i=lowBin; i<highBin; ++i){
134 std::stringstream str;
135 std::stringstream range;
138 range<<
"["<<hist->
GetBinContent(i+1) <<
"," << low <<
"," << highStr <<
"]";
140 range<<
"["<< low <<
"," << high <<
"]";
141 cout <<
"for bin N"+str.str() <<
" var " << prefix+str.str()+
" with range " << range.str() << endl;
145 if(! (productPrefix.empty() || systTerm.empty()) )
146 proto->factory((
"prod:"+productPrefix+str.str()+
"("+prefix+str.str()+
","+systTerm+
")").c_str() );
151 proto->defineSet(prefix.c_str(),argset);
160 for(
Int_t i=lowBin; i<highBin; ++i){
161 std::stringstream str;
168 for(
int i=lowBin; i<highBin; ++i){
169 for(
int j=0; j<highBin-lowBin; ++j){
171 Cov(i,j) =
sqrt(mean(i));
179 floating, mean, Cov);
181 proto->import(constraint);
183 likelihoodTermNames.push_back(constraint.
GetName());
189 vector<string> sourceName,
string prefix,
string productPrefix,
string systTerm,
190 int lowBin,
int highBin, vector<string>& likelihoodTermNames){
198 for(
unsigned int j=0; j<lowHist.size(); ++j){
199 std::stringstream str;
204 temp = (
RooRealVar*)
proto->factory((
"alpha_"+sourceName.at(j)+range).c_str());
207 string command=(
"Gaussian::alpha_"+sourceName.at(j)+
"Constraint(alpha_"+sourceName.at(j)+
",nom_"+sourceName.at(j)+
"[0.,-10,10],1.)");
208 cout << command << endl;
209 likelihoodTermNames.push_back(
proto->factory( command.c_str() )->
GetName() );
210 proto->var((
"nom_"+sourceName.at(j)).c_str())->setConstant();
211 const_cast<RooArgSet*
>(
proto->set(
"globalObservables"))->add(*
proto->var((
"nom_"+sourceName.at(j)).c_str()));
220 for(
Int_t i=lowBin; i<highBin; ++i){
221 std::stringstream str;
225 vector<double> low, high;
226 for(
unsigned int j=0; j<lowHist.size(); ++j){
227 low.push_back( lowHist.at(j)->GetBinContent(i+1) );
228 high.push_back( highHist.at(j)->GetBinContent(i+1) );
229 cout <<
"for "+prefix+
" bin "+str.str()+
" creating linear interp of nominal " << nominal->
GetBinContent(i+1)
230 <<
" in parameter " << sourceName.at(j)
231 <<
" between " << low.back() <<
" - " << high.back()
232 <<
" about " << 100.*
fabs(low.back() - high.back() )/nominal->
GetBinContent(i+1) <<
" % error"
240 proto->import(interp);
243 proto->factory((
"prod:"+productPrefix+str.str()+
"("+prefix+str.str()+
","+systTerm+
")").c_str() );
250 string overallNorm_times_sigmaEpsilon ;
252 vector<EstimateSummary::NormFactor> norm=es.
normFactor;
254 for(vector<EstimateSummary::NormFactor>::iterator itr=norm.begin(); itr!=norm.end(); ++itr){
255 cout <<
"making normFactor: " << itr->name << endl;
257 std::stringstream range;
258 range<<
"["<<itr->val<<
","<<itr->low<<
","<<itr->high<<
"]";
262 if(!prodNames.empty()) prodNames+=
",";
264 varname=itr->name+
"_"+channel;
269 proto->factory((varname+range.str()).c_str());
273 cout <<
"WARNING: Const attribute to <NormFactor> tag is deprecated, will ignore."<<
274 " Instead, add \n\t<ParamSetting Const=\"True\">"<<varname<<
"</ParamSetting>\n"<<
275 " to your top-level XML's <Measurment> entry"<< endl;
279 overallNorm_times_sigmaEpsilon = es.
name+
"_"+channel+
"_overallNorm_x_sigma_epsilon";
280 proto->factory((
"prod::"+overallNorm_times_sigmaEpsilon+
"("+prodNames+
","+sigmaEpsilon+
")").c_str());
283 if(!overallNorm_times_sigmaEpsilon.empty())
284 return overallNorm_times_sigmaEpsilon;
291 map<
string,pair<double,double> > systMap,
292 vector<string>& likelihoodTermNames, vector<string>& totSystTermNames){
298 totSystTermNames.push_back(prefix);
301 vector<double> lowVec, highVec;
302 for(map<
string,pair<double,double> >::iterator it=systMap.begin(); it!=systMap.end(); ++it){
306 temp = (
RooRealVar*)
proto->factory((prefix+ it->first +range).c_str());
308 string command=(
"Gaussian::"+prefix+it->first+
"Constraint("+prefix+it->first+
",nom_"+prefix+it->first+
"[0.,-10,10],1.)");
309 cout << command << endl;
310 likelihoodTermNames.push_back(
proto->factory( command.c_str() )->
GetName() );
311 proto->var((
"nom_"+prefix+it->first).c_str())->setConstant();
312 const_cast<RooArgSet*
>(
proto->set(
"globalObservables"))->add(*
proto->var((
"nom_"+prefix+it->first).c_str()));
318 std::stringstream lowhigh;
319 double low = it->second.first;
320 double high = it->second.second;
321 lowVec.push_back(low);
322 highVec.push_back(high);
325 if(systMap.size()>0){
327 LinInterpVar interp( (interpName).c_str(),
"", params, 1., lowVec, highVec);
328 proto->import(interp);
333 proto->import(interp);
340 int lowBin,
int highBin, vector<string>& syst_x_expectedPrefixNames,
341 vector<string>& normByNames){
345 for(
Int_t i=lowBin; i<highBin; ++i){
346 std::stringstream str;
348 string command=
"sum::"+totName+str.str()+
"(";
351 for(
unsigned int j=0; j<syst_x_expectedPrefixNames.size();++j){
352 command+=prepend+normByNames.at(j)+
"*"+syst_x_expectedPrefixNames.at(j)+str.str();
356 cout <<
"function to calculate total: " << command << endl;
357 proto->factory(command.c_str());
362 vector<string>& likelihoodTermNames){
367 for(
Int_t i=lowBin; i<highBin; ++i){
368 std::stringstream str;
371 string command(
"Poisson::"+prefix+str.str()+
"("+obsPrefix+str.str()+
","+expPrefix+str.str()+
",1)");
375 cout <<
"Poisson Term " << command << endl;
379 likelihoodTermNames.push_back( temp->
GetName() );
382 proto->defineSet(prefix.c_str(),Pois);
392 for(
Int_t i=lowBin; i<highBin; ++i){
393 std::stringstream str;
396 cout <<
"expected number of events called: " << expPrefix << endl;
402 cout <<
"setting obs"+str.str()+
" to expected = " <<
exp->getVal() <<
" check: " << obs->
getVal() << endl;
405 obsForTree[i] =
exp->getVal();
406 tree->Branch((obsPrefix+str.str()).c_str(), obsForTree+i ,(obsPrefix+str.str()+
"/D").c_str());
409 cout <<
"problem retrieving obs or exp " << obsPrefix+str.str() << obs <<
" " << expPrefix+str.str() <<
exp << endl;
415 proto->import(*data);
417 obsForTree =
nullptr;
421 cout <<
"in customizations" << endl;
422 string pdfName(pdfNameChar);
423 map<string,string>::iterator it;
424 string edit=
"EDIT::customized("+pdfName+
",";
426 for(it=renameMap.begin(); it!=renameMap.end(); ++it) {
427 cout << it->first +
"=" + it->second << endl;
428 edit+=precede + it->first +
"=" + it->second;
433 proto->factory( edit.c_str() );
440 string pdfName(pdfNameChar);
445 string edit=
"EDIT::newSimPdf("+pdfName+
",";
447 string lastPdf=pdfName;
449 unsigned int numReplacements = 0;
450 unsigned int nskipped = 0;
451 map<string,double>::iterator it;
454 for(it=gammaSyst.begin(); it!=gammaSyst.end(); ++it) {
456 if(!
proto->var((
"alpha_"+it->first).c_str())){
463 double relativeUncertainty = it->second;
464 double scale = 1/
sqrt((1+1/
pow(relativeUncertainty,2)));
467 proto->factory(
Form(
"beta_%s[1,0,10]",it->first.c_str()));
468 proto->factory(
Form(
"y_%s[%f]",it->first.c_str(),1./
pow(relativeUncertainty,2))) ;
469 proto->factory(
Form(
"theta_%s[%f]",it->first.c_str(),
pow(relativeUncertainty,2))) ;
470 proto->factory(
Form(
"Gamma::beta_%sConstraint(beta_%s,sum::k_%s(y_%s,one[1]),theta_%s,zero[0])",
475 it->first.c_str())) ;
491 proto->factory(
Form(
"PolyVar::alphaOfBeta_%s(beta_%s,{%f,%f})",it->first.c_str(),it->first.c_str(),-1./scale,1./scale));
494 if(
proto->var(
Form(
"alpha_%s",it->first.c_str()))->isConstant())
495 proto->var(
Form(
"beta_%s",it->first.c_str()))->setConstant(
true);
497 proto->var(
Form(
"beta_%s",it->first.c_str()))->setConstant(
false);
503 editList+=precede +
"alpha_"+it->first+
"Constraint=beta_" + it->first+
"Constraint";
506 editList+=precede +
"alpha_"+it->first+
"=alphaOfBeta_"+ it->first;
516 if(numReplacements%10 == 0 && numReplacements+nskipped!=gammaSyst.size()){
517 edit=
"EDIT::"+lastPdf+
"_("+lastPdf+
","+editList+
")";
521 cout <<
"Going to issue this edit command\n" << edit<< endl;
522 proto->factory( edit.c_str() );
525 cout <<
"\n\n ---------------------\n WARNING: failed to make EDIT\n\n" << endl;
531 for(it=uniformSyst.begin(); it!=uniformSyst.end(); ++it) {
532 cout <<
"edit for " << it->first <<
"with rel uncert = " << it->second << endl;
533 if(!
proto->var((
"alpha_"+it->first).c_str())){
534 cout <<
"systematic not there" << endl;
541 proto->factory(
Form(
"beta_%s[1,0,10]",it->first.c_str()));
542 proto->factory(
Form(
"Uniform::beta_%sConstraint(beta_%s)",it->first.c_str(),it->first.c_str()));
543 proto->factory(
Form(
"PolyVar::alphaOfBeta_%s(beta_%s,{-1,1})",it->first.c_str(),it->first.c_str()));
546 if(
proto->var(
Form(
"alpha_%s",it->first.c_str()))->isConstant())
547 proto->var(
Form(
"beta_%s",it->first.c_str()))->setConstant(
true);
549 proto->var(
Form(
"beta_%s",it->first.c_str()))->setConstant(
false);
554 cout <<
"alpha_"+it->first+
"Constraint=beta_" + it->first+
"Constraint" << endl;
555 editList+=precede +
"alpha_"+it->first+
"Constraint=beta_" + it->first+
"Constraint";
557 cout <<
"alpha_"+it->first+
"=alphaOfBeta_"+ it->first << endl;
558 editList+=precede +
"alpha_"+it->first+
"=alphaOfBeta_"+ it->first;
560 if(
proto->pdf((
"alpha_"+it->first+
"Constraint").c_str()) &&
proto->var((
"alpha_"+it->first).c_str()) )
561 cout <<
" checked they are there" <<
proto->pdf((
"alpha_"+it->first+
"Constraint").c_str()) <<
" " <<
proto->var((
"alpha_"+it->first).c_str()) << endl;
563 cout <<
"NOT THERE" << endl;
566 if(numReplacements%10 == 0 && numReplacements+nskipped!=gammaSyst.size()){
567 edit=
"EDIT::"+lastPdf+
"_("+lastPdf+
","+editList+
")";
572 proto->factory( edit.c_str() );
575 cout <<
"\n\n ---------------------\n WARNING: failed to make EDIT\n\n" << endl;
585 for(it=logNormSyst.begin(); it!=logNormSyst.end(); ++it) {
586 cout <<
"edit for " << it->first <<
"with rel uncert = " << it->second << endl;
587 if(!
proto->var((
"alpha_"+it->first).c_str())){
588 cout <<
"systematic not there" << endl;
594 double relativeUncertainty = it->second;
595 double kappa = 1+relativeUncertainty;
599 double scale = relativeUncertainty;
603 proto->factory(
Form(
"beta_%s[1,0,10]",it->first.c_str()));
604 proto->factory(
Form(
"kappa_%s[%f]",it->first.c_str(),kappa));
605 proto->factory(
Form(
"Lognormal::beta_%sConstraint(beta_%s,one[1],kappa_%s)",
608 it->first.c_str())) ;
609 proto->factory(
Form(
"PolyVar::alphaOfBeta_%s(beta_%s,{%f,%f})",it->first.c_str(),it->first.c_str(),-1./scale,1./scale));
613 if(
proto->var(
Form(
"alpha_%s",it->first.c_str()))->isConstant())
614 proto->var(
Form(
"beta_%s",it->first.c_str()))->setConstant(
true);
616 proto->var(
Form(
"beta_%s",it->first.c_str()))->setConstant(
false);
621 cout <<
"alpha_"+it->first+
"Constraint=beta_" + it->first+
"Constraint" << endl;
622 editList+=precede +
"alpha_"+it->first+
"Constraint=beta_" + it->first+
"Constraint";
624 cout <<
"alpha_"+it->first+
"=alphaOfBeta_"+ it->first << endl;
625 editList+=precede +
"alpha_"+it->first+
"=alphaOfBeta_"+ it->first;
627 if(
proto->pdf((
"alpha_"+it->first+
"Constraint").c_str()) &&
proto->var((
"alpha_"+it->first).c_str()) )
628 cout <<
" checked they are there" <<
proto->pdf((
"alpha_"+it->first+
"Constraint").c_str()) <<
" " <<
proto->var((
"alpha_"+it->first).c_str()) << endl;
630 cout <<
"NOT THERE" << endl;
633 if(numReplacements%10 == 0 && numReplacements+nskipped!=gammaSyst.size()){
634 edit=
"EDIT::"+lastPdf+
"_("+lastPdf+
","+editList+
")";
639 proto->factory( edit.c_str() );
642 cout <<
"\n\n ---------------------\n WARNING: failed to make EDIT\n\n" << endl;
651 edit=
"EDIT::newSimPdf("+lastPdf+
","+editList+
")";
653 proto->factory( edit.c_str() );
658 combined_config->
SetPdf(*newOne);
661 cout <<
"\n\n ---------------------\n WARNING: failed to make EDIT\n\n" << endl;
667 pFile = fopen ((filename).c_str(),
"w");
678 fprintf(
pFile,
"\\\\ \\hline \n" );
690 fprintf(
pFile,
" \\\\\n");
701 if (summary.empty() ) {
702 Error(
"MakeSingleChannelModel",
"vector of EstimateSummry is empty - return a nullptr");
709 string channel=summary[0].channel;
710 cout <<
"\n\n-------------------\nStarting to process " << channel <<
" channel" << endl;
719 RooArgSet likelihoodTerms(
"likelihoodTerms");
720 vector<string> likelihoodTermNames, totSystTermNames,syst_x_expectedPrefixNames, normalizationNames;
722 string prefix, range;
727 if (summary.at(0).name==
"Data") {
730 cout <<
"Will use expected (\"Asimov\") data set" << endl;
739 std::stringstream lumiStr;
742 proto->factory((
"Lumi"+lumiStr.str()).c_str());
743 cout <<
"lumi str = " << lumiStr.str() << endl;
745 std::stringstream lumiErrorStr;
748 proto->factory((
"Gaussian::lumiConstraint(Lumi,"+lumiErrorStr.str()+
")").c_str());
749 proto->var(
"nominalLumi")->setConstant();
750 proto->defineSet(
"globalObservables",
"nominalLumi");
751 likelihoodTermNames.push_back(
"lumiConstraint");
752 cout <<
"lumi Error str = " << lumiErrorStr.str() << endl;
758 vector<EstimateSummary>::iterator it = summary.begin();
759 for(; it!=summary.end(); ++it){
760 if(it->name==
"Data")
continue;
762 string overallSystName = it->name+
"_"+it->channel+
"_epsilon";
763 string systSourcePrefix =
"alpha_";
766 likelihoodTermNames, totSystTermNames);
770 TH1* nominal = it->nominal;
771 if(it->lowHists.size() == 0){
772 cout << it->name+
"_"+it->channel+
" has no variation histograms " <<endl;
773 string expPrefix=it->name+
"_"+it->channel+
"_expN";
774 string syst_x_expectedPrefix=it->name+
"_"+it->channel+
"_overallSyst_x_Exp";
776 syst_x_expectedPrefixNames.push_back(syst_x_expectedPrefix);
777 }
else if(it->lowHists.size() != it->highHists.size()){
778 cout <<
"problem in "+it->name+
"_"+it->channel
779 <<
" number of low & high variation histograms don't match" << endl;
782 string constraintPrefix = it->name+
"_"+it->channel+
"_Hist_alpha";
783 string syst_x_expectedPrefix = it->name+
"_"+it->channel+
"_overallSyst_x_HistSyst";
785 constraintPrefix, syst_x_expectedPrefix, overallSystName,
787 syst_x_expectedPrefixNames.push_back(syst_x_expectedPrefix);
793 normalizationNames.push_back(
"Lumi" );
795 normalizationNames.push_back( it->normName);
802 syst_x_expectedPrefixNames, normalizationNames);
810 if(summary.at(0).name!=
"Data"){
812 cout <<
" using asimov data" << endl;
815 cout <<
" using input data histogram" << endl;
820 for(
unsigned int i=0; i<systToFix.size(); ++i){
823 else cout <<
"could not find variable " << systToFix.at(i) <<
" could not set it to constant" << endl;
828 for(
unsigned int i=0; i<likelihoodTermNames.size(); ++i){
830 likelihoodTerms.
add(* (
proto->arg(likelihoodTermNames[i].c_str())) );
834 proto->defineSet(
"likelihoodTerms",likelihoodTerms);
837 cout <<
"-----------------------------------------"<<endl;
838 cout <<
"import model into workspace" << endl;
840 "product of Poissons accross bins for a single channel",
848 proto->importClassCode();
870 if (ch_names.empty() || chs.empty() ) {
871 Error(
"MakeCombinedModel",
"Input vectors are empty - return a nullptr");
874 if (chs.size() < ch_names.size() ) {
875 Error(
"MakeCombinedModel",
"Input vector of workspace has an invalid size - return a nullptr");
879 map<string, RooAbsPdf*> pdfMap;
880 vector<RooAbsPdf*> models;
884 for(
unsigned int i = 0; i< ch_names.size(); ++i){
885 string channel_name=ch_names[i];
887 if (ss.str().empty()) ss << channel_name ;
888 else ss <<
',' << channel_name ;
892 models.push_back(
model);
893 globalObs.
add(*ch->
set(
"globalObservables"));
896 pdfMap[channel_name]=
model;
900 cout <<
"\n\n------------------\n Entering combination" << endl;
908 combined->
import(globalObs);
909 combined->
defineSet(
"globalObservables",globalObs);
914 cout <<
"-----------------------------------------"<<endl;
915 cout <<
"create toy data for " << ss.str() << endl;
917 const RooArgSet* obsN = chs[0]->set(
"obsN");
921 for(
unsigned int i = 1; i< ch_names.size(); ++i){
924 simData->
append(*simData_ch);
931 cout <<
"\n\n----------------\n Importing combined model" << endl;
934 cout <<
"check pointer " << simPdf << endl;
936 for(
unsigned int i=0; i<
fSystToFix.size(); ++i){
941 cout <<
"setting " <<
fSystToFix.at(i) <<
" constant" << endl;
943 else cout <<
"could not find variable " <<
fSystToFix.at(i) <<
" could not set it to constant" << endl;
951 combined_config->
SetPdf(*simPdf);
953 combined->
import(*combined_config,combined_config->
GetName());
990 cout <<
"\n\n---------------" << endl;
991 cout <<
"---------------- Doing "<< channel <<
" Fit" << endl;
992 cout <<
"---------------\n\n" << endl;
1004 while((params_obj=params_itr->
Next())){
1046 delete frame;
delete c1;
1065 for(
int i=0; i<curve_N; i++){
1066 double f=curve_x[i];
1069 y_arr_nll[i]=nll->
getVal();
1076 delete [] y_arr_nll;
1119 for(vector<string>::iterator itr=names.begin(); itr != names.end(); ++itr){
1120 if( ! path.empty() ) path+=
"/";
1122 ptr=
file->GetDirectory(path.c_str());
1123 if( ! ptr ) ptr=
file->mkdir((*itr).c_str());
1124 file=
file->GetDirectory(path.c_str());
1131 ptr=
file->GetDirectory(
name.c_str());
1132 if( ! ptr ) ptr=
file->mkdir(
name.c_str());