33 RooStats::HistFactory::Channel::Channel() :
39 RooStats::HistFactory::Channel::Channel(
const Channel& other) :
41 fInputFile( other.fInputFile ),
42 fHistoPath( other.fHistoPath ),
44 fAdditionalData( other.fAdditionalData ),
45 fStatErrorConfig( other.fStatErrorConfig ),
46 fSamples( other.fSamples )
50 RooStats::HistFactory::Channel::Channel(std::string ChanName, std::string ChanInputFile) :
51 fName( ChanName ), fInputFile( ChanInputFile )
57 namespace HistFactory{
65 void RooStats::HistFactory::Channel::AddSample( RooStats::HistFactory::Sample sample )
69 sample.SetChannelName( GetName() );
70 fSamples.push_back( sample );
73 void RooStats::HistFactory::Channel::Print( std::ostream& stream ) {
76 stream <<
"\t Channel Name: " << fName
77 <<
"\t InputFile: " << fInputFile
80 stream <<
"\t Data:" << std::endl;
81 fData.Print( stream );
84 stream <<
"\t statErrorConfig:" << std::endl;
85 fStatErrorConfig.Print( stream );
88 if( fSamples.size() != 0 ) {
90 stream <<
"\t Samples: " << std::endl;
91 for(
unsigned int i = 0; i < fSamples.size(); ++i ) {
92 fSamples.at(i).Print( stream );
97 stream <<
"\t End of Channel " << fName << std::endl;
103 void RooStats::HistFactory::Channel::PrintXML( std::string directory, std::string prefix ) {
106 std::cout <<
"Printing XML Files for channel: " << GetName() << std::endl;
108 std::string XMLName = prefix + fName +
".xml";
109 if( directory !=
"" ) XMLName = directory +
"/" + XMLName;
111 ofstream xml( XMLName.c_str() );
114 xml <<
"<!--" << std::endl;
115 xml <<
"This xml file created automatically on: " << std::endl;
121 t.GetDate(
true, 0, &year, &month, &day);
126 xml <<
"-->" << std::endl;
129 xml <<
"<!DOCTYPE Channel SYSTEM 'HistFactorySchema.dtd'> " << std::endl << std::endl;
132 xml <<
" <Channel Name=\"" << fName <<
"\" InputFile=\"" << fInputFile <<
"\" >" << std::endl << std::endl;
134 fData.PrintXML( xml );
142 fStatErrorConfig.PrintXML( xml );
149 for(
unsigned int i = 0; i < fSamples.size(); ++i ) {
150 fSamples.at(i).PrintXML( xml );
151 xml << std::endl << std::endl;
155 xml <<
" </Channel> " << std::endl;
158 std::cout <<
"Finished printing XML files" << std::endl;
164 void RooStats::HistFactory::Channel::SetData( std::string DataHistoName, std::string DataInputFile, std::string DataHistoPath ) {
168 fData.SetHistoName( DataHistoName );
169 fData.SetInputFile( DataInputFile );
170 fData.SetHistoPath( DataHistoPath );
176 void RooStats::HistFactory::Channel::SetData( TH1* hData ) {
178 fData.SetHisto( hData );
181 void RooStats::HistFactory::Channel::SetData(
double val ) {
188 std::string DataHistName = fName +
"_data";
191 TH1F* hData =
new TH1F( DataHistName.c_str(), DataHistName.c_str(), 1, 0, 1 );
192 hData->SetBinContent( 1, val );
201 void RooStats::HistFactory::Channel::SetStatErrorConfig(
double StatRelErrorThreshold, Constraint::Type StatConstraintType ) {
203 fStatErrorConfig.SetRelErrorThreshold( StatRelErrorThreshold );
204 fStatErrorConfig.SetConstraintType( StatConstraintType );
208 void RooStats::HistFactory::Channel::SetStatErrorConfig(
double StatRelErrorThreshold, std::string StatConstraintType ) {
210 fStatErrorConfig.SetRelErrorThreshold( StatRelErrorThreshold );
211 fStatErrorConfig.SetConstraintType( Constraint::GetType(StatConstraintType) );
217 void RooStats::HistFactory::Channel::CollectHistograms() {
224 if( fData.GetInputFile() !=
"" ) {
225 fData.SetHisto( GetHistogram(fData.GetInputFile(),
226 fData.GetHistoPath(),
227 fData.GetHistoName()) );
231 for(
unsigned int i=0; i < fAdditionalData.size(); ++i) {
232 RooStats::HistFactory::Data& data = fAdditionalData.at(i);
233 if( data.GetInputFile() !=
"" ) {
234 data.SetHisto( GetHistogram(data.GetInputFile(), data.GetHistoPath(),data.GetHistoName()) );
239 for(
unsigned int sampItr = 0; sampItr < fSamples.size(); ++sampItr ) {
241 RooStats::HistFactory::Sample& sample = fSamples.at( sampItr );
245 std::cout <<
"Collecting Nominal Histogram" << std::endl;
246 TH1* Nominal = GetHistogram(sample.GetInputFile(),
247 sample.GetHistoPath(),
248 sample.GetHistoName());
250 sample.SetHisto( Nominal );
254 if( sample.GetStatError().GetUseHisto() ) {
255 sample.GetStatError().SetErrorHist( GetHistogram(sample.GetStatError().GetInputFile(),
256 sample.GetStatError().GetHistoPath(),
257 sample.GetStatError().GetHistoName()) );
262 for(
unsigned int histoSysItr = 0; histoSysItr < sample.GetHistoSysList().size(); ++histoSysItr ) {
264 RooStats::HistFactory::HistoSys& histoSys = sample.GetHistoSysList().at( histoSysItr );
266 histoSys.SetHistoLow( GetHistogram(histoSys.GetInputFileLow(),
267 histoSys.GetHistoPathLow(),
268 histoSys.GetHistoNameLow()) );
270 histoSys.SetHistoHigh( GetHistogram(histoSys.GetInputFileHigh(),
271 histoSys.GetHistoPathHigh(),
272 histoSys.GetHistoNameHigh()) );
277 for(
unsigned int histoFactorItr = 0; histoFactorItr < sample.GetHistoFactorList().size(); ++histoFactorItr ) {
279 RooStats::HistFactory::HistoFactor& histoFactor = sample.GetHistoFactorList().at( histoFactorItr );
281 histoFactor.SetHistoLow( GetHistogram(histoFactor.GetInputFileLow(),
282 histoFactor.GetHistoPathLow(),
283 histoFactor.GetHistoNameLow()) );
285 histoFactor.SetHistoHigh( GetHistogram(histoFactor.GetInputFileHigh(),
286 histoFactor.GetHistoPathHigh(),
287 histoFactor.GetHistoNameHigh()) );
292 for(
unsigned int shapeSysItr = 0; shapeSysItr < sample.GetShapeSysList().size(); ++shapeSysItr ) {
294 RooStats::HistFactory::ShapeSys& shapeSys = sample.GetShapeSysList().at( shapeSysItr );
296 shapeSys.SetErrorHist( GetHistogram(shapeSys.GetInputFile(),
297 shapeSys.GetHistoPath(),
298 shapeSys.GetHistoName()) );
303 for(
unsigned int shapeFactorItr = 0; shapeFactorItr < sample.GetShapeFactorList().size(); ++shapeFactorItr ) {
305 RooStats::HistFactory::ShapeFactor& shapeFactor = sample.GetShapeFactorList().at( shapeFactorItr );
308 if( shapeFactor.HasInitialShape() ) {
309 TH1* hist = GetHistogram( shapeFactor.GetInputFile(), shapeFactor.GetHistoPath(),
310 shapeFactor.GetHistoName() );
311 shapeFactor.SetInitialShape( hist );
323 bool RooStats::HistFactory::Channel::CheckHistograms() {
330 if( fData.GetHisto() == NULL && fData.GetInputFile() !=
"" ) {
331 std::cout <<
"Error: Data Histogram for channel " << GetName() <<
" is NULL." << std::endl;
336 for(
unsigned int sampItr = 0; sampItr < fSamples.size(); ++sampItr ) {
338 RooStats::HistFactory::Sample& sample = fSamples.at( sampItr );
341 if( sample.GetHisto() == NULL ) {
342 std::cout <<
"Error: Nominal Histogram for sample " << sample.GetName() <<
" is NULL." << std::endl;
348 std::vector<int> NegativeBinNumber;
349 std::vector<double> NegativeBinContent;
350 const TH1* histNominal = sample.GetHisto();
351 for(
int ibin=1; ibin<=histNominal->GetNbinsX(); ++ibin) {
352 if(histNominal->GetBinContent(ibin) < 0) {
353 NegativeBinNumber.push_back(ibin);
354 NegativeBinContent.push_back(histNominal->GetBinContent(ibin));
357 if(NegativeBinNumber.size()>0) {
358 std::cout <<
"WARNING: Nominal Histogram " << histNominal->GetName() <<
" for Sample = " << sample.GetName()
359 <<
" in Channel = " << GetName() <<
" has negative entries in bin numbers = ";
361 for(
unsigned int ibin=0; ibin<NegativeBinNumber.size(); ++ibin) {
362 if(ibin>0) std::cout <<
" , " ;
363 std::cout << NegativeBinNumber[ibin] <<
" : " << NegativeBinContent[ibin] ;
365 std::cout << std::endl;
371 if( sample.GetStatError().GetUseHisto() ) {
372 if( sample.GetStatError().GetErrorHist() == NULL ) {
373 std::cout <<
"Error: Statistical Error Histogram for sample " << sample.GetName() <<
" is NULL." << std::endl;
380 for(
unsigned int histoSysItr = 0; histoSysItr < sample.GetHistoSysList().size(); ++histoSysItr ) {
382 RooStats::HistFactory::HistoSys& histoSys = sample.GetHistoSysList().at( histoSysItr );
384 if( histoSys.GetHistoLow() == NULL ) {
385 std::cout <<
"Error: HistoSyst Low for Systematic " << histoSys.GetName()
386 <<
" in sample " << sample.GetName() <<
" is NULL." << std::endl;
389 if( histoSys.GetHistoHigh() == NULL ) {
390 std::cout <<
"Error: HistoSyst High for Systematic " << histoSys.GetName()
391 <<
" in sample " << sample.GetName() <<
" is NULL." << std::endl;
399 for(
unsigned int histoFactorItr = 0; histoFactorItr < sample.GetHistoFactorList().size(); ++histoFactorItr ) {
401 RooStats::HistFactory::HistoFactor& histoFactor = sample.GetHistoFactorList().at( histoFactorItr );
403 if( histoFactor.GetHistoLow() == NULL ) {
404 std::cout <<
"Error: HistoSyst Low for Systematic " << histoFactor.GetName()
405 <<
" in sample " << sample.GetName() <<
" is NULL." << std::endl;
408 if( histoFactor.GetHistoHigh() == NULL ) {
409 std::cout <<
"Error: HistoSyst High for Systematic " << histoFactor.GetName()
410 <<
" in sample " << sample.GetName() <<
" is NULL." << std::endl;
418 for(
unsigned int shapeSysItr = 0; shapeSysItr < sample.GetShapeSysList().size(); ++shapeSysItr ) {
420 RooStats::HistFactory::ShapeSys& shapeSys = sample.GetShapeSysList().at( shapeSysItr );
422 if( shapeSys.GetErrorHist() == NULL ) {
423 std::cout <<
"Error: HistoSyst High for Systematic " << shapeSys.GetName()
424 <<
" in sample " << sample.GetName() <<
" is NULL." << std::endl;
433 catch(std::exception& e)
435 std::cout << e.what() << std::endl;
449 TH1* RooStats::HistFactory::Channel::GetHistogram(std::string InputFile, std::string HistoPath, std::string HistoName) {
451 std::cout <<
"Getting histogram. "
452 <<
" InputFile " << InputFile
453 <<
" HistoPath " << HistoPath
454 <<
" HistoName " << HistoName
459 TFile* inFile = TFile::Open( InputFile.c_str() );
461 std::cout <<
"Error: Unable to open input file: " << InputFile << std::endl;
465 std::cout <<
"Opened input file: " << InputFile <<
": " << inFile << std::endl;
467 std::string HistNameFull = HistoPath + HistoName;
469 if( HistoPath != std::string(
"") ) {
470 if( HistoPath[ HistoPath.length()-1 ] != std::string(
"/") ) {
471 std::cout <<
"WARNING: Histogram path is set to: " << HistoPath
472 <<
" but it should end with a '/' " << std::endl;
473 std::cout <<
"Total histogram path is now: " << HistNameFull << std::endl;
479 hist =
dynamic_cast<TH1*
>( inFile->Get( HistNameFull.c_str() ) );
481 catch(std::exception& e)
483 std::cout <<
"Failed to cast object to TH1*" << std::endl;
484 std::cout << e.what() << std::endl;
488 std::cout <<
"Failed to get histogram: " << HistNameFull
489 <<
" in file: " << InputFile << std::endl;
494 TH1 * ptr = (TH1 *) hist->Clone();
497 std::cerr <<
"Not all necessary info are set to access the input file. Check your config" << std::endl;
498 std::cerr <<
"filename: " << InputFile
499 <<
"path: " << HistoPath
500 <<
"obj: " << HistoName << std::endl;
504 ptr->SetDirectory(0);
509 std::cout <<
"Found Histogram: " << HistoName
" at address: " << ptr
510 <<
" with integral " << ptr->Integral() <<
" and mean " << ptr->GetMean()