CMS 3D CMS Logo

TestResolution.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: TestResolution
4 // Class: TestResolution
5 //
13 //
14 // Original Author: Marco De Mattia
15 // Created: Thu Sep 11 12:16:00 CEST 2008
16 //
17 //
18 
19 // system include files
20 #include <memory>
21 #include <string>
22 #include <vector>
23 
24 // user include files
37 
38 // For the momentum scale resolution
40 
41 // ROOT includes
42 #include "TCanvas.h"
43 #include "TLegend.h"
44 #include "TFile.h"
45 #include "TProfile.h"
46 
47 //
48 // class decleration
49 //
50 
52 public:
53  explicit TestResolution(const edm::ParameterSet&);
54  ~TestResolution() override;
55 
56 private:
57  void analyze(const edm::Event&, const edm::EventSetup&) override;
58  template <typename T>
59  std::vector<reco::LeafCandidate> fillMuonCollection(const std::vector<T>& tracks) {
60  std::vector<reco::LeafCandidate> muons;
61  typename std::vector<T>::const_iterator track;
62  for (track = tracks.begin(); track != tracks.end(); ++track) {
63  // Where 0.011163612 is the squared muon mass.
65  track->px(), track->py(), track->pz(), sqrt(track->p() * track->p() + 0.011163612));
66  reco::LeafCandidate muon(track->charge(), mu);
67  // Store muon
68  // ----------
69  muons.push_back(muon);
70  }
71  return muons;
72  }
73 
74  // ----------member data ---------------------------
75 
76  // Collections labels
77  // ------------------
82 
85  TFile* outputFile_;
86 
87  TProfile* sigmaPt_;
88 
90 
91  std::unique_ptr<ResolutionFunction> resolutionFunction_;
92 };
93 
94 //
95 // constructors and destructor
96 //
98  : theMuonLabel_(iConfig.getParameter<edm::InputTag>("MuonLabel")),
99  glbMuonsToken_(mayConsume<reco::MuonCollection>(theMuonLabel_)),
100  saMuonsToken_(mayConsume<reco::TrackCollection>(theMuonLabel_)),
101  tracksToken_(mayConsume<reco::TrackCollection>(theMuonLabel_)),
102  theMuonType_(iConfig.getParameter<int>("MuonType")),
103  theRootFileName_(iConfig.getUntrackedParameter<std::string>("OutputFileName")) {
104  //now do what ever initialization is needed
105  outputFile_ = new TFile(theRootFileName_.c_str(), "RECREATE");
106  outputFile_->cd();
107  sigmaPt_ = new TProfile("sigmaPtOverPt", "sigmaPt/Pt vs muon Pt", 1000, 0, 100);
108  eventCounter_ = 0;
109  // Create the corrector and set the parameters
111  std::make_unique<ResolutionFunction>(iConfig.getUntrackedParameter<std::string>("ResolutionsIdentifier"));
112  std::cout << "resolutionFunction_ = " << &*resolutionFunction_ << std::endl;
113 }
114 
116  outputFile_->cd();
117  TCanvas canvas("sigmaPtOverPt", "sigmaPt/Pt vs muon Pt", 1000, 800);
118  canvas.cd();
119  sigmaPt_->GetXaxis()->SetTitle("Pt(GeV)");
120  // TLegend * legend = new TLegend(0.7,0.71,0.98,1.);
121  // legend->SetTextSize(0.02);
122  // legend->SetFillColor(0); // Have a white background
123  // legend->AddEntry(uncorrectedPt_, "original pt");
124  // legend->AddEntry(correctedPt_, "corrected pt");
125  sigmaPt_->Draw();
126  // legend->Draw("same");
127 
128  canvas.Write();
129  sigmaPt_->Write();
130  outputFile_->Close();
131 
132  std::cout << "Total analyzed events = " << eventCounter_ << std::endl;
133 }
134 
135 //
136 // member functions
137 //
138 
139 // ------------ method called to for each event ------------
141  using namespace edm;
142 
143  ++eventCounter_;
144  if (eventCounter_ % 100 == 0) {
145  std::cout << "Event number " << eventCounter_ << std::endl;
146  }
147 
148  // Take the reco-muons, depending on the type selected in the cfg
149  // --------------------------------------------------------------
150 
151  std::vector<reco::LeafCandidate> muons;
152 
153  if (theMuonType_ == 1) { // GlobalMuons
155  iEvent.getByToken(glbMuonsToken_, glbMuons);
156  muons = fillMuonCollection(*glbMuons);
157  } else if (theMuonType_ == 2) { // StandaloneMuons
159  iEvent.getByToken(saMuonsToken_, saMuons);
160  muons = fillMuonCollection(*saMuons);
161  } else if (theMuonType_ == 3) { // Tracker tracks
163  iEvent.getByToken(tracksToken_, tracks);
165  }
166 
167  // Loop on the recMuons
168  std::vector<reco::LeafCandidate>::const_iterator recMuon = muons.begin();
169  for (; recMuon != muons.end(); ++recMuon) {
170  // Fill the histogram with uncorrected pt values
171  sigmaPt_->Fill(resolutionFunction_->sigmaPt(*recMuon, 0), recMuon->pt());
172  }
173 }
174 
175 //define this as a plug-in
TestResolution(const edm::ParameterSet &)
edm::EDGetTokenT< reco::TrackCollection > saMuonsToken_
std::vector< Track > TrackCollection
collection of Tracks
Definition: TrackFwd.h:14
std::string theRootFileName_
std::vector< Muon > MuonCollection
collection of Muon objects
Definition: MuonFwd.h:9
edm::EDGetTokenT< reco::TrackCollection > tracksToken_
T getUntrackedParameter(std::string const &, T const &) const
int iEvent
Definition: GenABIO.cc:224
T sqrt(T t)
Definition: SSEVec.h:19
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
~TestResolution() override
std::vector< reco::LeafCandidate > fillMuonCollection(const std::vector< T > &tracks)
auto const & tracks
cannot be loose
TProfile * sigmaPt_
fixed size matrix
HLT enums.
edm::EDGetTokenT< reco::MuonCollection > glbMuonsToken_
def canvas(sub, attr)
Definition: svgfig.py:482
void analyze(const edm::Event &, const edm::EventSetup &) override
std::unique_ptr< ResolutionFunction > resolutionFunction_
edm::InputTag theMuonLabel_
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Particle.h:21