CMS.RooFit in FWLite

Complete: 3

  • How to load and from FWLite:

  • How to fill a RooDataSet from PAT objects: once we are up and running in FWLite and can access CMS.RooFit, we can use CMS.RooFit to actually do something. There are several ways of doing that:
    • Iterate over PAT files, make histograms (like in StarterKit), and then create RooDataHist objects from TH1F's and fit and plot them in CMS.RooFit
    • Alternatively, we could iterate over the PAT collections in FWLite, and make a RooDataSet (which is like an ntuple), which we can then manipulate further in CMS.RooFit (e.g. in an unbinned likelihood fit)

Using FWLite and RooFit for a toy analysis

This is brief macro demonstrating what you have to do to run RooFit in FWLite. To run the macro, you need have the StarterKit Skim file in your path. You can use other data, but you may need to modify parts of the code appropriately (specifically the file and the getByLabel lines)

#include "DataFormats/FWLite/interface/Handle.h"

#if !defined(__CINT__) && !defined(__MAKECINT__)
#include "DataFormats/PatCandidates/interface/Muon.h"
#include "DataFormats/PatCandidates/interface/Tau.h"

#include <iostream>
#include <TFile.h>
#include <TH1D.h>
#include "BigHeader.h"
//We declare using for the specific things that we need to use to prevent polluting the namespace
using std::cout;
using std::vector;
using pat::Muon;
using pat::Tau;
using fwlite::Handle;
using fwlite::Event;
using namespace CMS.RooFit;

void print_plot(RooDataSet *dataSet, RooRealVar *printVar, const char* outname)
  RooPlot *genericFrame = printVar->frame();
  TCanvas genericCanvas;

void RooFWLite()
  //FWLite Stuff (open a file and get events from it)
  TFile  *file = new TFile("/uscms_data/d1/dbjergaa/part1_PatAnalyzerKitSkim.root");
  Event ev(file);
  //CMS.RooFit Stuff (variables, dataset, argument set)
  RooRealVar muPt("muPt", "Muon Transverse momentum", 0, 100, "GeV/c");
  RooRealVar muEta("muEta","Muon Eta",-3.1415,3.1415,"Rad");
  RooRealVar tauPt("tauPt","Tau Transverse momentum",0,100,"GeV/c");
  RooRealVar tauEta("tauEta","Tau Eta",-3.1415,3.1415,"Rad");

  RooArgSet varSet( muPt, muEta, tauPt, tauEta);
  RooDataSet toyDataSet("toyDataSet", "Taus and Muons", varSet);

  //Event Loop
  for( ev.toBegin(); !ev.atEnd(); ++ev) 
      Handle<vector<Muon> > h_mu;
      Handle<vector<Tau> > h_tau;
      //loop over each muon and get required numbers
      for ( unsigned int i = 0; i < h_mu.ptr()->size();  ++i ) 
     vector<Muon> const & muons = *h_mu;
          //update dataset with the latest data
      //loop over taus and get information
      for (unsigned int i = 0; i < h_tau.ptr()->size(); ++i)
     vector<Tau> const & taus = *h_tau;


  //Save plots of the data (and eventually fitting)

This will print 4 plots using the print_plot function which create plots to use in a talk etc. The quality of these plots are not that great by default, but it wouldn't be hard to implement a custom style to produce more attractive plots. To run this macro do:
$ root -l -b
root[] .L RooFWLite.C++
(compile output)
root[]  RooFWLite();

-- DavidBjergaard - 06 Jan 2009

-- PetarMaksimovic - 06 Jan 2009

Edit | Attach | Watch | Print version | History: r1 | Backlinks | Raw View | WYSIWYG | More topic actions
Topic revision: r1 - 2009-05-14 - LucaLista
    • Cern Search Icon Cern Search
    • TWiki Search Icon TWiki Search
    • Google Search Icon Google Search

    CMSPublic All webs login

This site is powered by the TWiki collaboration platform Powered by PerlCopyright & 2008-2019 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback