100 std::unique_ptr<TFile> outFile;
101 FILE* tableFile=
nullptr;
108 cxcoutIHF <<
"Making Model and Measurements (Fast) for measurement: " << measurement.
GetName() << std::endl;
112 cxcoutIHF <<
"using lumi = " << measurement.
GetLumi() <<
" and lumiError = " << lumiError
113 <<
" including bins between " << measurement.
GetBinLow() <<
" and " << measurement.
GetBinHigh() << std::endl;
115 std::ostringstream parameterMessage;
116 parameterMessage <<
"fixing the following parameters:" << std::endl;
119 parameterMessage <<
" " << *itr <<
'\n';
123 std::string rowTitle = measurement.
GetName();
125 std::vector<std::unique_ptr<RooWorkspace>> channel_workspaces;
126 std::vector<std::string> channel_names;
135 size_t pos = prefix.rfind(
"/");
136 if (pos != std::string::npos) {
137 std::string outputDir = prefix.substr(0,pos);
138 cxcoutDHF <<
"Checking if output directory : " << outputDir <<
" - exists" << std::endl;
140 cxcoutDHF <<
"Output directory : " << outputDir <<
" - does not exist, try to create" << std::endl;
143 std::string fullOutputDir = std::string(
gSystem->
pwd()) + std::string(
"/") + outputDir;
144 cxcoutEHF <<
"Error: Failed to make output directory: " << fullOutputDir << std::endl;
152 cxcoutIHF <<
"Creating the output file: " << outputFileName << std::endl;
153 outFile = std::make_unique<TFile>(outputFileName.c_str(),
"recreate");
158 cxcoutIHF <<
"Creating the table file: " << tableFileName << std::endl;
159 tableFile = fopen( tableFileName.c_str(),
"a");
161 cxcoutIHF <<
"Creating the HistoToWorkspaceFactoryFast factory" << std::endl;
162 HistoToWorkspaceFactoryFast factory{measurement, cfg};
166 cxcoutIHF <<
"Setting preprocess functions" << std::endl;
170 fprintf(tableFile,
" %s &", rowTitle.c_str() );
173 for(
unsigned int chanItr = 0; chanItr < measurement.
GetChannels().
size(); ++chanItr ) {
175 HistFactory::Channel& channel = measurement.
GetChannels().at( chanItr );
176 if( ! channel.CheckHistograms() ) {
177 cxcoutEHF <<
"MakeModelAndMeasurementsFast: Channel: " << channel.GetName()
178 <<
" has uninitialized histogram pointers" << std::endl;
183 std::string ch_name = channel.GetName();
184 cxcoutPHF <<
"Starting to process channel: " << ch_name << std::endl;
185 channel_names.push_back(ch_name);
186 RooWorkspace* ws_single = factory.MakeSingleChannelModel( measurement, channel );
187 channel_workspaces.emplace_back(ws_single);
192 + ch_name +
"_" + rowTitle +
"_model.root";
193 cxcoutIHF <<
"Opening File to hold channel: " << ChannelFileName << std::endl;
194 std::unique_ptr<TFile> chanFile{
TFile::Open( ChannelFileName.c_str(),
"RECREATE" )};
199 meas_chan.GetChannels().clear();
200 meas_chan.GetChannels().push_back( channel );
201 cxcoutIHF <<
"About to write channel measurement to file" << std::endl;
202 meas_chan.writeToFile( chanFile.get() );
203 cxcoutPHF <<
"Successfully wrote channel to file" << std::endl;
213 <<
", no parameter of interest" << std::endl;
215 if(ws_single->
data(
"obsData")) {
217 ch_name,
"obsData", outFile.get(), tableFile);
220 ch_name,
"asimovData", outFile.get(), tableFile);
225 fprintf(tableFile,
" & " );
235 ws = factory.MakeCombinedModel(channel_names, channel_workspaces);
238 HistoToWorkspaceFactoryFast::ConfigureWorkspaceForMeasurement(
"simPdf", ws, measurement );
245 + rowTitle +
"_model.root";
246 cxcoutPHF <<
"Writing combined workspace to file: " << CombinedFileName << std::endl;
247 std::unique_ptr<TFile> combFile{
TFile::Open( CombinedFileName.c_str(),
"RECREATE" )};
248 if( combFile ==
nullptr ) {
249 cxcoutEHF <<
"Error: Failed to open file " << CombinedFileName << std::endl;
252 combFile->WriteTObject(ws);
253 cxcoutPHF <<
"Writing combined measurement to file: " << CombinedFileName << std::endl;
261 <<
", no parameter of interest" << std::endl;
264 if(ws->
data(
"obsData")){
266 "obsData", outFile.get(), tableFile);
270 "asimovData", outFile.get(), tableFile);
275 fprintf(tableFile,
" \\ \n");
281 if( tableFile ) fclose(tableFile);
294 const std::string& FileNamePrefix,
296 std::string data_name,
297 TFile* outFile, FILE* tableFile ) {
299 if( outFile ==
nullptr ) {
300 cxcoutEHF <<
"Error: Output File in FitModelAndPlot is nullptr" << std::endl;
304 if( tableFile ==
nullptr ) {
305 cxcoutEHF <<
"Error: tableFile in FitModelAndPlot is nullptr" << std::endl;
309 if( combined ==
nullptr ) {
310 cxcoutEHF <<
"Error: Supplied workspace in FitModelAndPlot is nullptr" << std::endl;
315 if(!combined_config){
316 cxcoutEHF <<
"Error: no ModelConfig found in Measurement: "
317 << MeasurementName << std::endl;
323 cxcoutEHF <<
"Error: Failed to get dataset: " << data_name
324 <<
" in measurement: " << MeasurementName << std::endl;
330 cxcoutEHF <<
"Not Fitting Model for measurement: " << MeasurementName
331 <<
", no poi found" << std::endl;
337 if( model==
nullptr ) {
338 cxcoutEHF <<
"Error: Failed to find pdf in ModelConfig: " << combined_config->
GetName()
349 combined->
saveSnapshot(
"InitialValues", PoiPlusNuisance);
354 <<
"\nDoing "<< channel <<
" Fit"
355 <<
"\n---------------\n\n" << std::endl;
360 if( POIs->
empty() ) {
361 cxcoutWHF <<
"WARNING: No POIs found in measurement: " << MeasurementName << std::endl;
366 for (
auto const *poi : static_range_cast<RooRealVar *>(*POIs)) {
368 <<
" at " << poi->
getVal()<<
" high "
381 std::unique_ptr<RooAbsReal> nll{model->
createNLL(*simData)};
382 std::unique_ptr<RooAbsReal> profile{nll->createProfile(*poi)};
383 if( profile==
nullptr ) {
384 cxcoutEHF <<
"Error: Failed to make ProfileLikelihood for: " << poi->
GetName()
385 <<
" using model: " << model->
GetName()
386 <<
" and data: " << simData->
GetName()
391 std::unique_ptr<RooPlot> frame{poi->
frame()};
396 TCanvas profileLikelihoodCanvas{channel.c_str(),
"",800,600};
398 profile->plotOn(frame.get());
399 frame->SetMinimum(0);
400 frame->SetMaximum(2.);
402 std::string profilePlotName = FileNamePrefix+
"_"+channel+
"_"+MeasurementName+
"_profileLR.eps";
403 profileLikelihoodCanvas.SaveAs( profilePlotName.c_str() );
413 if( channel_dir ==
nullptr ) {
414 cxcoutEHF <<
"Error: Failed to make channel directory: " << channel << std::endl;
418 if( summary_dir ==
nullptr ) {
419 cxcoutEHF <<
"Error: Failed to make Summary directory for channel: "
420 << channel << std::endl;
428 double* curve_x=curve->
GetX();
430 std::vector<double> x_arr(curve_N);
431 std::vector<double> y_arr_nll(curve_N);
433 for(
int i=0; i<curve_N; i++){
437 y_arr_nll[i]=nll->getVal();
440 TGraph g{curve_N, x_arr.data(), y_arr_nll.data()};
441 g.SetName( (FileNamePrefix +
"_nll").c_str() );
452 cxcoutIHF <<
"In Fit Model" << std::endl;
454 if(!combined_config){
455 cxcoutEHF <<
"no model config " <<
"ModelConfig" <<
" exiting" << std::endl;
461 cxcoutEHF <<
"no data " << data_name <<
" exiting" << std::endl;
467 cxcoutEHF <<
"no poi " << data_name <<
" exiting" << std::endl;
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
RooAbsData is the common abstract base class for binned and unbinned datasets.
virtual RooFit::OwningPtr< RooAbsReal > createNLL(RooAbsData &data, const RooLinkedList &cmdList={})
Construct representation of -log(L) of PDF with given dataset.
virtual RooFit::OwningPtr< RooFitResult > fitTo(RooAbsData &data, const RooLinkedList &cmdList={})
Fit PDF to given dataset.
RooAbsRealLValue is the common abstract base class for objects that represent a real value that may a...
virtual double getMax(const char *name=nullptr) const
Get maximum of currently defined range.
RooPlot * frame(const RooCmdArg &arg1, const RooCmdArg &arg2=RooCmdArg::none(), const RooCmdArg &arg3=RooCmdArg::none(), const RooCmdArg &arg4=RooCmdArg::none(), const RooCmdArg &arg5=RooCmdArg::none(), const RooCmdArg &arg6=RooCmdArg::none(), const RooCmdArg &arg7=RooCmdArg::none(), const RooCmdArg &arg8=RooCmdArg::none()) const
Create a new RooPlot on the heap with a drawing frame initialized for this object,...
virtual double getMin(const char *name=nullptr) const
Get minimum of currently defined range.
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
RooArgSet is a container object that can hold multiple RooAbsArg objects.
A RooCurve is a one-dimensional graphical representation of a real-valued function.
static RooMsgService & instance()
Return reference to singleton instance.
A RooPlot is a 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.
virtual void SetMinimum(double minimum=-1111)
Set minimum value of Y axis.
virtual void SetMaximum(double maximum=-1111)
Set maximum value of Y axis.
RooAbsRealLValue * getPlotVar() const
RooRealVar represents a 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...
void writeToFile(TFile *file)
A measurement, once fully configured, can be saved into a ROOT file.
double GetLumiRelErr()
retrieve relative uncertainty on luminosity
std::vector< std::string > & GetConstantParams()
get vector of all constant parameters
std::vector< RooStats::HistFactory::Channel > & GetChannels()
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)
const RooArgSet * GetNuisanceParameters() const
get RooArgSet containing the nuisance parameters (return nullptr if not existing)
RooAbsPdf * GetPdf() const
get model PDF (return nullptr if pdf has not been specified or does not exist)
The RooWorkspace is a 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.
RooRealVar * var(RooStringView name) const
Retrieve real-valued variable (RooRealVar) with given name. A null pointer is returned if not found.
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.
void Draw(Option_t *option="") override
Draw a canvas.
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 a suite of consecutive data records (TKey instances) with a well defined format.
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.
Mother of all ROOT objects.
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.
RooCmdArg Minos(bool flag=true)
RooCmdArg LineColor(Color_t color)
RooCmdArg LineStyle(Style_t style)
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
void FitModel(RooWorkspace *, std::string data_name="obsData")
void FormatFrameForLikelihood(RooPlot *frame, std::string xTitle=std::string("#sigma / #sigma_{SM}"), std::string yTitle=std::string("-log likelihood"))
void FitModelAndPlot(const std::string &measurementName, const std::string &fileNamePrefix, RooWorkspace *, std::string, std::string, TFile *, FILE *)
RooWorkspace * MakeModelAndMeasurementFast(RooStats::HistFactory::Measurement &measurement, HistoToWorkspaceFactoryFast::Configuration const &cfg={})