3.5.3 Examples of FW Lite Macros in 3.4.X And Earlier.

Complete: 2
Detailed Review status

This page is now deprecated. Please see WorkBookFWLiteExamples for the latest version.

Goals of this page:

In this page you will find working examples of FW Lite macros that can be used as starting points for your own analysis.

You can also visit FWLite Executable, an introduction to FWLite using executables.

Contents

Example 1: the Bare Bones -- Histogram on Muon Pt

/*   A macro for making a histogram of Muon Pt with cuts
This is a basic way to cut out muons of a certain Pt and Eta using an if statement
This example creates a histogram of Muon Pt, using Muons with Pt above 30 and ETA above -2.1 and below 2.1
*/

#include "DataFormats/FWLite/interface/Handle.h"
#include "DataFormats/FWLite/interface/Event.h"
#include "TFile.h"
#include "TH1.h"
#include "TCanvas.h"
#include "TLegend.h"


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

#include <iostream>
#include <cmath>      //necessary for absolute function fabs()

using namespace std;

void sk_fwlitecuts()
{
  TFile  * file = new TFile("PAT_ZMuMuRelVal_25K.root");
  TH1D * hist_muPt = new TH1D("hist_muPt", "Muon p_{T}", 20, 0, 100 );
  fwlite::Event ev(file);

  //loop through each event
  for( ev.toBegin();
         ! ev.atEnd();
         ++ev) {
    fwlite::Handle<std::vector<pat::Muon> > h_mu;
    h_mu.getByLabel(ev,"selectedLayer1Muons");
    if (!h_mu.isValid() ) continue;
    vector<pat::Muon> const & muons = *h_mu;

   //loop through each Muon
   vector<pat::Muon>::const_iterator iter;
   for ( iter = muons.begin(); iter != muons.end() ; ++iter) {
   
     if ( (iter->pt() > 30 ) && ( fabs(iter->eta() ) < 2.1)  )
      hist_muPt->Fill( iter->pt() );

   }   //end Muon loop   
   }   //end event loop

   hist_muPt->Draw();   
}

Example 2: Analyze Two Files With fwlite::!ChainEvent

/**
A macro for making a histogram from 2 seperate root files.
This example creates a histogram of muon pt, using muons from both files.
*/

#include "DataFormats/FWLite/interface/Handle.h"
#include "DataFormats/FWLite/interface/Event.h"
#include "TFile.h"
#include "TH1.h"
#include "TCanvas.h"
#include "TLegend.h"

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

#include <iostream>

using namespace std;

void sk_fwlite2files()
{
  vector<string> fileNames; //holds all the file names to be linked
  fileNames.push_back("PAT_ZMuMuRelVal_25K.root");
  fileNames.push_back("PAT_jPsiMuMuRelVal_25K.root");

  TH1D * hist_muPt = new TH1D("hist_muPt", "Muon p_{T}", 100, 0, 100 );
  
  //creates a ChainEvent allowing 2 files to be linked   
  fwlite::ChainEvent ev(fileNames);   
   
  for( ev.toBegin();
         ! ev.atEnd();
         ++ev) {
    fwlite::Handle<std::vector<pat::Muon> > h_mu;
    h_mu.getByLabel(ev,"selectedLayer1Muons");

    if ( !h_mu.isValid() ) continue;
    
    vector<pat::Muon> const & muons = *h_mu;
   
     vector<pat::Muon>::const_iterator iter;
     for ( iter = muons.begin(); iter != muons.end();  ++iter ) {
      hist_muPt->Fill( iter->pt() );
     }   //end Muon loop
   }   //end event loop
   hist_muPt->Draw();
}

Example 3: Four Histograms of Muon Mass, Charge, Energy, and Eta

/*   A macro for making a histogram of Muon Mass, Charge, Energy, and Eta
This example creates 4 histograms in one canvas of Muon Mass, Charge, Energy, and Eta
*/

#include "DataFormats/FWLite/interface/Handle.h"
#include "DataFormats/FWLite/interface/Event.h"
#include "TFile.h"
#include "TH1.h"
#include "TCanvas.h"
#include "TLegend.h"

#if !defined(__CINT__) && !defined(__MAKECINT__)   //necessary to avoid repeats
#include "DataFormats/PatCandidates/interface/Muon.h"   
#endif

#include <iostream>
using namespace std;

void sk_fwlite_MuonInfo()
{

  TFile  * file = new TFile("PAT_ZMuMuRelVal_25K.root");
  
  using namespace std;

   //creating a 1 dimensional histogram for each Muon attribute
   TH1D * hist_Mass = new TH1D("hist_Mass", "Muon Mass", 100, 0, .25 );   
   TH1D * hist_Charge = new TH1D("hist_Charge", "Muon Charge", 100, -2, 2 );
   TH1D * hist_Energy = new TH1D("hist_Energy", "Muon Energy", 100, 0, 200 );
   TH1D * hist_Eta = new TH1D("hist_Eta", "Muon Eta", 100, -3, 3 );


  fwlite::Event ev(file);
  for( ev.toBegin();
          ! ev.atEnd();
          ++ev) {

        fwlite::Handle<std::vector<pat::Muon> > h_muon;
        h_muon.getByLabel(ev,"selectedLayer1Muons");    
   if( !h_muon.isValid() ) continue;
   vector<pat::Muon> const & muon = *h_muon;

   vector<pat::Muon>::const_iterator iter;     
       for ( iter= muon.begin(); iter != muon.end();  ++iter ) {
   hist_Mass->Fill( iter->mass() );  
   hist_Charge->Fill( iter->charge() );
   hist_Energy->Fill( iter->energy() );
   hist_Eta->Fill( iter->eta() );
   } //end Muon loop

 } //end event loop     
   //creates the canvas and prints 4 histograms
   TCanvas *c1 = new TCanvas("Muon","Muon");
      c1->Divide(2,2);
      c1->cd(1);
      hist_Mass->Draw();
      c1->cd(2);   
      hist_Charge->Draw(); 
      c1->cd(3);
      hist_Energy->Draw();
      c1->cd(4);
      hist_Eta->Draw();
}

Example 4: Angle Between Jets and a Muon, Multiple Histograms

/**
A macro for for calculating the angle between jets and a muon.
This example calculates the angle (delta phi) between a muon and a jet,
a muon and a second jet, and two jets.
*/


#include "DataFormats/FWLite/interface/Handle.h"
#include "DataFormats/FWLite/interface/Event.h"
#include "TH1.h"
#include "TFile.h"
#include "TCanvas.h"
#include "TLegend.h"

#if !defined(__CINT__) && !defined(__MAKECINT__)
#include "DataFormats/PatCandidates/interface/Jet.h"
#include "DataFormats/PatCandidates/interface/Muon.h"
#include "DataFormats/Math/interface/deltaPhi.h"
#endif

#include <iostream>

using namespace std;

void sk_fwlite_JetDeltaPhi()
{
     TFile * file = new TFile("PAT_ZMuMuRelVal_25K.root");
   
   TH1D * hist_MuonJet1 = new TH1D("hist_MuonJet1", "Phi between Muon and Jet 1", 100, 0, 3.5); 
   TH1D * hist_MuonJet2 = new TH1D("hist_MuonJet2", "Phi between Muon and Jet 2", 100, 0, 3.5);
   TH1D * hist_Jet1Jet2 = new TH1D("hist_Jet1Jet2", "Phi between Jet 1 and Jet 2", 100, 0, 3.5);

   fwlite::Event ev(file);
   for( ev.toBegin();
      ! ev.atEnd();
      ++ev) {

   fwlite::Handle<std::vector<pat::Muon> > h_muon;
   h_muon.getByLabel(ev,"selectedLayer1Muons");
        if ( !h_muon.isValid() ) continue;

   fwlite::Handle<std::vector<pat::Jet> > h_jet;
   h_jet.getByLabel(ev,"selectedLayer1Jets");
   if ( !h_jet.isValid() ) continue;
   
        vector<pat::Jet> const & jet = *h_jet;
        vector<pat::Muon> const & muon = *h_muon;

   if ( (h_muon->size() > 0) && (h_jet->size() > 1) ) {  //necessary check
   
      double phi1, phi2, phi3;
      
      //Calculates the difference in angle
      phi1 = deltaPhi( muon[0].phi(), jet[0].phi() );
      phi2 = deltaPhi( muon[0].phi(), jet[1].phi() );
      phi3 = deltaPhi( jet[0].phi(), jet[1].phi() );

      hist_MuonJet1->Fill(phi1);
      hist_MuonJet2->Fill(phi2);
      hist_Jet1Jet2->Fill(phi3);

      } //end if loop
   
   } //end event loop

   TCanvas *delPhi = new TCanvas("deltaPhi", "deltaPhi");
      delPhi->Divide(1,3);
      delPhi->cd(1);
      hist_MuonJet1->Draw();
      delPhi->cd(2);
      hist_MuonJet2->Draw();
      delPhi->cd(3);
      hist_Jet1Jet2->Draw();
}

Example 5: Jet Analysis: Pt Cuts on Jet Energy, Invariant Mass of Dijets

/**
A macro for applying a pt cut on jets and finding invariant mass.
This examples creates two histograms.  The first histogram shows the 
invariant mass of two jets, and the second histogram shows the pt of jets,
after applying a cut.
*/


#include "DataFormats/FWLite/interface/Handle.h"
#include "DataFormats/FWLite/interface/Event.h"
#include "TH1.h"
#include "TFile.h"
#include "TCanvas.h"
#include "TLegend.h"
#include <TLorentzVector.h>

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

#include <iostream>

using namespace std;

void sk_fwlite_JetInvariantMass()
{
     TFile * file = new TFile("PAT_ZMuMuRelVal_25K.root");
   
   TH1D * hist_invarMass = new TH1D("hist_invarMass", "Invariant Mass of the 4-vector sum of Two Jets", 100, 0, 200);
   TH1D * hist_jets = new TH1D("hist_jets", "Jets with Pt above cut", 100, 0, 200);


   fwlite::Event ev(file);
   for( ev.toBegin();
      ! ev.atEnd();
      ++ev) {

   fwlite::Handle<std::vector<pat::Jet> > h_jet;
   h_jet.getByLabel(ev,"selectedLayer1Jets");
   if ( !h_jet.isValid() ) continue;

        vector<pat::Jet> const & jets= *h_jet;

   // Calculates the Invariant Mass of the two jets with the hightest Pt
   if (h_jet->size() > 1) {
   
      TLorentzVector jet1(jets[0].px(), jets[0].py(), jets[0].pz(), jets[0].energy() );
      TLorentzVector jet2(jets[1].px(), jets[1].py(), jets[1].pz(), jets[1].energy() );

      double result = (jet1 + jet2).Mag();

      hist_invarMass->Fill(result);

      } //end if loop
   
   // Applies a cut on the collection of jets and prints the rest to the histogram
   vector<pat::Jet>::const_iterator iter;
   for ( iter = jets.begin(); iter != jets.end(); ++iter) {
      
      if (iter->pt() > 50)
         hist_jets->Fill( iter->pt() );
       } // end jet for loop      
   } //end event loop

   TCanvas *invariantMass = new TCanvas("invariantMass", "invariantMass");
      invariantMass->Divide(1,2);
      invariantMass->cd(1);
      hist_invarMass->Draw();
      invariantMass->cd(2);
      hist_jets->Draw();
}

Example 6: A More Thoroughly Commented Macro

/**
A macro for plotting the transverse momentum of muons in your dataset.
The comments include some C++ tips.
*/

/**
These are for fwlite::Handle and fwlite::Event.
*/
#include "DataFormats/FWLite/interface/Handle.h"
#include "DataFormats/FWLite/interface/Event.h"

/**
Make sure you include all the ROOT classes you need. :)
*/
#include "TFile.h"
#include "TH1.h"
#include "TCanvas.h"
#include "TLegend.h"

/**
Because CINT, the C++ interpreter for ROOT, cannot compile some header files,
we put such files within the following statement.
*/
#if !defined(__CINT__) && !defined(__MAKECINT__)
#include "DataFormats/PatCandidates/interface/Muon.h"
#endif

#include <iostream>

using namespace std;

/**
You should give the function the same name as the file.
*/
void sk_fwlite()
{

  /**
  Call your data file.
  */
  TFile  * file = new TFile("PAT_ZMuMuRelVal_25K.root");

  using namespace std;

  TH1D * hist_muPt = new TH1D("hist_muPt", "Muon p_{T}", 20, 0, 100 );

  /**
  Create a fwlite::Event.
  */
  fwlite::Event ev(file);

  /**
  Run through your Event.
  */
  for( ev.toBegin();
          ! ev.atEnd();
          ++ev) {

    /**
    Get the class you want to use (e.g., Muon).
    */
    
    fwlite::Handle<std::vector<pat::Muon> > h_mu;
    h_mu   .getByLabel(ev,"selectedLayer1Muons");

    /**
    Just to be sure that what you're dereferencing actually exists,
    include this line:
    */
    if ( !h_mu.isValid() ) continue;

    /**
    Assign the vector you want.
    */
    vector<pat::Muon> const & muons = *h_mu;

    /**
    Get the data. Use an unsigned integer index so ROOT doesn't give warnings 
    when you compile.
    */

     for ( unsigned int i = 0; i < h_mu->size();  ++i ) {
       hist_muPt->Fill( muons[i].pt() );
     }
   }
   hist_muPt->Draw();
}

Example 7: Access to Runs, LuminosityBlocks, and their products

This macro is a demonstration of the various ways to get at luminosity block and run information.

#if !defined(__CINT__) && !defined(__MAKECINT__)
#include "DataFormats/FWLite/interface/Handle.h"
#include "DataFormats/FWLite/interface/Event.h"

#include "DataFormats/FWLite/interface/LuminosityBlock.h"
#include "DataFormats/FWLite/interface/Run.h"
#include "DataFormats/Luminosity/interface/LumiSummary.h"
#endif

#include "TFile.h"


void print_data() {
   TFile file("SomeData.root");

   fwlite::Event ev(&file);
   fwlite::Handle<LumiSummary> summary;

   // We can get run and luminosity blocks from events as well as associated products
   // All this works for ChainEvent and MultiChainEvent as well, not just on one  
   for(ev.toBegin(); !ev.atEnd(); ++ev) {
      // We can get the Luminosity block ID from the event
      std::cout << " Luminosity ID " << ev.getLuminosityBlock().id() << std::endl;
      // or the Run ID from the event
      std::cout <<" Run ID " << ev.getRun().id()<< std::endl;
      // We can get the Run ID from the luminosity block we got from the event
      std::cout << "Run via lumi " << ev.getLuminosityBlock().getRun().id() << std::endl;
      // We can get the integrated luminosity (or any luminosity product) from the event
      summary.getByLabel(ev.getLuminosityBlock(),"lumiProducer");
      std::cout << " Inst.  Luminosity = " << summary->avgInsDelLumi()<<std::endl;
   }

   // We can also loop over luminosity blocks themselves, same as events
   fwlite::LuminosityBlock ls(&file);
   for(ls.toBegin(); !ls.atEnd(); ++ls) {
      summary.getByLabel(ls,"lumiProducer");
      std::cout  << ls.id() << " Inst.  Luminosity = " << summary->avgInsDelLumi() << std::endl;
      // And get the associated run from this lumi
      std::cout << "Run from lumi " << ls.getRun().id() << std::endl;
   }

   // And of course do the same for runs.
   fwlite::Run r(&file);
   for(r.toBegin(); !r.atEnd(); ++r) {
      std::cout << "Run " << r.id() <<std::endl;
   }
}

Review status

Reviewer/Editor and Date (copy from screen) Comments
YourName - 14 Mar 2006 What you did (e.g., created page, reviewed, edited, added/answered questions or comments, etc.) and/or what you think needs to be done next (e.g., "needs review by ...", "still needs content on subject x", etc.)

Responsible: PetarMaksimovic
Last reviewed by: BenediktHegner - 21 Jun 2009

-- BenediktHegner - 21 Jun 2009

-- SalvatoreRoccoRappoccio - 29-Mar-2010

Edit | Attach | Watch | Print version | History: r1 | Backlinks | Raw View | WYSIWYG | More topic actions
Topic revision: r1 - 2010-03-29 - SalvatoreRRappoccio
 
    • 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