PAT Examples: FWLite Example in a Compiled Analyzer



In this example you will find how to access the pat::Muon collection within a simple CMS.FWLiteAnalyzer. You can find the CMS.FWLiteAnalyzer in the bin directory of the PatExamples package. It will provide you with the following information:

  • how to create an executable within a CMSSW package.
  • how to book and fill histograms with plain root.
  • how to access and loop the pat::Muon collection within a CMS.FWLiteAnalyzer.

ALERT! Notes:

  • This example is based on the PAT tutorials.
  • FWLite is not supported in a non-compiled CINT macro.

If you are using this for the PAT tutorials, use these tags:

Tutorial Branch cvs tag
CMSSW_2_2_X patTutorial_june09_module1
CMSSW_3_1_X patTutorial_sept09_module1
CMSSW_3_3_X patTutorial_dec09_module1
CMSSW_3_5_X patTutorial_mar10_module1

For at-home users, please see here for most up-to-date prescriptions! This tutorial assumes you are using the 3_5_X branch.

In the following we assume that the CMSSW environment is setup (cmsenv) and you are in the $CMSSW_BASE/src directory.

How to get the code

To run the example follow the procedure here:

How to compile and run the code

Warning, important Note: For this example you will need to have produced PATLayer1_Output.fromAOD_full.root (see WorkBookPATAccessExercise fro more details).

Compile and run the example as given below:

scram b 

ALERT! Note: You might have to rehash after compiling to get the executable tab completion. Instead of rehash you also just retype cmsenv in your working directory.

Find out more about the details

You can find the compilation description for the executable in the bin/CMS.BuildFile:

<use   name="DataFormats/PatCandidates"/>
<use   name="DataFormats/FWLite"/>
<use   name="FWCore/Framework"/>
<use   name="FWCore/FWLite"/>
<use   name="rootcintex"/>
<use   name="root"/>
<use   name="boost"/>
<use   name="CommonTools/Utils"/>
<use   name="CMS.PhysicsTools/Utilities"/>
<use   name="CMS.PhysicsTools/PatUtils"/>
<use   name="CMS.PhysicsTools/PatExamples"/>
<use   name="CMS.PhysicsTools/FWLite"/>
 <bin file=""> </bin>

The CMS.FWLiteAnalyzer itself only consists of a main function. In the first part the histograms are booked and the file is opened:

// ----------------------------------------------------------------------
  // First Part: 
  //  * enable the AutoLibraryLoader 
  //  * book the histograms of interest 
  //  * open the input file
  // ----------------------------------------------------------------------

  // load framework libraries
  gSystem->Load( "libFWCoreFWLite" );
  // book a set of histograms
  fwlite::TFileService fs = fwlite::TFileService("analyzePatBasics.root");
  TFileDirectory theDir = fs.mkdir("analyzeBasicPat");
  TH1F* muonPt_  = theDir.make<TH1F>("muonPt", "pt",    100,  0.,300.);
  TH1F* muonEta_ = theDir.make<TH1F>("muonEta","eta",   100, -3.,  3.);
  TH1F* muonPhi_ = theDir.make<TH1F>("muonPhi","phi",   100, -5.,  5.);  
  // open input file (can be located on castor)
  TFile* inFile = TFile::Open( "file:PATLayer1_Output.fromAOD_full.root" );

ALERT! Note: Before doing anything you always have to enable the AutoLibraryLoader. Take care where you book the histograms not to get mixed up with root file directories. In this example we book the histograms before opening any file. Then we open the input file, loop the events and close it again.

In the second part the events are looped, within one event the pat::Muon collection for cleanPatMuons is received from the input file and the muon collection is looped. A few simple histograms are filled. After the loop the input file is closed again:

// ----------------------------------------------------------------------
  // Second Part: 
  //  * loop the events in the input file 
  //  * receive the collections of interest via fwlite::Handle
  //  * fill the histograms
  //  * after the loop close the input file
  // ----------------------------------------------------------------------

  // loop the events
  unsigned int iEvent=0;
  fwlite::Event ev(inFile);
  for(ev.toBegin(); !ev.atEnd(); ++ev, ++iEvent){
    edm::EventBase const & event = ev;

    // break loop after end of file is reached 
    // or after 1000 events have been processed
    if( iEvent==1000 ) break;
    // simple event counter
    if(iEvent>0 && iEvent%1==0){
      std::cout << "  processing event: " << iEvent << std::endl;

    // Handle to the muon collection
    edm::Handle<std::vector<pat::Muon> > muons;
    edm::InputTag muonLabel("cleanPatMuons");
    event.getByLabel(muonLabel, muons);
    // loop muon collection and fill histograms
    for(unsigned i=0; i<muons->size(); ++i){
      muonPt_ ->Fill( (*muons)[i].pt()  );
      muonEta_->Fill( (*muons)[i].eta() );
      muonPhi_->Fill( (*muons)[i].phi() );
  // close input file

ALERT! Note: The fwlite::Handle looks very similar to the edm::Handle. The functionality is all the same. The pat::Muon collection was just taken as an example. For any other collection you can proceed in a similar way. Have a look to WorkBookPATDataFormats to find out what collection labels are available (after running the default workflow). Have a look to WorkBookPATWorkflow to find out how they are produced and what they mean. As this is a very simple example we stop after 1000 events.

In the third part a new output file is opened and the histograms are written to file:

// ----------------------------------------------------------------------
  // Third Part: 
  //  * open the output file 
  //  * write the histograms to the output file
  //  * close the output file
  // ----------------------------------------------------------------------
  //open output file
  TFile outFile( "analyzePatBasics.root", "recreate" );
  muonPt_ ->Write( );
  muonEta_->Write( );
  muonPhi_->Write( );

ALERT! Note: We also create a directory in the root file. In this way the file structure is the same as for the WorkBookPATAccessExercise.

Finally you should never forget to free the allocated memory, there's nothing to be done here though when using the TFileService:

// ----------------------------------------------------------------------
  // Third Part: 
  //  * never forget to free the memory of objects you created
  // ----------------------------------------------------------------------

  // in this example there is nothing to do 
  // that's it!
  return 0;

ALERT! Note: Make sure that the executable contains all header files you need. Also do not forget to re-compiled whenever you change something (like the name of the input file).

How to get more information

To learn more about creating and using FWLite executables have a look at WorkBookFWLiteExecutable. You have more questions? Don't hesitate to contact the SWGuidePAT#Support.

Review status

Reviewer/Editor and Date (copy from screen) Comments
RogerWolf - 06 Mar 2010 Updated to CMSSW_3_5_X

Responsible: RogerWolf
Last reviewed by: most recent reviewer and date

Edit | Attach | Watch | Print version | History: r21 < r20 < r19 < r18 < r17 | Backlinks | Raw View | WYSIWYG | More topic actions
Topic revision: r21 - 2010-09-16 - ColinBernet



    • 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-2021 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
or Ideas, requests, problems regarding TWiki? use Discourse or Send feedback