32 ClassImp(RooStats::HistFactory::Measurement); ;
35 RooStats::HistFactory::Measurement::Measurement() :
36 fPOI(), fLumi( 1.0 ), fLumiRelErr( .10 ),
37 fBinLow( 0 ), fBinHigh( 1 ), fExportOnly( false )
51 RooStats::HistFactory::Measurement::Measurement(
const char* Name,
const char* Title) :
52 TNamed( Name, Title ),
53 fPOI(), fLumi( 1.0 ), fLumiRelErr( .10 ),
54 fBinLow( 0 ), fBinHigh( 1 ), fExportOnly( false )
68 void RooStats::HistFactory::Measurement::AddConstantParam(
const std::string& param )
72 if( std::find(fConstantParams.begin(), fConstantParams.end(), param) != fConstantParams.end() ) {
73 std::cout <<
"Warning: Setting parameter: " << param
74 <<
" to constant, but it is already listed as constant. "
75 <<
"You may ignore this warning."
80 fConstantParams.push_back( param );
86 void RooStats::HistFactory::Measurement::SetParamValue(
const std::string& param,
double value )
92 if( fParamValues.find(param) != fParamValues.end() ) {
93 std::cout <<
"Warning: Chainging parameter: " << param
94 <<
" value from: " << fParamValues[param]
100 std::cout <<
"Setting parameter: " << param
101 <<
" value to " << value
104 fParamValues[param] = value;
111 void RooStats::HistFactory::Measurement::AddPreprocessFunction( std::string name, std::string expression, std::string dependencies )
115 PreprocessFunction func(name, expression, dependencies);
116 AddFunctionObject(func);
121 std::vector<std::string> RooStats::HistFactory::Measurement::GetPreprocessFunctions()
125 std::vector<std::string> PreprocessFunctionExpressions;
126 for(
unsigned int i = 0; i < fFunctionObjects.size(); ++i ) {
127 std::string expression = fFunctionObjects.at(i).GetCommand();
128 PreprocessFunctionExpressions.push_back( expression );
130 return PreprocessFunctionExpressions;
134 void RooStats::HistFactory::Measurement::AddGammaSyst(std::string syst,
double uncert)
136 fGammaSyst[syst] = uncert;
140 void RooStats::HistFactory::Measurement::AddLogNormSyst(std::string syst,
double uncert)
142 fLogNormSyst[syst] = uncert;
146 void RooStats::HistFactory::Measurement::AddUniformSyst(std::string syst)
148 fUniformSyst[syst] = 1.0;
152 void RooStats::HistFactory::Measurement::AddNoSyst(std::string syst)
158 bool RooStats::HistFactory::Measurement::HasChannel( std::string ChanName )
162 for(
unsigned int i = 0; i < fChannels.size(); ++i ) {
164 Channel& chan = fChannels.at(i);
165 if( chan.GetName() == ChanName ) {
178 RooStats::HistFactory::Channel& RooStats::HistFactory::Measurement::GetChannel( std::string ChanName )
180 for(
unsigned int i = 0; i < fChannels.size(); ++i ) {
182 Channel& chan = fChannels.at(i);
183 if( chan.GetName() == ChanName ) {
191 std::cout <<
"Error: Did not find channel: " << ChanName
192 <<
" in measurement: " << GetName() << std::endl;
209 void RooStats::HistFactory::Measurement::PrintTree( std::ostream& stream )
213 stream <<
"Measurement Name: " << GetName()
214 <<
"\t OutputFilePrefix: " << fOutputFilePrefix
216 for(
unsigned int i = 0; i < fPOI.size(); ++i) {
217 stream << fPOI.at(i);
219 stream <<
"\t Lumi: " << fLumi
220 <<
"\t LumiRelErr: " << fLumiRelErr
221 <<
"\t BinLow: " << fBinLow
222 <<
"\t BinHigh: " << fBinHigh
223 <<
"\t ExportOnly: " << fExportOnly
227 if( fConstantParams.size() != 0 ) {
228 stream <<
"Constant Params: ";
229 for(
unsigned int i = 0; i < fConstantParams.size(); ++i ) {
230 stream <<
" " << fConstantParams.at(i);
235 if( fFunctionObjects.size() != 0 ) {
236 stream <<
"Preprocess Functions: ";
237 for(
unsigned int i = 0; i < fFunctionObjects.size(); ++i ) {
238 stream <<
" " << fFunctionObjects.at(i).GetCommand();
243 if( fChannels.size() != 0 ) {
244 stream <<
"Channels:" << std::endl;
245 for(
unsigned int i = 0; i < fChannels.size(); ++i ) {
246 fChannels.at(i).Print( stream );
250 std::cout <<
"End Measurement: " << GetName() << std::endl;
260 void RooStats::HistFactory::Measurement::PrintXML( std::string directory, std::string newOutputPrefix )
263 auto testExists = [](
const std::string& theDirectory) {
264 void* dir = gSystem->OpenDirectory(theDirectory.c_str());
265 bool exists = dir !=
nullptr;
267 gSystem->FreeDirectory(dir);
272 if ( !directory.empty() && !testExists(directory) ) {
273 int success = gSystem->MakeDirectory(directory.c_str() );
275 std::cout <<
"Error: Failed to make directory: " << directory << std::endl;
282 std::cout <<
"Printing XML Files for measurement: " << GetName() << std::endl;
284 std::string XMLName = std::string(GetName()) +
".xml";
285 if( directory !=
"" ) XMLName = directory +
"/" + XMLName;
287 ofstream xml( XMLName.c_str() );
289 if( ! xml.is_open() ) {
290 std::cout <<
"Error opening xml file: " << XMLName << std::endl;
296 xml <<
"<!--" << std::endl;
297 xml <<
"This xml file created automatically on: " << std::endl;
311 t.GetDate(
true, 0, &year, &month, &day);
317 xml <<
"-->" << std::endl;
320 xml <<
"<!DOCTYPE Combination SYSTEM 'HistFactorySchema.dtd'>" << std::endl << std::endl;
323 if (newOutputPrefix.empty() ) newOutputPrefix = fOutputFilePrefix;
324 xml <<
"<Combination OutputFilePrefix=\"" << newOutputPrefix <<
"\" >" << std::endl << std::endl;
327 for(
unsigned int i = 0; i < fFunctionObjects.size(); ++i ) {
328 RooStats::HistFactory::PreprocessFunction func = fFunctionObjects.at(i);
341 for(
unsigned int i = 0; i < fChannels.size(); ++i ) {
342 xml <<
" <Input>" <<
"./";
343 if (!directory.empty() ) xml << directory <<
"/";
344 xml << GetName() <<
"_" << fChannels.at(i).GetName() <<
".xml" <<
"</Input>" << std::endl;
350 xml <<
" <Measurement Name=\"" << GetName() <<
"\" "
351 <<
"Lumi=\"" << fLumi <<
"\" "
352 <<
"LumiRelErr=\"" << fLumiRelErr <<
"\" "
355 <<
"ExportOnly=\"" << (fExportOnly ? std::string(
"True") : std::string(
"False")) <<
"\" "
356 <<
" >" << std::endl;
361 for(
unsigned int i = 0; i < fPOI.size(); ++i) {
362 if(i==0) xml << fPOI.at(i);
363 else xml <<
" " << fPOI.at(i);
365 xml <<
"</POI> " << std::endl;
368 if(fConstantParams.size()) {
369 xml <<
" <ParamSetting Const=\"True\">";
370 for(
unsigned int i = 0; i < fConstantParams.size(); ++i ) {
371 if (i==0) xml << fConstantParams.at(i);
372 else xml <<
" " << fConstantParams.at(i);;
374 xml <<
"</ParamSetting>" << std::endl;
378 std::map<std::string, double>::iterator ConstrItr;
381 for( ConstrItr = fGammaSyst.begin(); ConstrItr != fGammaSyst.end(); ++ConstrItr ) {
382 xml <<
"<ConstraintTerm Type=\"Gamma\" RelativeUncertainty=\""
383 << ConstrItr->second <<
"\">" << ConstrItr->first
384 <<
"</ConstraintTerm>" << std::endl;
387 for( ConstrItr = fUniformSyst.begin(); ConstrItr != fUniformSyst.end(); ++ConstrItr ) {
388 xml <<
"<ConstraintTerm Type=\"Uniform\" RelativeUncertainty=\""
389 << ConstrItr->second <<
"\">" << ConstrItr->first
390 <<
"</ConstraintTerm>" << std::endl;
393 for( ConstrItr = fLogNormSyst.begin(); ConstrItr != fLogNormSyst.end(); ++ConstrItr ) {
394 xml <<
"<ConstraintTerm Type=\"LogNormal\" RelativeUncertainty=\""
395 << ConstrItr->second <<
"\">" << ConstrItr->first
396 <<
"</ConstraintTerm>" << std::endl;
399 for( ConstrItr = fNoSyst.begin(); ConstrItr != fNoSyst.end(); ++ConstrItr ) {
400 xml <<
"<ConstraintTerm Type=\"NoSyst\" RelativeUncertainty=\""
401 << ConstrItr->second <<
"\">" << ConstrItr->first
402 <<
"</ConstraintTerm>" << std::endl;
407 xml <<
" </Measurement> " << std::endl << std::endl;
410 xml <<
"</Combination>" << std::endl;
417 std::string prefix = std::string(GetName()) +
"_";
419 for(
unsigned int i = 0; i < fChannels.size(); ++i ) {
420 fChannels.at(i).PrintXML( directory, prefix );
424 std::cout <<
"Finished printing XML files" << std::endl;
438 void RooStats::HistFactory::Measurement::writeToFile( TFile* file )
443 RooStats::HistFactory::Measurement outMeas( *
this );
445 std::string OutputFileName = file->GetName();
451 for(
unsigned int chanItr = 0; chanItr < outMeas.fChannels.size(); ++chanItr ) {
459 RooStats::HistFactory::Channel& channel = outMeas.fChannels.at( chanItr );
460 std::string chanName = channel.GetName();
463 if( ! channel.CheckHistograms() ) {
464 std::cout <<
"Measurement.writeToFile(): Channel: " << chanName
465 <<
" has uninitialized histogram pointers" << std::endl;
478 TDirectory* chanDir = file->mkdir( (chanName +
"_hists").c_str() );
479 if( chanDir == NULL ) {
480 std::cout <<
"Error: Cannot create channel " << (chanName +
"_hists")
487 TDirectory* dataDir = chanDir->mkdir(
"data" );
488 if( dataDir == NULL ) {
489 std::cout <<
"Error: Cannot make directory " << chanDir << std::endl;
494 channel.fData.writeToFile( OutputFileName, GetDirPath(dataDir) );
511 for(
unsigned int sampItr = 0; sampItr < channel.GetSamples().size(); ++sampItr ) {
513 RooStats::HistFactory::Sample& sample = channel.GetSamples().at( sampItr );
514 std::string sampName = sample.GetName();
516 std::cout <<
"Writing sample: " << sampName << std::endl;
520 TDirectory* sampleDir = chanDir->mkdir( sampName.c_str() );
521 if( sampleDir == NULL ) {
522 std::cout <<
"Error: Directory " << sampName <<
" not created properly" << std::endl;
525 std::string sampleDirPath = GetDirPath( sampleDir );
528 std::cout <<
"Error making directory: " << sampName
529 <<
" in directory: " << chanName
537 sample.writeToFile( OutputFileName, sampleDirPath );
599 std::cout <<
"Saved all histograms" << std::endl;
604 std::cout <<
"Saved Measurement" << std::endl;
610 std::string RooStats::HistFactory::Measurement::GetDirPath( TDirectory* dir )
614 std::string path = dir->GetPath();
616 if( path.find(
":") != std::string::npos ) {
617 size_t index = path.find(
":");
618 path.replace( 0, index+1,
"" );
645 void RooStats::HistFactory::Measurement::CollectHistograms()
649 for(
unsigned int chanItr = 0; chanItr < fChannels.size(); ++chanItr) {
651 RooStats::HistFactory::Channel& chan = fChannels.at( chanItr );
653 chan.CollectHistograms();