Compiled Root Analysis

Complete: 0

This page is obsolete. Please see the WorkBook for updated examples.


Compiled ROOT applications allow more stability than interactive macros. Compiler are usually more effective in debugging C++ code than the CINT interpreter. Moreover, external libraries can be linked in a compiled application. If the application is compiled within CMSSW, libraries from a CMSSW release can be used in batch ROOT application.

Writing a Compiled ROOT Application under CMSSW

The following example shows how to write a simple ROOT batch analysis application that analyzes a collection of tracks.

  • The application should contain a main program:

  int main() {
    // . . .
    return 0;

  • In order to avoid warning messages when opening a file, The automatic library loading should be enabled:

  gSystem->Load( "libFWCoreFWLite" );

  • The event file should be opened, and the interesting branches connected. Get sure the branch name is correct!
  TFile file( "events.root" );
  TTree * events = dynamic_cast<TTree*>( file.Get( "Events" ) );
  TBranch * branch = events->GetBranch( "recoTracks_ctfWithMaterialTracks__Test.obj" );
  TrackCollection tracks;
  branch->SetAddress( & tracks );

  • Histograms are booked:
  TH1F h_pt( "pt", "p_{t}", 50, 0, 20 );
  TH1F h_eta( "eta", "#eta", 50, -3, 3 );

  • We loop over the events, loop over tracks, and fill the histograms:
  int nev = events->GetEntries();
  for( int ev = 0; ev < nev; ++ ev ) {
    branch->GetEntry( ev );
    for( size_t i = 0; i < tracks.size(); ++i ) {
      const Track & track = tracks[ i ];
      h_pt.Fill( );
      h_eta.Fill( track.eta() );

  • Then the file is closed, the histograms plotted:
  TCanvas c;
  c.SaveAs( "pt.jpg" );
  c.SaveAs( "eta.jpg" );

A Complete Example

A complete example can be found below:

Review Status

Reviewer/Editor and Date (copy from screen) Comments
LucaLista - 02 Feb 2007 page author
JennyWilliams - 05 Feb 2007 editing to include in SWGuide

Responsible: LucaLista
Last reviewed by: Reviewer

Edit | Attach | Watch | Print version | History: r10 < r9 < r8 < r7 < r6 | Backlinks | Raw View | WYSIWYG | More topic actions
Topic revision: r10 - 2010-03-08 - 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