Skip to content

Commit 56083e7

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 56083e7

File tree

1 file changed

+38
-43
lines changed

1 file changed

+38
-43
lines changed

roofit/histfactory/src/HistoToWorkspaceFactoryFast.cxx

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,13 @@ 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

102102

103-
namespace RooStats{
104-
namespace HistFactory{
103+
namespace RooStats::HistFactory {
105104

106105
HistoToWorkspaceFactoryFast::HistoToWorkspaceFactoryFast(RooStats::HistFactory::Measurement& measurement) :
107106
HistoToWorkspaceFactoryFast{measurement, Configuration{}} {}
@@ -130,8 +129,7 @@ namespace HistFactory{
130129
// Make a ModelConfig and configure it
131130
ModelConfig * proto_config = static_cast<ModelConfig *>(ws_single->obj("ModelConfig"));
132131
if( proto_config == nullptr ) {
133-
std::cout << "Error: Did not find 'ModelConfig' object in file: " << ws_single->GetName()
134-
<< std::endl;
132+
cxcoutFHF << "Error: Did not find 'ModelConfig' object in file: " << ws_single->GetName() << std::endl;
135133
throw hf_exc();
136134
}
137135

@@ -153,7 +151,7 @@ namespace HistFactory{
153151
params.add(*poi);
154152
}
155153
else {
156-
std::cout << "WARNING: Can't find parameter of interest: " << poi_name
154+
cxcoutWHF << "WARNING: Can't find parameter of interest: " << poi_name
157155
<< " in Workspace. Not setting in ModelConfig." << std::endl;
158156
//throw hf_exc();
159157
}
@@ -196,9 +194,8 @@ namespace HistFactory{
196194
cxcoutPHF << "Importing Asimov dataset" << std::endl;
197195
bool failure = ws_single->import(*asimov_dataset, RooFit::Rename(AsimovName.c_str()));
198196
if( failure ) {
199-
std::cout << "Error: Failed to import Asimov dataset: " << AsimovName
200-
<< std::endl;
201-
throw hf_exc();
197+
cxcoutFHF << "Error: Failed to import Asimov dataset: " << AsimovName << std::endl;
198+
throw hf_exc();
202199
}
203200

204201
// Load the snapshot at the end of every loop iteration
@@ -229,7 +226,7 @@ namespace HistFactory{
229226
// Create a workspace for a SingleChannel from the Measurement Object
230227
std::unique_ptr<RooWorkspace> ws_single{this->MakeSingleChannelWorkspace(measurement, channel)};
231228
if( ws_single == nullptr ) {
232-
cxcoutF(HistFactory) << "Error: Failed to make Single-Channel workspace for channel: " << ch_name
229+
cxcoutFHF << "Error: Failed to make Single-Channel workspace for channel: " << ch_name
233230
<< " and measurement: " << measurement.GetName() << std::endl;
234231
throw hf_exc();
235232
}
@@ -277,8 +274,8 @@ namespace HistFactory{
277274
HistoToWorkspaceFactoryFast histFactory(measurement, config);
278275

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

283280
for(HistFactory::Channel& channel : measurement.GetChannels()) {
284281

@@ -748,7 +745,7 @@ RooArgList HistoToWorkspaceFactoryFast::createObservables(const TH1 *hist, RooWo
748745
}
749746

750747
if( ! channel.CheckHistograms() ) {
751-
std::cout << "MakeSingleChannelWorkspace: Channel: " << channel.GetName()
748+
cxcoutFHF << "MakeSingleChannelWorkspace: Channel: " << channel.GetName()
752749
<< " has uninitialized histogram pointers" << std::endl;
753750
throw hf_exc();
754751
}
@@ -796,7 +793,7 @@ RooArgList HistoToWorkspaceFactoryFast::createObservables(const TH1 *hist, RooWo
796793
//
797794
auto protoOwner = std::make_unique<RooWorkspace>(channel_name.c_str(), (channel_name+" workspace").c_str());
798795
RooWorkspace &proto = *protoOwner;
799-
auto proto_config = make_unique<ModelConfig>("ModelConfig", &proto);
796+
auto proto_config = std::make_unique<ModelConfig>("ModelConfig", &proto);
800797

801798
// preprocess functions
802799
for(auto const& func : fPreprocessFunctions){
@@ -814,8 +811,8 @@ RooArgList HistoToWorkspaceFactoryFast::createObservables(const TH1 *hist, RooWo
814811
std::vector<std::vector<RooAbsArg*>> allSampleHistFuncs;
815812
std::vector<RooProduct*> sampleScaleFactors;
816813

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

821818
string prefix;
@@ -918,7 +915,7 @@ RooArgList HistoToWorkspaceFactoryFast::createObservables(const TH1 *hist, RooWo
918915
if( sample.GetStatError().GetActivate() ) {
919916

920917
if( fObsNameVec.size() > 3 ) {
921-
cxcoutF(HistFactory) << "Cannot include Stat Error for histograms of more than 3 dimensions."
918+
cxcoutFHF << "Cannot include Stat Error for histograms of more than 3 dimensions."
922919
<< std::endl;
923920
throw hf_exc();
924921
} else {
@@ -1018,7 +1015,7 @@ 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."
1018+
cxcoutFHF << "Cannot include Stat Error for histograms of more than 3 dimensions."
10221019
<< std::endl;
10231020
throw hf_exc();
10241021
} else {
@@ -1086,8 +1083,7 @@ RooArgList HistoToWorkspaceFactoryFast::createObservables(const TH1 *hist, RooWo
10861083
if( !sample.GetShapeSysList().empty() ) {
10871084

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

@@ -1210,10 +1206,10 @@ RooArgList HistoToWorkspaceFactoryFast::createObservables(const TH1 *hist, RooWo
12101206

12111207
// Create the histogram of (binwise)
12121208
// stat uncertainties:
1213-
unique_ptr<TH1> fracStatError( MakeScaledUncertaintyHist( channel_name + "_StatUncert" + "_RelErr", statHistPairs) );
1209+
std::unique_ptr<TH1> fracStatError( 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;
1211+
cxcoutFHF << "Error: Failed to make ScaledUncertaintyHist for: "
1212+
<< channel_name + "_StatUncert" + "_RelErr\n";
12171213
throw hf_exc();
12181214
}
12191215

@@ -1281,7 +1277,7 @@ RooArgList HistoToWorkspaceFactoryFast::createObservables(const TH1 *hist, RooWo
12811277
for(unsigned int i=0; i<constraintTermNames.size(); ++i){
12821278
RooAbsArg* proto_arg = proto.arg(constraintTermNames[i]);
12831279
if( proto_arg==nullptr ) {
1284-
cxcoutF(HistFactory) << "Error: Cannot find arg set: " << constraintTermNames.at(i)
1280+
cxcoutFHF << "Error: Cannot find arg set: " << constraintTermNames.at(i)
12851281
<< " in workspace: " << proto.GetName() << std::endl;
12861282
throw hf_exc();
12871283
}
@@ -1291,7 +1287,7 @@ RooArgList HistoToWorkspaceFactoryFast::createObservables(const TH1 *hist, RooWo
12911287
for(unsigned int i=0; i<likelihoodTermNames.size(); ++i){
12921288
RooAbsArg* proto_arg = (proto.arg(likelihoodTermNames[i]));
12931289
if( proto_arg==nullptr ) {
1294-
cxcoutF(HistFactory) << "Error: Cannot find arg set: " << likelihoodTermNames.at(i)
1290+
cxcoutFHF << "Error: Cannot find arg set: " << likelihoodTermNames.at(i)
12951291
<< " in workspace: " << proto.GetName() << std::endl;
12961292
throw hf_exc();
12971293
}
@@ -1322,7 +1318,7 @@ RooArgList HistoToWorkspaceFactoryFast::createObservables(const TH1 *hist, RooWo
13221318
<< "\timport model into workspace"
13231319
<< "\n-----------------------------------------\n" << std::endl;
13241320

1325-
auto model = make_unique<RooProdPdf>(
1321+
auto model = std::make_unique<RooProdPdf>(
13261322
("model_"+channel_name).c_str(), // MB : have changed this into conditional pdf. Much faster for toys!
13271323
"product of Poissons across bins for a single channel",
13281324
constraintTerms, RooFit::Conditional(likelihoodTerms,observables));
@@ -1355,7 +1351,7 @@ RooArgList HistoToWorkspaceFactoryFast::createObservables(const TH1 *hist, RooWo
13551351
// actually create the files with the stored per-channel workspaces.
13561352
// Otherwise, we just spend time calculating something that gets thrown
13571353
// away anyway (for the combined workspace, we'll create a new Asimov).
1358-
unique_ptr<RooAbsData> asimov_dataset(AsymptoticCalculator::GenerateAsimovData(*model, observables));
1354+
std::unique_ptr<RooAbsData> asimov_dataset(AsymptoticCalculator::GenerateAsimovData(*model, observables));
13591355
proto.import(*asimov_dataset, RooFit::Rename("asimovData"));
13601356
}
13611357

@@ -1378,16 +1374,16 @@ RooArgList HistoToWorkspaceFactoryFast::createObservables(const TH1 *hist, RooWo
13781374

13791375
for(auto const& data : channel.GetAdditionalData()) {
13801376
if(data.GetName().empty()) {
1381-
cxcoutF(HistFactory) << "Error: Additional Data histogram for channel: " << channel.GetName()
1377+
cxcoutFHF << "Error: Additional Data histogram for channel: " << channel.GetName()
13821378
<< " 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;
1379+
<< "either via the \"Name\" tag in the XML or via RooStats::HistFactory::Data::SetName().\n";
13841380
throw hf_exc();
13851381
}
13861382
std::string const& dataName = data.GetName();
13871383
TH1 const* mnominal = data.GetHisto();
13881384
if( !mnominal ) {
1389-
cxcoutF(HistFactory) << "Error: Additional Data histogram for channel: " << channel.GetName()
1390-
<< " with name: " << dataName << " is nullptr" << std::endl;
1385+
cxcoutFHF << "Error: Additional Data histogram for channel: " << channel.GetName()
1386+
<< " with name: " << dataName << " is nullptr\n";
13911387
throw hf_exc();
13921388
}
13931389

@@ -1488,7 +1484,7 @@ RooArgList HistoToWorkspaceFactoryFast::createObservables(const TH1 *hist, RooWo
14881484
/// These things were used for debugging. Maybe useful in the future
14891485
//
14901486

1491-
map<string, RooAbsPdf*> pdfMap;
1487+
std::map<string, RooAbsPdf*> pdfMap;
14921488
vector<RooAbsPdf*> models;
14931489

14941490
RooArgList obsList;
@@ -1512,8 +1508,10 @@ RooArgList HistoToWorkspaceFactoryFast::createObservables(const TH1 *hist, RooWo
15121508
RooWorkspace * ch=chs[i].get();
15131509

15141510
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;
1511+
if(!model) {
1512+
cxcoutFHF << "failed to find model for channel\n";
1513+
throw hf_exc();
1514+
}
15171515
models.push_back(model);
15181516
globalObs.add(*ch->set("globalObservables"), /*silent=*/true); // silent because observables might exist in other channel.
15191517

@@ -1621,7 +1619,7 @@ RooArgList HistoToWorkspaceFactoryFast::createObservables(const TH1 *hist, RooWo
16211619
combined->import( *asimov_combined, RooFit::Rename("asimovData"));
16221620
}
16231621
else {
1624-
std::cout << "Error: Failed to create combined asimov dataset" << std::endl;
1622+
cxcoutFHF << "Error: Failed to create combined asimov dataset\n";
16251623
throw hf_exc();
16261624
}
16271625

@@ -1654,18 +1652,17 @@ RooArgList HistoToWorkspaceFactoryFast::createObservables(const TH1 *hist, RooWo
16541652

16551653
// Check that histError != NAN
16561654
if( histError != histError ) {
1657-
std::cout << "Warning: In histogram " << Nominal->GetName()
1655+
cxcoutFHF << "Warning: In histogram " << Nominal->GetName()
16581656
<< " bin error for bin " << i_bin
1659-
<< " is NAN. Not using Error!!!"
1660-
<< std::endl;
1657+
<< " is NAN. Not using Error!!!\n";
16611658
throw hf_exc();
16621659
//histError = sqrt( histContent );
16631660
//histError = 0;
16641661
}
16651662

16661663
// Check that histError ! < 0
16671664
if( histError < 0 ) {
1668-
std::cout << "Warning: In histogram " << Nominal->GetName()
1665+
cxcoutWHF << "Warning: In histogram " << Nominal->GetName()
16691666
<< " bin error for bin " << binNumber
16701667
<< " is < 0. Setting Error to 0"
16711668
<< std::endl;
@@ -1743,7 +1740,7 @@ RooArgList HistoToWorkspaceFactoryFast::createObservables(const TH1 *hist, RooWo
17431740
double histError = error->GetBinContent( binNumber );
17441741

17451742
if( histError != histError ) {
1746-
cxcoutE(HistFactory) << "In histogram " << error->GetName()
1743+
cxcoutFHF << "In histogram " << error->GetName()
17471744
<< " bin error for bin " << binNumber
17481745
<< " is NAN. Not using error!!";
17491746
throw hf_exc();
@@ -1813,6 +1810,4 @@ RooArgList HistoToWorkspaceFactoryFast::createObservables(const TH1 *hist, RooWo
18131810
}
18141811

18151812

1816-
} // namespace RooStats
1817-
} // namespace HistFactory
1818-
1813+
} // namespace RooStats::HistFactory

0 commit comments

Comments
 (0)