Skip to content

Commit

Permalink
style(code): better formatting of C++ ROOT macros (reanahub#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiborsimko committed Nov 21, 2024
1 parent 4529746 commit 1a3047f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 61 deletions.
53 changes: 26 additions & 27 deletions code/fitdata.C
Original file line number Diff line number Diff line change
@@ -1,55 +1,54 @@
#ifndef __CINT__
#include "RooGlobalFunc.h"
#endif
#include "RooRealVar.h"
#include "RooDataSet.h"
#include "RooGaussian.h"
#include "RooChebychev.h"
#include "RooAddPdf.h"
#include "RooChebychev.h"
#include "RooDataSet.h"
#include "RooExtendPdf.h"
#include "TCanvas.h"
#include "TAxis.h"
#include "RooGaussian.h"
#include "RooPlot.h"
using namespace RooFit ;
#include "RooRealVar.h"
#include "TAxis.h"
#include "TCanvas.h"
using namespace RooFit;

void fitdata(const char* input, const char* output)
{
void fitdata(const char *input, const char *output) {
// Open input file with workspace (generated by rf14_wspacewrite)
TFile *f = new TFile(input) ;
TFile *f = new TFile(input);

// Retrieve workspace from file
RooWorkspace* w = (RooWorkspace*) f->Get("w") ;
RooWorkspace *w = (RooWorkspace *)f->Get("w");

// Retrieve x,model and data from workspace
RooRealVar* x = w->var("x") ;
RooAbsPdf* model = w->pdf("model") ;
RooAbsData* data = w->data("modelData") ;
RooRealVar *x = w->var("x");
RooAbsPdf *model = w->pdf("model");
RooAbsData *data = w->data("modelData");

// Fit model to data, extended ML term automatically included
model->fitTo(*data) ;
model->fitTo(*data);

// Plot data and PDF overlaid
RooPlot* xframe = x->frame(Title("Fit example")) ;
data->plotOn(xframe) ;
model->plotOn(xframe,Normalization(1.0,RooAbsReal::RelativeExpected)) ;
RooPlot *xframe = x->frame(Title("Fit example"));
data->plotOn(xframe);
model->plotOn(xframe, Normalization(1.0, RooAbsReal::RelativeExpected));

// Overlay the background component of model with a dashed line
model->plotOn(xframe,Components("bkg"),LineStyle(kDashed),Normalization(1.0,RooAbsReal::RelativeExpected)) ;
model->plotOn(xframe, Components("bkg"), LineStyle(kDashed),
Normalization(1.0, RooAbsReal::RelativeExpected));

// Overlay the background components of model with a dotted line
//model->plotOn(xframe,Components(RooArgSet("bkg")),LineStyle(kDotted),Normalization(1.0,RooAbsReal::RelativeExpected)) ;
// model->plotOn(xframe,Components(RooArgSet("bkg")),LineStyle(kDotted),Normalization(1.0,RooAbsReal::RelativeExpected))
// ;

// Print structure of composite p.d.f.
//model.Print("t") ;
// Draw the frame on the canvas
TCanvas res("rf202_composite","rf202_composite",600,600) ;
gPad->SetLeftMargin(0.15) ;
xframe->GetYaxis()->SetTitleOffset(1.4) ;
// model.Print("t") ;
// Draw the frame on the canvas
TCanvas res("rf202_composite", "rf202_composite", 600, 600);
gPad->SetLeftMargin(0.15);
xframe->GetYaxis()->SetTitleOffset(1.4);
xframe->Draw();

res.Update();
res.SaveAs(output);
res.Close();


}
70 changes: 36 additions & 34 deletions code/gendata.C
Original file line number Diff line number Diff line change
@@ -1,57 +1,59 @@
#ifndef __CINT__
#include "RooGlobalFunc.h"
#endif
#include "RooRealVar.h"
#include "RooDataSet.h"
#include "RooGaussian.h"
#include "RooChebychev.h"
#include "RooAddPdf.h"
#include "RooChebychev.h"
#include "RooDataSet.h"
#include "RooExtendPdf.h"
#include "TCanvas.h"
#include "TAxis.h"
#include "RooGaussian.h"
#include "RooPlot.h"
using namespace RooFit ;
#include "RooRealVar.h"
#include "TAxis.h"
#include "TCanvas.h"
using namespace RooFit;

void gendata(int numevents, const char* outfilename)
{
// Declare observable x
RooRealVar x("x","x",0,10) ;
void gendata(int numevents, const char *outfilename) {
// Declare observable x
RooRealVar x("x", "x", 0, 10);

// Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their parameters
RooRealVar mean("mean","mean of gaussians",5) ;
RooRealVar sigma1("sigma1","width of gaussians",0.5) ;
// Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their
// parameters
RooRealVar mean("mean", "mean of gaussians", 5);
RooRealVar sigma1("sigma1", "width of gaussians", 0.5);
// RooRealVar sigma2("sigma2","width of gaussians",1) ;

RooGaussian sig1("sig1","Signal component 1",x,mean,sigma1) ;
//RooGaussian sig2("sig2","Signal component 2",x,mean,sigma2) ;
RooGaussian sig1("sig1", "Signal component 1", x, mean, sigma1);
// RooGaussian sig2("sig2","Signal component 2",x,mean,sigma2) ;

// Build Chebychev polynomial p.d.f.
RooRealVar a0("a0","a0",0.5,0.,1.) ;
RooRealVar a1("a1","a1",-0.2,0.,1.) ;
RooChebychev bkg("bkg","Background",x,RooArgSet(a0,a1)) ;
RooRealVar a0("a0", "a0", 0.5, 0., 1.);
RooRealVar a1("a1", "a1", -0.2, 0., 1.);
RooChebychev bkg("bkg", "Background", x, RooArgSet(a0, a1));

// Sum the signal components into a composite signal p.d.f.
RooRealVar sig1frac("sig1frac","fraction of component 1 in signal",0.8,0.,1.) ;
//RooAddPdf sig("sig","Signal",RooArgList(sig1,sig2),sig1frac) ;
RooAddPdf sig("sig","Signal",RooArgList(sig1),sig1frac) ;
RooRealVar sig1frac("sig1frac", "fraction of component 1 in signal", 0.8, 0.,
1.);
// RooAddPdf sig("sig","Signal",RooArgList(sig1,sig2),sig1frac) ;
RooAddPdf sig("sig", "Signal", RooArgList(sig1), sig1frac);

// Sum the composite signal and background into an extended pdf nsig*sig+nbkg*bkg
RooRealVar nsig("nsig","number of signal events",500,0.,10000) ;
RooRealVar nbkg("nbkg","number of background events",500,0,10000) ;
RooAddPdf model("model","(g1+g2)+a",RooArgList(bkg,sig),RooArgList(nbkg,nsig)) ;
// Sum the composite signal and background into an extended pdf
// nsig*sig+nbkg*bkg
RooRealVar nsig("nsig", "number of signal events", 500, 0., 10000);
RooRealVar nbkg("nbkg", "number of background events", 500, 0, 10000);
RooAddPdf model("model", "(g1+g2)+a", RooArgList(bkg, sig),
RooArgList(nbkg, nsig));

RooDataSet *data = model.generate(x, numevents) ;
RooDataSet *data = model.generate(x, numevents);

// Create a new workspace
RooWorkspace *w = new RooWorkspace("w","workspace") ;
w->import(model) ;
w->import(*data) ;
RooWorkspace *w = new RooWorkspace("w", "workspace");
w->import(model);
w->import(*data);

// Print workspace contents
w->Print() ;
w->Print();
// Save the workspace into a ROOT file
w->writeToFile(outfilename) ;
w->writeToFile(outfilename);
// Workspace will remain in memory after macro finishes
gDirectory->Add(w) ;

gDirectory->Add(w);
}

0 comments on commit 1a3047f

Please sign in to comment.