You are here

TutorialSelector.C

// This class is derived from the ROOT class TSelector to demonstrate
// the use of PROOF with the ROOT 6 Analysis Workshop from
// http://root.cern.ch/drupal/content/root-6-analysis-workshop
//
// For more information on the TSelector framework see
// $ROOTSYS/README/README.SELECTOR or the ROOT User Manual.
// The following methods are defined in this file:
//    SlaveBegin():   called after Begin(), when on PROOF called only on the
//                    slave servers, a convenient place to create your histograms.
//    Process():      called for each event, in this function you decide what
//                    to read and fill your histograms.
//    Terminate():    called at the end of the loop on the tree,
//                    a convenient place to draw/fit your histograms.
//
// To use this file, try the following session on your Tree T:
//
 
#include "TEfficiency.h"
#include "TRandom.h"
#include "TSelector.h"
#include "TTreeReader.h"
#include "TTreeReaderArray.h"
 
class TutorialSelector : public TSelector {
public :
 
   TEfficiency* fEff;
 
   // Variables used to access and store the data
   TTreeReader fReader;                            // The tree reader
   TTreeReaderArray<double> fRaPt;                 // muon pt
   TTreeReaderArray<bool> fRaTriggered;            // trigger decision
 
  TutorialSelector(TTree * = 0): fEff(nullptr), fRaPt(fReader, "muons.fPt"),
				  fRaTriggered(fReader, "muons.fTriggered") { }
   virtual ~TutorialSelector() { }
   virtual void    Init(TTree *tree);
   virtual void    SlaveBegin(TTree *tree);
   virtual Bool_t  Process(Long64_t entry);
   virtual void    Terminate();
   virtual Int_t   Version() const { return 2; }
 
   ClassDef(TutorialSelector,0);
};
 
void TutorialSelector::Init(TTree *tree)
{
   // The Init() function is called when the selector needs to initialize
   // a new tree or chain. Typically here the branch addresses and branch
   // pointers of the tree will be set.
   // It is normally not necessary to make changes to the generated
   // code, but the routine can be extended by the user if needed.
   // Init() will be called many times when running on PROOF
   // (once per file to be processed).
 
   // Associate the reader and the tree
   fReader.SetTree(tree);
}
 
void TutorialSelector::SlaveBegin(TTree *tree)
{
   // SlaveBegin() is a good place to create histograms.
   // For PROOF, this is called for each worker.
   // The TTree* is there for backward compatibility; e.g. PROOF passes 0.
 
   fEff  = new TEfficiency("eff", "Moun trigger efficiency", 100, 0., 10.);
   // Add to output list (needed for PROOF)
   GetOutputList()->Add(fEff);
}
 
Bool_t TutorialSelector::Process(Long64_t entry)
{
   // The Process() function is called for each entry in the tree to be
   // processed. The entry argument specifies which entry in the currently
   // loaded tree is to be processed.
   // It can be passed to either EventSelector::GetEntry() or TBranch::GetEntry()
   // to read either all or the required parts of the TTree.
   //
   // This function should contain the "body" of the analysis: select relevant
   // tree entries, run algorithms on the tree entry and typically fill histograms.
 
   // *** 1. *** Tell the reader to load the data for this entry:
   fReader.SetEntry(entry);
 
   // *** 2. *** Do the actual analysis
   int tagMuon = gRandom->Integer(fRaPt.GetSize());
 
   if (fRaTriggered[tagMuon]) {
     for (int iMuon = 0, nMuons = fRaPt.GetSize(); iMuon < nMuons; ++iMuon) {
       // All but the tag muon can be probe muons
       if (iMuon != tagMuon) {
	 // Fill the efficiency object with
	 // - whether this muon has triggered
	 // - the pT of this muon.
	 fEff->Fill(fRaTriggered[iMuon], fRaPt[iMuon]);
       }
     }
   }
 
   return kTRUE;
}
 
void TutorialSelector::Terminate()
{
   // The Terminate() function is the last function to be called during the
   // analysis of a tree with a selector. It always runs on the client, it can
   // be used to present the results graphically or save the results to file.
 
   fEff->Print();
}