CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PatTriggerAnalyzer.cc
Go to the documentation of this file.
1 #include <map>
2 #include <string>
3 
4 #include "TH1D.h"
5 #include "TH2D.h"
6 
14 
15 
17 
18  public:
20  explicit PatTriggerAnalyzer( const edm::ParameterSet & iConfig );
23 
24  private:
26  virtual void beginJob();
28  virtual void analyze( const edm::Event & iEvent, const edm::EventSetup & iSetup );
30  virtual void endJob();
31 
41  unsigned nBins_;
42  double binWidth_;
44  unsigned minID_;
46  unsigned maxID_;
47 
49  std::map< std::string, TH1D* > histos1D_;
50  std::map< std::string, TH2D* > histos2D_;
51 
53  std::map< unsigned, unsigned > sumN_;
54  std::map< unsigned, double > sumPt_;
55 };
56 
57 
58 #include "TMath.h"
62 
63 
64 using namespace pat;
65 
66 
68  // pat::Trigger
69  trigger_( iConfig.getParameter< edm::InputTag >( "trigger" ) ),
70  // pat::TriggerEvent
71  triggerEvent_( iConfig.getParameter< edm::InputTag >( "triggerEvent" ) ),
72  // muon input collection
73  muons_( iConfig.getParameter< edm::InputTag >( "muons" ) ),
74  // muon match objects
75  muonMatch_( iConfig.getParameter< std::string >( "muonMatch" ) ),
76  // binning for turn-on curve
77  nBins_( iConfig.getParameter< unsigned >( "nBins" ) ),
78  binWidth_( iConfig.getParameter< double >( "binWidth" ) ),
79  // minimal id for of all trigger objects
80  minID_( iConfig.getParameter< unsigned >( "minID" ) ),
81  // maximal id for of all trigger objects
82  maxID_( iConfig.getParameter< unsigned >( "maxID" ) ),
83  histos1D_(), histos2D_()
84 {
85 }
86 
88 {
89 }
90 
92 {
94 
95  // pt correlation plot
96  histos2D_[ "ptTrigCand" ] = fileService->make< TH2D >( "ptTrigCand", "Object vs. candidate p_{T} (GeV)", 60, 0., 300., 60, 0., 300. );
97  histos2D_[ "ptTrigCand" ]->SetXTitle( "candidate p_{T} (GeV)" );
98  histos2D_[ "ptTrigCand" ]->SetYTitle( "object p_{T} (GeV)" );
99  // eta correlation plot
100  histos2D_[ "etaTrigCand" ] = fileService->make< TH2D >( "etaTrigCand", "Object vs. candidate #eta", 50, -2.5, 2.5, 50, -2.5, 2.5 );
101  histos2D_[ "etaTrigCand" ]->SetXTitle( "candidate #eta" );
102  histos2D_[ "etaTrigCand" ]->SetYTitle( "object #eta" );
103  // phi correlation plot
104  histos2D_[ "phiTrigCand" ] = fileService->make< TH2D >( "phiTrigCand", "Object vs. candidate #phi", 60, -TMath::Pi(), TMath::Pi(), 60, -TMath::Pi(), TMath::Pi() );
105  histos2D_[ "phiTrigCand" ]->SetXTitle( "candidate #phi" );
106  histos2D_[ "phiTrigCand" ]->SetYTitle( "object #phi" );
107  // candidate counter for turn-on curve
108  histos1D_[ "countCand" ] = fileService->make< TH1D >( "countCand", "p_{T} (GeV) of candidate", nBins_, 20., 20. + nBins_ * binWidth_ );
109  histos1D_[ "countCand" ]->SetXTitle( "candidate p_{T} (GeV)" );
110  histos1D_[ "countCand" ]->SetYTitle( "# of candidates" );
111  // turn-on curve
112  histos1D_[ "turnOn" ] = fileService->make< TH1D >( "turnOn", "p_{T} (GeV) of candidate", nBins_, 20., 20. + nBins_ * binWidth_ );
113  histos1D_[ "turnOn" ]->SetXTitle( "candidate p_{T} (GeV)" );
114  histos1D_[ "turnOn" ]->SetYTitle( "efficiency" );
115  // mean pt for all trigger objects
116  histos1D_[ "ptMean" ] = fileService->make< TH1D >( "ptMean", "Mean p_{T} (GeV) per trigger object type", maxID_ - minID_ + 1, minID_ - 0.5, maxID_ + 0.5);
117  histos1D_[ "ptMean" ]->SetXTitle( "trigger object type" );
118  histos1D_[ "ptMean" ]->SetYTitle( "mean p_{T} (GeV)" );
119 
120  // initialize counters for mean pt calculation
121  for( unsigned id = minID_; id <= maxID_; ++id ) {
122  sumN_ [ id ] = 0;
123  sumPt_[ id ] = 0.;
124  }
125 }
126 
128 {
129  // PAT trigger event
130  edm::Handle< TriggerEvent > triggerEvent;
131  iEvent.getByLabel( triggerEvent_, triggerEvent );
132 
133  // PAT object collection
135  iEvent.getByLabel( muons_, muons );
136 
137  // PAT trigger helper for trigger matching information
138  const helper::TriggerMatchHelper matchHelper;
139 
140  /*
141  kinematics comparison
142  */
143 
144  // loop over muon references (PAT muons have been used in the matcher in task 3)
145  for( size_t iMuon = 0; iMuon < muons->size(); ++iMuon ) {
146  // we need all these ingedients to recieve matched trigger objects from the matchHelper
147  const TriggerObjectRef trigRef( matchHelper.triggerMatchObject( muons, iMuon, muonMatch_, iEvent, *triggerEvent ) );
148  // finally we can fill the histograms
149  if ( trigRef.isAvailable() && trigRef.isNonnull() ) { // check references (necessary!)
150  histos2D_[ "ptTrigCand" ]->Fill( muons->at( iMuon ).pt(), trigRef->pt() );
151  histos2D_[ "etaTrigCand" ]->Fill( muons->at( iMuon ).eta(), trigRef->eta() );
152  histos2D_[ "phiTrigCand" ]->Fill( muons->at( iMuon ).phi(), trigRef->phi() );
153  }
154  }
155 
156  /*
157  turn-on curve
158  */
159 
160  // loop over muon references again
161  for( size_t iMuon = 0; iMuon < muons->size(); ++iMuon ) {
162  // fill the counting histogram...
163  histos1D_[ "countCand" ]->Fill( muons->at( iMuon ).pt() );
164  }
165 
166  // get the trigger objects corresponding to the used matching (HLT muons)
167  const TriggerObjectRefVector trigRefs( triggerEvent->objects( trigger::TriggerMuon ) );
168  // loop over selected trigger objects
169  for ( TriggerObjectRefVector::const_iterator iTrig = trigRefs.begin(); iTrig != trigRefs.end(); ++iTrig ) {
170  // get all matched candidates for the trigger object
171  const reco::CandidateBaseRefVector candRefs( matchHelper.triggerMatchCandidates( ( *iTrig ), muonMatch_, iEvent, *triggerEvent ) );
172  if ( candRefs.empty() ) continue;
173  // fill the histogram...
174  // (only for the first match, since we resolved ambiguities in the matching configuration,
175  // so that we have one at maximum per trigger object)
176  reco::CandidateBaseRef muonRef( candRefs.at( 0 ) );
177  if ( muonRef.isAvailable() && muonRef.isNonnull() ) {
178  histos1D_[ "turnOn" ]->Fill( muonRef->pt() );
179  }
180  }
181 
182  /*
183  mean pt
184  */
185 
186  // loop over all trigger objects from minID to maxID; have
187  // a look to DataFormats/HLTReco/interface/TriggerTypeDefs.h to
188  // know more about the available trrigger object IDs
189  for ( unsigned id = minID_; id <= maxID_; ++id ) {
190  // vector of all objects for a given object id
191  const TriggerObjectRefVector objRefs( triggerEvent->objects( id ) );
192  // buffer the number of objects
193  sumN_[ id ] += objRefs.size();
194  // iterate the objects and buffer the pt of the objects
195  for ( TriggerObjectRefVector::const_iterator iRef = objRefs.begin(); iRef != objRefs.end(); ++iRef ) {
196  sumPt_[ id ] += ( *iRef )->pt();
197  }
198  }
199 }
200 
202 {
203  /*
204  turn-on curve
205  */
206 
207  // normalise bins for turn-on based with counter
208  histos1D_[ "turnOn" ]->Divide( histos1D_[ "countCand" ] );
209 
210  /*
211  mean pt
212  */
213 
214  // normalize the entries for the mean pt plot
215  for(unsigned id=minID_; id<=maxID_; ++id){
216  if( sumN_[ id ] != 0 ) histos1D_[ "ptMean" ]->Fill( id, sumPt_[ id ] / sumN_[ id ] );
217  }
218 }
219 
220 
const double Pi
virtual void beginJob()
everythin that needs to be done before the event loop
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
virtual void analyze(const edm::Event &iEvent, const edm::EventSetup &iSetup)
everythin that needs to be done during the event loop
~PatTriggerAnalyzer()
default destructor
virtual void endJob()
everythin that needs to be done after the event loop
int iEvent
Definition: GenABIO.cc:243
PatTriggerAnalyzer(const edm::ParameterSet &iConfig)
default constructor
std::map< std::string, TH1D * > histos1D_
histogram management
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:361
unsigned minID_
minimal id for meanPt plot
edm::Service< TFileService > fileService
edm::InputTag triggerEvent_
input for patTriggerEvent
std::map< unsigned, double > sumPt_
edm::InputTag muons_
input for muons
tuple muons
Definition: patZpeak.py:38
edm::InputTag trigger_
input for patTrigger
std::map< std::string, TH2D * > histos2D_
std::map< unsigned, unsigned > sumN_
internals for meanPt histogram calculation
T * make() const
make new ROOT object
unsigned maxID_
maximal id for meanPt plot
std::string muonMatch_
input for trigger match objects
unsigned nBins_
binning for turn-on curve