Skip to content

Commit 26b25d7

Browse files
committed
[RF][HF] Unify errors and warnings in HistFactory workspace creation
Replace printing directly to `std::cout` with HistFactory printing macros, for consistent logging.
1 parent 7278ec6 commit 26b25d7

File tree

1 file changed

+77
-88
lines changed

1 file changed

+77
-88
lines changed

roofit/histfactory/src/HistoToWorkspaceFactoryFast.cxx

Lines changed: 77 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,17 @@ std::vector<double> histToVector(TH1 const &hist)
9494

9595
// use this order for safety on library loading
9696
using namespace RooStats;
97-
using std::string, std::vector, std::make_unique, std::pair, std::unique_ptr, std::map;
97+
using std::string, std::vector;
9898

9999
using namespace RooStats::HistFactory::Detail;
100100
using namespace RooStats::HistFactory::Detail::MagicConstants;
101101

102+
namespace RooStats::HistFactory {
102103

103-
namespace RooStats{
104-
namespace HistFactory{
105-
106-
HistoToWorkspaceFactoryFast::HistoToWorkspaceFactoryFast(RooStats::HistFactory::Measurement& measurement) :
107-
HistoToWorkspaceFactoryFast{measurement, Configuration{}} {}
104+
HistoToWorkspaceFactoryFast::HistoToWorkspaceFactoryFast(RooStats::HistFactory::Measurement &measurement)
105+
: HistoToWorkspaceFactoryFast{measurement, Configuration{}}
106+
{
107+
}
108108

109109
HistoToWorkspaceFactoryFast::HistoToWorkspaceFactoryFast(RooStats::HistFactory::Measurement& measurement,
110110
Configuration const& cfg) :
@@ -130,9 +130,8 @@ namespace HistFactory{
130130
// Make a ModelConfig and configure it
131131
ModelConfig * proto_config = static_cast<ModelConfig *>(ws_single->obj("ModelConfig"));
132132
if( proto_config == nullptr ) {
133-
std::cout << "Error: Did not find 'ModelConfig' object in file: " << ws_single->GetName()
134-
<< std::endl;
135-
throw hf_exc();
133+
cxcoutFHF << "Error: Did not find 'ModelConfig' object in file: " << ws_single->GetName() << std::endl;
134+
throw hf_exc();
136135
}
137136

138137
if( measurement.GetPOIList().empty() ) {
@@ -153,9 +152,9 @@ namespace HistFactory{
153152
params.add(*poi);
154153
}
155154
else {
156-
std::cout << "WARNING: Can't find parameter of interest: " << poi_name
157-
<< " in Workspace. Not setting in ModelConfig." << std::endl;
158-
//throw hf_exc();
155+
cxcoutWHF << "WARNING: Can't find parameter of interest: " << poi_name
156+
<< " in Workspace. Not setting in ModelConfig." << std::endl;
157+
// throw hf_exc();
159158
}
160159
}
161160
proto_config->SetParametersOfInterest(params);
@@ -196,9 +195,8 @@ namespace HistFactory{
196195
cxcoutPHF << "Importing Asimov dataset" << std::endl;
197196
bool failure = ws_single->import(*asimov_dataset, RooFit::Rename(AsimovName.c_str()));
198197
if( failure ) {
199-
std::cout << "Error: Failed to import Asimov dataset: " << AsimovName
200-
<< std::endl;
201-
throw hf_exc();
198+
cxcoutFHF << "Error: Failed to import Asimov dataset: " << AsimovName << std::endl;
199+
throw hf_exc();
202200
}
203201

204202
// Load the snapshot at the end of every loop iteration
@@ -229,9 +227,9 @@ namespace HistFactory{
229227
// Create a workspace for a SingleChannel from the Measurement Object
230228
std::unique_ptr<RooWorkspace> ws_single{this->MakeSingleChannelWorkspace(measurement, channel)};
231229
if( ws_single == nullptr ) {
232-
cxcoutF(HistFactory) << "Error: Failed to make Single-Channel workspace for channel: " << ch_name
233-
<< " and measurement: " << measurement.GetName() << std::endl;
234-
throw hf_exc();
230+
cxcoutFHF << "Error: Failed to make Single-Channel workspace for channel: " << ch_name
231+
<< " and measurement: " << measurement.GetName() << std::endl;
232+
throw hf_exc();
235233
}
236234

237235
// Finally, configure that workspace based on
@@ -277,8 +275,8 @@ namespace HistFactory{
277275
HistoToWorkspaceFactoryFast histFactory(measurement, config);
278276

279277
// Loop over the channels and create the individual workspaces
280-
vector<std::unique_ptr<RooWorkspace>> channel_workspaces;
281-
vector<string> channel_names;
278+
std::vector<std::unique_ptr<RooWorkspace>> channel_workspaces;
279+
std::vector<std::string> channel_names;
282280

283281
for(HistFactory::Channel& channel : measurement.GetChannels()) {
284282

@@ -748,9 +746,9 @@ RooArgList HistoToWorkspaceFactoryFast::createObservables(const TH1 *hist, RooWo
748746
}
749747

750748
if( ! channel.CheckHistograms() ) {
751-
std::cout << "MakeSingleChannelWorkspace: Channel: " << channel.GetName()
752-
<< " has uninitialized histogram pointers" << std::endl;
753-
throw hf_exc();
749+
cxcoutFHF << "MakeSingleChannelWorkspace: Channel: " << channel.GetName()
750+
<< " has uninitialized histogram pointers" << std::endl;
751+
throw hf_exc();
754752
}
755753

756754

@@ -796,7 +794,7 @@ RooArgList HistoToWorkspaceFactoryFast::createObservables(const TH1 *hist, RooWo
796794
//
797795
auto protoOwner = std::make_unique<RooWorkspace>(channel_name.c_str(), (channel_name+" workspace").c_str());
798796
RooWorkspace &proto = *protoOwner;
799-
auto proto_config = make_unique<ModelConfig>("ModelConfig", &proto);
797+
auto proto_config = std::make_unique<ModelConfig>("ModelConfig", &proto);
800798

801799
// preprocess functions
802800
for(auto const& func : fPreprocessFunctions){
@@ -814,8 +812,8 @@ RooArgList HistoToWorkspaceFactoryFast::createObservables(const TH1 *hist, RooWo
814812
std::vector<std::vector<RooAbsArg*>> allSampleHistFuncs;
815813
std::vector<RooProduct*> sampleScaleFactors;
816814

817-
std::vector< pair<string,string> > statNamePairs;
818-
std::vector< pair<const TH1*, std::unique_ptr<TH1>> > statHistPairs; // <nominal, error>
815+
std::vector<std::pair<string, string>> statNamePairs;
816+
std::vector<std::pair<const TH1 *, std::unique_ptr<TH1>>> statHistPairs; // <nominal, error>
819817
const std::string statFuncName = "mc_stat_" + channel_name;
820818

821819
string prefix;
@@ -918,9 +916,8 @@ RooArgList HistoToWorkspaceFactoryFast::createObservables(const TH1 *hist, RooWo
918916
if( sample.GetStatError().GetActivate() ) {
919917

920918
if( fObsNameVec.size() > 3 ) {
921-
cxcoutF(HistFactory) << "Cannot include Stat Error for histograms of more than 3 dimensions."
922-
<< std::endl;
923-
throw hf_exc();
919+
cxcoutFHF << "Cannot include Stat Error for histograms of more than 3 dimensions." << std::endl;
920+
throw hf_exc();
924921
} else {
925922

926923
// If we are using StatUncertainties, we multiply this object
@@ -1018,9 +1015,8 @@ RooArgList HistoToWorkspaceFactoryFast::createObservables(const TH1 *hist, RooWo
10181015
if( !sample.GetShapeFactorList().empty() ) {
10191016

10201017
if( fObsNameVec.size() > 3 ) {
1021-
cxcoutF(HistFactory) << "Cannot include Stat Error for histograms of more than 3 dimensions."
1022-
<< std::endl;
1023-
throw hf_exc();
1018+
cxcoutFHF << "Cannot include Stat Error for histograms of more than 3 dimensions." << std::endl;
1019+
throw hf_exc();
10241020
} else {
10251021

10261022
cxcoutI(HistFactory) << "Sample: " << sample.GetName() << " in channel: " << channel_name
@@ -1086,9 +1082,8 @@ RooArgList HistoToWorkspaceFactoryFast::createObservables(const TH1 *hist, RooWo
10861082
if( !sample.GetShapeSysList().empty() ) {
10871083

10881084
if( fObsNameVec.size() > 3 ) {
1089-
cxcoutF(HistFactory) << "Cannot include Stat Error for histograms of more than 3 dimensions."
1090-
<< std::endl;
1091-
throw hf_exc();
1085+
cxcoutFHF << "Cannot include Stat Error for histograms of more than 3 dimensions.\n";
1086+
throw hf_exc();
10921087
}
10931088

10941089
// List of ShapeSys ParamHistFuncs
@@ -1210,11 +1205,11 @@ RooArgList HistoToWorkspaceFactoryFast::createObservables(const TH1 *hist, RooWo
12101205

12111206
// Create the histogram of (binwise)
12121207
// stat uncertainties:
1213-
unique_ptr<TH1> fracStatError( MakeScaledUncertaintyHist( channel_name + "_StatUncert" + "_RelErr", statHistPairs) );
1208+
std::unique_ptr<TH1> fracStatError(
1209+
MakeScaledUncertaintyHist(channel_name + "_StatUncert" + "_RelErr", statHistPairs));
12141210
if( fracStatError == nullptr ) {
1215-
cxcoutE(HistFactory) << "Error: Failed to make ScaledUncertaintyHist for: "
1216-
<< channel_name + "_StatUncert" + "_RelErr" << std::endl;
1217-
throw hf_exc();
1211+
cxcoutFHF << "Error: Failed to make ScaledUncertaintyHist for: " << channel_name + "_StatUncert" + "_RelErr\n";
1212+
throw hf_exc();
12181213
}
12191214

12201215
// Using this TH1* of fractinal stat errors,
@@ -1281,19 +1276,19 @@ RooArgList HistoToWorkspaceFactoryFast::createObservables(const TH1 *hist, RooWo
12811276
for(unsigned int i=0; i<constraintTermNames.size(); ++i){
12821277
RooAbsArg* proto_arg = proto.arg(constraintTermNames[i]);
12831278
if( proto_arg==nullptr ) {
1284-
cxcoutF(HistFactory) << "Error: Cannot find arg set: " << constraintTermNames.at(i)
1285-
<< " in workspace: " << proto.GetName() << std::endl;
1286-
throw hf_exc();
1279+
cxcoutFHF << "Error: Cannot find arg set: " << constraintTermNames.at(i)
1280+
<< " in workspace: " << proto.GetName() << std::endl;
1281+
throw hf_exc();
12871282
}
12881283
constraintTerms.add( *proto_arg );
12891284
// constraintTerms.add(* proto_arg(proto.arg(constraintTermNames[i].c_str())) );
12901285
}
12911286
for(unsigned int i=0; i<likelihoodTermNames.size(); ++i){
12921287
RooAbsArg* proto_arg = (proto.arg(likelihoodTermNames[i]));
12931288
if( proto_arg==nullptr ) {
1294-
cxcoutF(HistFactory) << "Error: Cannot find arg set: " << likelihoodTermNames.at(i)
1295-
<< " in workspace: " << proto.GetName() << std::endl;
1296-
throw hf_exc();
1289+
cxcoutFHF << "Error: Cannot find arg set: " << likelihoodTermNames.at(i)
1290+
<< " in workspace: " << proto.GetName() << std::endl;
1291+
throw hf_exc();
12971292
}
12981293
likelihoodTerms.add( *proto_arg );
12991294
}
@@ -1322,10 +1317,10 @@ RooArgList HistoToWorkspaceFactoryFast::createObservables(const TH1 *hist, RooWo
13221317
<< "\timport model into workspace"
13231318
<< "\n-----------------------------------------\n" << std::endl;
13241319

1325-
auto model = make_unique<RooProdPdf>(
1326-
("model_"+channel_name).c_str(), // MB : have changed this into conditional pdf. Much faster for toys!
1327-
"product of Poissons across bins for a single channel",
1328-
constraintTerms, RooFit::Conditional(likelihoodTerms,observables));
1320+
auto model = std::make_unique<RooProdPdf>(
1321+
("model_" + channel_name).c_str(), // MB : have changed this into conditional pdf. Much faster for toys!
1322+
"product of Poissons across bins for a single channel", constraintTerms,
1323+
RooFit::Conditional(likelihoodTerms, observables));
13291324
// can give channel a title by setting title of corresponding data histogram
13301325
if (channel.GetData().GetHisto() && strlen(channel.GetData().GetHisto()->GetTitle())>0) {
13311326
model->SetTitle(channel.GetData().GetHisto()->GetTitle());
@@ -1355,7 +1350,7 @@ RooArgList HistoToWorkspaceFactoryFast::createObservables(const TH1 *hist, RooWo
13551350
// actually create the files with the stored per-channel workspaces.
13561351
// Otherwise, we just spend time calculating something that gets thrown
13571352
// away anyway (for the combined workspace, we'll create a new Asimov).
1358-
unique_ptr<RooAbsData> asimov_dataset(AsymptoticCalculator::GenerateAsimovData(*model, observables));
1353+
std::unique_ptr<RooAbsData> asimov_dataset(AsymptoticCalculator::GenerateAsimovData(*model, observables));
13591354
proto.import(*asimov_dataset, RooFit::Rename("asimovData"));
13601355
}
13611356

@@ -1378,17 +1373,17 @@ RooArgList HistoToWorkspaceFactoryFast::createObservables(const TH1 *hist, RooWo
13781373

13791374
for(auto const& data : channel.GetAdditionalData()) {
13801375
if(data.GetName().empty()) {
1381-
cxcoutF(HistFactory) << "Error: Additional Data histogram for channel: " << channel.GetName()
1382-
<< " has no name! The name always needs to be set for additional datasets, "
1383-
<< "either via the \"Name\" tag in the XML or via RooStats::HistFactory::Data::SetName()." << std::endl;
1384-
throw hf_exc();
1376+
cxcoutFHF << "Error: Additional Data histogram for channel: " << channel.GetName()
1377+
<< " has no name! The name always needs to be set for additional datasets, "
1378+
<< "either via the \"Name\" tag in the XML or via RooStats::HistFactory::Data::SetName().\n";
1379+
throw hf_exc();
13851380
}
13861381
std::string const& dataName = data.GetName();
13871382
TH1 const* mnominal = data.GetHisto();
13881383
if( !mnominal ) {
1389-
cxcoutF(HistFactory) << "Error: Additional Data histogram for channel: " << channel.GetName()
1390-
<< " with name: " << dataName << " is nullptr" << std::endl;
1391-
throw hf_exc();
1384+
cxcoutFHF << "Error: Additional Data histogram for channel: " << channel.GetName()
1385+
<< " with name: " << dataName << " is nullptr\n";
1386+
throw hf_exc();
13921387
}
13931388

13941389
// THis works and is natural, but the memory size of the simultaneous dataset grows exponentially with channels
@@ -1488,13 +1483,13 @@ RooArgList HistoToWorkspaceFactoryFast::createObservables(const TH1 *hist, RooWo
14881483
/// These things were used for debugging. Maybe useful in the future
14891484
//
14901485

1491-
map<string, RooAbsPdf*> pdfMap;
1492-
vector<RooAbsPdf*> models;
1486+
std::map<string, RooAbsPdf *> pdfMap;
1487+
vector<RooAbsPdf *> models;
14931488

1494-
RooArgList obsList;
1495-
for(unsigned int i = 0; i< ch_names.size(); ++i){
1496-
obsList.add(*static_cast<ModelConfig *>(chs[i]->obj("ModelConfig"))->GetObservables());
1497-
}
1489+
RooArgList obsList;
1490+
for (unsigned int i = 0; i < ch_names.size(); ++i) {
1491+
obsList.add(*static_cast<ModelConfig *>(chs[i]->obj("ModelConfig"))->GetObservables());
1492+
}
14981493
cxcoutI(HistFactory) <<"full list of observables:\n" << obsList << std::endl;
14991494

15001495
RooArgSet globalObs;
@@ -1512,8 +1507,10 @@ RooArgList HistoToWorkspaceFactoryFast::createObservables(const TH1 *hist, RooWo
15121507
RooWorkspace * ch=chs[i].get();
15131508

15141509
RooAbsPdf* model = ch->pdf("model_"+channel_name);
1515-
if(!model) std::cout <<"failed to find model for channel"<< std::endl;
1516-
// std::cout << "int = " << model->createIntegral(*obsN)->getVal() << std::endl;
1510+
if (!model) {
1511+
cxcoutFHF << "failed to find model for channel\n";
1512+
throw hf_exc();
1513+
}
15171514
models.push_back(model);
15181515
globalObs.add(*ch->set("globalObservables"), /*silent=*/true); // silent because observables might exist in other channel.
15191516

@@ -1621,8 +1618,8 @@ RooArgList HistoToWorkspaceFactoryFast::createObservables(const TH1 *hist, RooWo
16211618
combined->import( *asimov_combined, RooFit::Rename("asimovData"));
16221619
}
16231620
else {
1624-
std::cout << "Error: Failed to create combined asimov dataset" << std::endl;
1625-
throw hf_exc();
1621+
cxcoutFHF << "Error: Failed to create combined asimov dataset\n";
1622+
throw hf_exc();
16261623
}
16271624

16281625
return RooFit::makeOwningPtr(std::move(combined));
@@ -1654,23 +1651,19 @@ RooArgList HistoToWorkspaceFactoryFast::createObservables(const TH1 *hist, RooWo
16541651

16551652
// Check that histError != NAN
16561653
if( histError != histError ) {
1657-
std::cout << "Warning: In histogram " << Nominal->GetName()
1658-
<< " bin error for bin " << i_bin
1659-
<< " is NAN. Not using Error!!!"
1660-
<< std::endl;
1661-
throw hf_exc();
1662-
//histError = sqrt( histContent );
1663-
//histError = 0;
1654+
cxcoutFHF << "Warning: In histogram " << Nominal->GetName() << " bin error for bin " << i_bin
1655+
<< " is NAN. Not using Error!!!\n";
1656+
throw hf_exc();
1657+
// histError = sqrt( histContent );
1658+
// histError = 0;
16641659
}
16651660

16661661
// Check that histError ! < 0
16671662
if( histError < 0 ) {
1668-
std::cout << "Warning: In histogram " << Nominal->GetName()
1669-
<< " bin error for bin " << binNumber
1670-
<< " is < 0. Setting Error to 0"
1671-
<< std::endl;
1672-
//histError = sqrt( histContent );
1673-
histError = 0;
1663+
cxcoutWHF << "Warning: In histogram " << Nominal->GetName() << " bin error for bin " << binNumber
1664+
<< " is < 0. Setting Error to 0" << std::endl;
1665+
// histError = sqrt( histContent );
1666+
histError = 0;
16741667
}
16751668

16761669
ErrorHist->SetBinContent( binNumber, histError );
@@ -1743,10 +1736,9 @@ RooArgList HistoToWorkspaceFactoryFast::createObservables(const TH1 *hist, RooWo
17431736
double histError = error->GetBinContent( binNumber );
17441737

17451738
if( histError != histError ) {
1746-
cxcoutE(HistFactory) << "In histogram " << error->GetName()
1747-
<< " bin error for bin " << binNumber
1748-
<< " is NAN. Not using error!!";
1749-
throw hf_exc();
1739+
cxcoutFHF << "In histogram " << error->GetName() << " bin error for bin " << binNumber
1740+
<< " is NAN. Not using error!!";
1741+
throw hf_exc();
17501742
}
17511743

17521744
TotalBinContent.at(i_bins) += histValue;
@@ -1812,7 +1804,4 @@ RooArgList HistoToWorkspaceFactoryFast::createObservables(const TH1 *hist, RooWo
18121804
return std::unique_ptr<TH1>(ErrorHist);
18131805
}
18141806

1815-
1816-
} // namespace RooStats
1817-
} // namespace HistFactory
1818-
1807+
} // namespace RooStats::HistFactory

0 commit comments

Comments
 (0)