90 HistoToWorkspaceFactoryFast::Configuration
const &cfg)
92 std::unique_ptr<TFile> outFile;
93 std::ofstream tableFile;
98 cxcoutIHF <<
"Making Model and Measurements (Fast) for measurement: " << measurement.
GetName() << std::endl;
102 cxcoutIHF <<
"using lumi = " << measurement.
GetLumi() <<
" and lumiError = " << lumiError
103 <<
" including bins between " << measurement.
GetBinLow() <<
" and " << measurement.
GetBinHigh() << std::endl;
105 std::ostringstream parameterMessage;
106 parameterMessage <<
"fixing the following parameters:" << std::endl;
109 parameterMessage <<
" " << *itr <<
'\n';
111 cxcoutIHF << parameterMessage.str();
113 std::string rowTitle = measurement.
GetName();
115 std::vector<std::unique_ptr<RooWorkspace>> channel_workspaces;
116 std::vector<std::string> channel_names;
125 size_t pos = prefix.rfind(
'/');
126 if (pos != std::string::npos) {
127 std::string outputDir = prefix.substr(0,pos);
128 cxcoutDHF <<
"Checking if output directory : " << outputDir <<
" - exists" << std::endl;
130 cxcoutDHF <<
"Output directory : " << outputDir <<
" - does not exist, try to create" << std::endl;
133 std::string fullOutputDir = std::string(
gSystem->
pwd()) + std::string(
"/") + outputDir;
134 cxcoutEHF <<
"Error: Failed to make output directory: " << fullOutputDir << std::endl;
142 cxcoutIHF <<
"Creating the output file: " << outputFileName << std::endl;
143 outFile = std::make_unique<TFile>(outputFileName.c_str(),
"recreate");
146 tableFile.open(measurement.
GetOutputFilePrefix() +
"_results.table", std::ios::out | std::ios::app);
148 cxcoutIHF <<
"Creating the HistoToWorkspaceFactoryFast factory" << std::endl;
149 HistoToWorkspaceFactoryFast factory{measurement, cfg};
153 cxcoutIHF <<
"Setting preprocess functions" << std::endl;
157 tableFile <<
" " << rowTitle <<
" &";
160 for(
unsigned int chanItr = 0; chanItr < measurement.GetChannels().
size(); ++chanItr ) {
162 HistFactory::Channel& channel = measurement.GetChannels().at( chanItr );
163 if( ! channel.CheckHistograms() ) {
164 cxcoutEHF <<
"MakeModelAndMeasurementsFast: Channel: " << channel.
GetName()
165 <<
" has uninitialized histogram pointers" << std::endl;
170 std::string ch_name = channel.GetName();
171 cxcoutPHF <<
"Starting to process channel: " << ch_name << std::endl;
172 channel_names.push_back(ch_name);
173 std::unique_ptr<RooWorkspace> ws_single{factory.MakeSingleChannelModel( measurement, channel )};
175 if (cfg.createPerRegionWorkspaces) {
178 + ch_name +
"_" + rowTitle +
"_model.root";
179 cxcoutIHF <<
"Opening File to hold channel: " << ChannelFileName << std::endl;
180 std::unique_ptr<TFile> chanFile{
TFile::Open( ChannelFileName.c_str(),
"RECREATE" )};
185 meas_chan.GetChannels().clear();
186 meas_chan.GetChannels().push_back( channel );
187 cxcoutIHF <<
"About to write channel measurement to file" << std::endl;
188 meas_chan.writeToFile( chanFile.get() );
189 cxcoutPHF <<
"Successfully wrote channel to file" << std::endl;
196 if(! measurement.GetExportOnly()){
198 cxcoutWHF <<
"Can't do fit for: " << measurement.
GetName()
199 <<
", no parameter of interest" << std::endl;
201 if(ws_single->data(
"obsData")) {
203 ch_name,
"obsData", *outFile, tableFile);
206 ch_name,
"asimovData", *outFile, tableFile);
213 channel_workspaces.emplace_back(std::move(ws_single));
223 std::unique_ptr<RooWorkspace> ws{factory.MakeCombinedModel(channel_names, channel_workspaces)};
226 HistoToWorkspaceFactoryFast::ConfigureWorkspaceForMeasurement(
"simPdf", ws.get(), measurement);
233 + rowTitle +
"_model.root";
234 cxcoutPHF <<
"Writing combined workspace to file: " << CombinedFileName << std::endl;
235 std::unique_ptr<TFile> combFile{
TFile::Open( CombinedFileName.c_str(),
"RECREATE" )};
236 if( combFile ==
nullptr ) {
237 cxcoutEHF <<
"Error: Failed to open file " << CombinedFileName << std::endl;
240 combFile->WriteTObject(ws.get());
241 cxcoutPHF <<
"Writing combined measurement to file: " << CombinedFileName << std::endl;
242 measurement.writeToFile( combFile.get() );
246 if(! measurement.GetExportOnly()){
248 cxcoutWHF <<
"Can't do fit for: " << measurement.
GetName()
249 <<
", no parameter of interest" << std::endl;
252 if(ws->data(
"obsData")){
254 "obsData", *outFile, tableFile);
258 "asimovData", *outFile, tableFile);
263 tableFile <<
" \\\\ \n";
274 RooWorkspace &combined, std::string channel, std::string data_name,
275 TFile &outFile, std::ostream &tableStream)
279 auto combined_config =
static_cast<ModelConfig *
>(combined.
obj(
"ModelConfig"));
280 if(!combined_config){
281 cxcoutEHF <<
"Error: no ModelConfig found in Measurement: "
282 << MeasurementName << std::endl;
288 cxcoutEHF <<
"Error: Failed to get dataset: " << data_name
289 <<
" in measurement: " << MeasurementName << std::endl;
293 const RooArgSet* POIs = combined_config->GetParametersOfInterest();
295 cxcoutEHF <<
"Not Fitting Model for measurement: " << MeasurementName
296 <<
", no poi found" << std::endl;
301 RooAbsPdf* model = combined_config->GetPdf();
302 if( model==
nullptr ) {
303 cxcoutEHF <<
"Error: Failed to find pdf in ModelConfig: " << combined_config->
GetName()
310 if( combined_config->GetNuisanceParameters() ) {
311 PoiPlusNuisance.
add( *combined_config->GetNuisanceParameters() );
313 PoiPlusNuisance.
add( *combined_config->GetParametersOfInterest() );
314 combined.
saveSnapshot(
"InitialValues", PoiPlusNuisance);
318 cxcoutPHF <<
"\n---------------"
319 <<
"\nDoing "<< channel <<
" Fit"
320 <<
"\n---------------\n\n" << std::endl;
322 model->
fitTo(*simData, Minos(
true), PrintLevel(printLevel));
326 if( POIs->
empty() ) {
327 cxcoutWHF <<
"WARNING: No POIs found in measurement: " << MeasurementName << std::endl;
332 for (
auto const *poi : static_range_cast<RooRealVar *>(*POIs)) {
333 cxcoutIHF <<
"printing results for " << poi->
GetName()
334 <<
" at " << poi->
getVal()<<
" high "
344 const auto oldPrecision = tableStream.precision();
345 tableStream.precision(4);
347 tableStream.precision(oldPrecision);
350 std::unique_ptr<RooAbsReal> nll{model->
createNLL(*simData)};
351 std::unique_ptr<RooAbsReal> profile{nll->createProfile(*poi)};
353 std::unique_ptr<RooPlot> frame{poi->
frame()};
358 TCanvas profileLikelihoodCanvas{channel.c_str(),
"",800,600};
359 nll->plotOn(frame.get(), ShiftToZero(), LineColor(
kRed), LineStyle(
kDashed));
360 profile->plotOn(frame.get());
361 frame->SetMinimum(0);
362 frame->SetMaximum(2.);
364 std::string profilePlotName = FileNamePrefix+
"_"+channel+
"_"+MeasurementName+
"_profileLR.eps";
365 profileLikelihoodCanvas.SaveAs( profilePlotName.c_str() );
375 if( channel_dir ==
nullptr ) {
376 cxcoutEHF <<
"Error: Failed to make channel directory: " << channel << std::endl;
380 if( summary_dir ==
nullptr ) {
381 cxcoutEHF <<
"Error: Failed to make Summary directory for channel: "
382 << channel << std::endl;
390 double* curve_x=curve->
GetX();
392 std::vector<double> x_arr(curve_N);
393 std::vector<double> y_arr_nll(curve_N);
395 for(
int i=0; i<curve_N; i++){
399 y_arr_nll[i]=nll->getVal();
402 TGraph g{curve_N, x_arr.data(), y_arr_nll.data()};
403 g.SetName( (FileNamePrefix +
"_nll").c_str() );
418 cxcoutIHF <<
"In Fit Model" << std::endl;
420 if(!combined_config){
421 cxcoutEHF <<
"no model config " <<
"ModelConfig" <<
" exiting" << std::endl;
427 cxcoutEHF <<
"no data " << data_name <<
" exiting" << std::endl;
433 cxcoutEHF <<
"no poi " << data_name <<
" exiting" << std::endl;
438 model->
fitTo(*simData, Minos(
true), PrintLevel(1));
static void formatFrameForLikelihoodImpl(RooPlot *frame, std::string YTitle)
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
R__EXTERN TStyle * gStyle
R__EXTERN TSystem * gSystem
virtual bool add(const RooAbsArg &var, bool silent=false)
Add the specified argument to list.
RooAbsArg * first() const
Abstract base class for binned and unbinned datasets.
Abstract interface for all probability density functions.
RooFit::OwningPtr< RooAbsReal > createNLL(RooAbsData &data, CmdArgs_t const &... cmdArgs)
Construct representation of -log(L) of PDF with given dataset.
RooFit::OwningPtr< RooFitResult > fitTo(RooAbsData &data, CmdArgs_t const &... cmdArgs)
Fit PDF to given dataset.
Abstract base class for objects that represent a real value that may appear on the left hand side of ...
virtual double getMax(const char *name=nullptr) const
Get maximum of currently defined range.
virtual double getMin(const char *name=nullptr) const
Get minimum of currently defined range.
RooPlot * frame(const RooCmdArg &arg1, const RooCmdArg &arg2={}, const RooCmdArg &arg3={}, const RooCmdArg &arg4={}, const RooCmdArg &arg5={}, const RooCmdArg &arg6={}, const RooCmdArg &arg7={}, const RooCmdArg &arg8={}) const
Create a new RooPlot on the heap with a drawing frame initialized for this object,...
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
RooArgSet is a container object that can hold multiple RooAbsArg objects.
One-dimensional graphical representation of a real-valued function.
static RooMsgService & instance()
Return reference to singleton instance.
bool isActive(T self, RooFit::MsgTopic topic, RooFit::MsgLevel level)
Check if logging is active for given object/topic/RooFit::MsgLevel combination.
Plot frame and a container for graphics objects within that frame.
void SetTitle(const char *name) override
Set the title of the RooPlot to 'title'.
void addObject(TObject *obj, Option_t *drawOptions="", bool invisible=false)
Add a generic object to this plot.
RooAbsRealLValue * getPlotVar() const
Variable that can be changed from the outside.
void setVal(double value) override
Set value of variable to 'value'.
double getErrorLo() const
double getErrorHi() const
The RooStats::HistFactory::Measurement class can be used to construct a model by combining multiple R...
double GetLumiRelErr()
retrieve relative uncertainty on luminosity
std::vector< std::string > & GetConstantParams()
get vector of all constant parameters
std::string GetOutputFilePrefix()
retrieve prefix for output files
std::vector< std::string > GetPreprocessFunctions() const
Returns a list of defined preprocess function expressions.
double GetLumi()
retrieve integrated luminosity
std::string GetPOI(unsigned int i=0)
get name of PoI at given index
ModelConfig is a simple class that holds configuration information specifying how a model should be u...
const RooArgSet * GetParametersOfInterest() const
get RooArgSet containing the parameter of interest (return nullptr if not existing)
RooAbsPdf * GetPdf() const
get model PDF (return nullptr if pdf has not been specified or does not exist)
Persistable container for RooFit projects.
TObject * obj(RooStringView name) const
Return any type of object (RooAbsArg, RooAbsData or generic object) with given name)
bool saveSnapshot(RooStringView, const char *paramNames)
Save snapshot of values and attributes (including "Constant") of given parameters.
RooAbsData * data(RooStringView name) const
Retrieve dataset (binned or unbinned) with given name. A null pointer is returned if not found.
bool loadSnapshot(const char *name)
Load the values and attributes of the parameters in the snapshot saved with the given name.
virtual void SetLineColor(Color_t lcolor)
Set the line color.
TDirectory * mkdir(const char *name, const char *title="", Bool_t returnExistingDirectory=kFALSE) override
Create a sub-directory "a" or a hierarchy of sub-directories "a/b/c/...".
Int_t WriteTObject(const TObject *obj, const char *name=nullptr, Option_t *option="", Int_t bufsize=0) override
Write object obj to this directory.
Describe directory structure in memory.
virtual Bool_t cd()
Change current directory to "this" directory.
virtual TDirectory * mkdir(const char *name, const char *title="", Bool_t returnExistingDirectory=kFALSE)
Create a sub-directory "a" or a hierarchy of sub-directories "a/b/c/...".
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
A TGraph is an object made of two arrays X and Y with npoints each.
Use the TLine constructor to create a simple line.
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
const char * GetName() const override
Returns name of object.
const char * GetTitle() const override
Returns title of object.
void SetPadBorderMode(Int_t mode=1)
void SetCanvasBorderMode(Int_t mode=1)
virtual void * OpenDirectory(const char *name)
Open a directory. Returns 0 if directory does not exist.
virtual int MakeDirectory(const char *name)
Make a directory.
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
OwningPtr< T > makeOwningPtr(std::unique_ptr< T > &&ptr)
Internal helper to turn a std::unique_ptr<T> into an OwningPtr.
T * OwningPtr
An alias for raw pointers for indicating that the return type of a RooFit function is an owning point...
void FormatFrameForLikelihood(RooPlot *frame, std::string xTitle=std::string("#sigma / #sigma_{SM}"), std::string yTitle=std::string("-log likelihood")) R__DEPRECATED(6
void FitModelAndPlot(const std::string &measurementName, const std::string &fileNamePrefix, RooWorkspace &, std::string, std::string, TFile &, std::ostream &) R__DEPRECATED(6
RooFit::OwningPtr< RooWorkspace > MakeModelAndMeasurementFast(RooStats::HistFactory::Measurement &measurement, HistoToWorkspaceFactoryFast::Configuration const &cfg={})
void FitModel(RooWorkspace *, std::string data_name="obsData") R__DEPRECATED(6