00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 #include <string>
00069 #include <vector>
00070 #include <map>
00071 #include <iostream>
00072 #include <sstream>
00073
00074
00075 #include "TFile.h"
00076 #include "TH1F.h"
00077 #include "TDOMParser.h"
00078 #include "TXMLAttr.h"
00079 #include "TString.h"
00080
00081
00082 #include "RooStats/ModelConfig.h"
00083
00084
00085 #include "Helper.h"
00086 #include "ConfigParser.h"
00087 #include "RooStats/HistFactory/EstimateSummary.h"
00088 #include "RooStats/HistFactory/HistoToWorkspaceFactoryFast.h"
00089
00090
00091 using namespace RooFit;
00092 using namespace RooStats;
00093 using namespace HistFactory;
00094
00095
00096
00097 void fastDriver(string input ){
00098
00099
00100
00101
00102
00103 TDOMParser xmlparser;
00104 Int_t parseError = xmlparser.ParseFile( input.c_str() );
00105 if( parseError ) {
00106 std::cerr << "Loading of xml document \"" << input
00107 << "\" failed" << std::endl;
00108 }
00109
00110 cout << "reading input : " << input << endl;
00111 TXMLDocument* xmldoc = xmlparser.GetXMLDocument();
00112 TXMLNode* rootNode = xmldoc->GetRootNode();
00113
00114 if( rootNode->GetNodeName() == TString( "Combination" ) ){
00115 string outputFileName, outputFileNamePrefix;
00116 vector<string> xml_input;
00117
00118 TListIter attribIt = rootNode->GetAttributes();
00119 TXMLAttr* curAttr = 0;
00120 while( ( curAttr = dynamic_cast< TXMLAttr* >( attribIt() ) ) != 0 ) {
00121 if( curAttr->GetName() == TString( "OutputFilePrefix" ) ) {
00122 outputFileNamePrefix=string(curAttr->GetValue());
00123 cout << "output file is : " << outputFileName << endl;
00124 }
00125 }
00126 TXMLNode* node = rootNode->GetChildren();
00127 while( node != 0 ) {
00128 if( node->GetNodeName() == TString( "Input" ) ) {
00129 xml_input.push_back(node->GetText());
00130 }
00131 node = node->GetNextNode();
00132 }
00133 node = rootNode->GetChildren();
00134 while( node != 0 ) {
00135 if( node->GetNodeName() == TString( "Measurement" ) ) {
00136
00137 Double_t nominalLumi=0, lumiRelError=0, lumiError=0;
00138 Int_t lowBin=0, highBin=0;
00139 string rowTitle, POI, mode;
00140 vector<string> systToFix;
00141 map<string,double> gammaSyst;
00142 map<string,double> uniformSyst;
00143 map<string,double> logNormSyst;
00144 bool exportOnly = false;
00145
00146
00147
00148 attribIt = node->GetAttributes();
00149 curAttr = 0;
00150 while( ( curAttr = dynamic_cast< TXMLAttr* >( attribIt() ) ) != 0 ) {
00151 if( curAttr->GetName() == TString( "Lumi" ) ) {
00152 nominalLumi=atof(curAttr->GetValue());
00153 }
00154 if( curAttr->GetName() == TString( "LumiRelErr" ) ) {
00155 lumiRelError=atof(curAttr->GetValue());
00156 }
00157 if( curAttr->GetName() == TString( "BinLow" ) ) {
00158 lowBin=atoi(curAttr->GetValue());
00159 }
00160 if( curAttr->GetName() == TString( "BinHigh" ) ) {
00161 highBin=atoi(curAttr->GetValue());
00162 }
00163 if( curAttr->GetName() == TString( "Name" ) ) {
00164 rowTitle=curAttr->GetValue();
00165 outputFileName=outputFileNamePrefix+"_"+rowTitle+".root";
00166 }
00167 if( curAttr->GetName() == TString( "Mode" ) ) {
00168 mode=curAttr->GetValue();
00169 }
00170 if( curAttr->GetName() == TString( "ExportOnly" ) ) {
00171 if(curAttr->GetValue() == TString( "True" ) )
00172 exportOnly = true;
00173 else
00174 exportOnly = false;
00175 }
00176 }
00177 lumiError=nominalLumi*lumiRelError;
00178
00179 TXMLNode* mnode = node->GetChildren();
00180 while( mnode != 0 ) {
00181 if( mnode->GetNodeName() == TString( "POI" ) ) {
00182 POI=mnode->GetText();
00183 }
00184 if( mnode->GetNodeName() == TString( "ParamSetting" ) ) {
00185
00186
00187 attribIt = mnode->GetAttributes();
00188 curAttr = 0;
00189 while( ( curAttr = dynamic_cast< TXMLAttr* >( attribIt() ) ) != 0 ) {
00190 if( curAttr->GetName() == TString( "Const" ) ) {
00191 if(curAttr->GetValue()==TString("True")){
00192 AddSubStrings(systToFix, mnode->GetText());
00193 }
00194 }
00195 }
00196 }
00197 if( mnode->GetNodeName() == TString( "ConstraintTerm" ) ) {
00198 vector<string> syst; string type = ""; double rel = 0;
00199 AddSubStrings(syst,mnode->GetText());
00200
00201
00202 attribIt = mnode->GetAttributes();
00203 curAttr = 0;
00204 while( ( curAttr = dynamic_cast< TXMLAttr* >( attribIt() ) ) != 0 ) {
00205 if( curAttr->GetName() == TString( "Type" ) ) {
00206 type = curAttr->GetValue();
00207 }
00208 if( curAttr->GetName() == TString( "RelativeUncertainty" ) ) {
00209 rel = atof(curAttr->GetValue());
00210 }
00211 }
00212 if (type=="Gamma" && rel!=0) {
00213 for (vector<string>::const_iterator it=syst.begin(); it!=syst.end(); it++) gammaSyst[(*it).c_str()] = rel;
00214 }
00215 if (type=="Uniform" && rel!=0) {
00216 for (vector<string>::const_iterator it=syst.begin(); it!=syst.end(); it++) uniformSyst[(*it).c_str()] = rel;
00217 }
00218 if (type=="LogNormal" && rel!=0) {
00219 for (vector<string>::const_iterator it=syst.begin(); it!=syst.end(); it++) logNormSyst[(*it).c_str()] = rel;
00220 }
00221 }
00222 mnode = mnode->GetNextNode();
00223 }
00224
00225
00226 cout << "using lumi = " << nominalLumi << " and lumiError = " << lumiError
00227 << " including bins between " << lowBin << " and " << highBin << endl;
00228 cout << "fixing the following parameters:" << endl;
00229 for(vector<string>::iterator itr=systToFix.begin(); itr!=systToFix.end(); ++itr){
00230 cout << " " << *itr << endl;
00231 }
00232
00233
00234
00235
00236
00237
00238 vector<vector<EstimateSummary> > summaries;
00239 if(xml_input.empty()){
00240 cerr << "no input channels found" << endl;
00241 exit(1);
00242 }
00243
00244
00245 vector<RooWorkspace*> chs;
00246 vector<string> ch_names;
00247 TFile* outFile = new TFile(outputFileName.c_str(), "recreate");
00248 HistoToWorkspaceFactoryFast factory(outputFileNamePrefix, rowTitle, systToFix, nominalLumi, lumiError, lowBin, highBin , outFile);
00249
00250
00251
00252 fprintf(factory.pFile, " %s &", rowTitle.c_str() );
00253
00254
00255 for(vector<string>::iterator itr=xml_input.begin(); itr!=xml_input.end(); ++itr){
00256 vector<EstimateSummary> oneChannel;
00257
00258 ReadXmlConfig(*itr, oneChannel, nominalLumi);
00259
00260 summaries.push_back(oneChannel);
00261
00262 string ch_name=oneChannel[0].channel;
00263 ch_names.push_back(ch_name);
00264 RooWorkspace * ws = factory.MakeSingleChannelModel(oneChannel, systToFix);
00265 chs.push_back(ws);
00266
00267 ModelConfig * proto_config = (ModelConfig *) ws->obj("ModelConfig");
00268 cout << "Setting Parameter of Interest as :" << POI << endl;
00269 RooRealVar* poi = (RooRealVar*) ws->var(POI.c_str());
00270 RooArgSet * params= new RooArgSet;
00271 params->add(*poi);
00272 proto_config->SetParameters(*params);
00273
00274
00275
00276
00277 if(gammaSyst.size()>0 || uniformSyst.size()>0 || logNormSyst.size()>0) {
00278 factory.EditSyst(ws,("model_"+oneChannel[0].channel).c_str(),gammaSyst,uniformSyst,logNormSyst);
00279 proto_config->SetPdf(*ws->pdf("newSimPdf"));
00280 }
00281
00282
00283 RooAbsData* expData = ws->data("asimovData");
00284 proto_config->GuessObsAndNuisance(*expData);
00285 ws->writeToFile((outputFileNamePrefix+"_"+ch_name+"_"+rowTitle+"_model.root").c_str());
00286
00287
00288 if(!exportOnly){
00289 if(ws->data("obsData")){
00290 factory.FitModel(ws, ch_name, "newSimPdf", "obsData", false);
00291 } else {
00292 factory.FitModel(ws, ch_name, "newSimPdf", "asimovData", false);
00293 }
00294
00295 }
00296 fprintf(factory.pFile, " & " );
00297 }
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308 if(mode.find("comb")!=string::npos){
00309 RooWorkspace* ws=factory.MakeCombinedModel(ch_names,chs);
00310
00311
00312 if(gammaSyst.size()>0 || uniformSyst.size()>0 || logNormSyst.size()>0)
00313 factory.EditSyst(ws,"simPdf",gammaSyst,uniformSyst,logNormSyst);
00314
00315
00316
00317 ModelConfig * combined_config = (ModelConfig *) ws->obj("ModelConfig");
00318 cout << "Setting Parameter of Interest as :" << POI << endl;
00319 RooRealVar* poi = (RooRealVar*) ws->var((POI).c_str());
00320
00321 RooArgSet * params= new RooArgSet;
00322 cout << poi << endl;
00323 params->add(*poi);
00324 combined_config->SetParameters(*params);
00325 ws->Print();
00326
00327
00328 if(gammaSyst.size()>0 || uniformSyst.size()>0 || logNormSyst.size()>0)
00329 combined_config->SetPdf(*ws->pdf("newSimPdf"));
00330
00331 RooAbsData* simData = ws->data("asimovData");
00332 combined_config->GuessObsAndNuisance(*simData);
00333
00334 ws->writeToFile((outputFileNamePrefix+"_combined_"+rowTitle+"_model.root").c_str());
00335
00336
00337
00338 if(!exportOnly){
00339 if(ws->data("obsData")){
00340 factory.FitModel(ws, "combined", "simPdf", "obsData", false);
00341 } else {
00342 factory.FitModel(ws, "combined", "simPdf", "asimovData", false);
00343 }
00344 }
00345 }
00346
00347
00348 fprintf(factory.pFile, " \\\\ \n");
00349
00350 outFile->Close();
00351 delete outFile;
00352
00353 }
00354 node = node->GetNextNode();
00355 }
00356 }
00357 }
00358
00359
00360