CMS 3D CMS Logo

TestCorrection.cc
Go to the documentation of this file.
1 #ifndef TESTCORRECTION_HH
2 #define TESTCORRECTION_HH
3 
4 // -*- C++ -*-
5 //
6 // Package: TestCorrection
7 // Class: TestCorrection
8 //
16 //
17 // Original Author: Marco De Mattia
18 // Created: Thu Sep 11 12:16:00 CEST 2008
19 //
20 //
21 
22 // system include files
23 #include <memory>
24 #include <string>
25 #include <vector>
26 
27 // user include files
32 
41 
43 
44 // For the momentum scale correction
45 #include "MuScleFitBase.h"
49 
50 #include "TFile.h"
51 #include "TProfile.h"
52 #include "TH1F.h"
53 #include "TCanvas.h"
54 #include "TLegend.h"
55 
56 
57 
58 
59 //
60 // class decleration
61 //
62 
64 public:
65  explicit TestCorrection(const edm::ParameterSet&);
66  ~TestCorrection() override;
67 
68 private:
69  virtual void initialize(const edm::EventSetup&);
70  void analyze(const edm::Event&, const edm::EventSetup&) override;
71  void endJob() override {};
72  template<typename T>
73  std::vector<MuScleFitMuon> fillMuonCollection( const std::vector<T>& tracks )
74  {
75  std::vector<MuScleFitMuon> muons;
76  typename std::vector<T>::const_iterator track;
77  for( track = tracks.begin(); track != tracks.end(); ++track ) {
79  mu = reco::Particle::LorentzVector(track->px(),track->py(),track->pz(),
80  sqrt(track->p()*track->p() + + 0.011163612));
81 
82  Double_t hitsTk(0), hitsMuon(0), ptError(0);
83  if ( const reco::Muon* myMu = dynamic_cast<const reco::Muon*>(&(*track)) ){
84  hitsTk = myMu->innerTrack()->hitPattern().numberOfValidTrackerHits();
85  hitsMuon = myMu->innerTrack()->hitPattern().numberOfValidMuonHits();
86  ptError = myMu->innerTrack()->ptError();
87  }
88  else if ( const pat::Muon* myMu = dynamic_cast<const pat::Muon*>(&(*track)) ) {
89  hitsTk = myMu->innerTrack()->hitPattern().numberOfValidTrackerHits();
90  hitsMuon = myMu->innerTrack()->hitPattern().numberOfValidMuonHits();
91  ptError = myMu->innerTrack()->ptError();
92  }
93  else if (const reco::Track* myMu = dynamic_cast<const reco::Track*>(&(*track))){
94  hitsTk = myMu->hitPattern().numberOfValidTrackerHits();
95  hitsMuon = myMu->hitPattern().numberOfValidMuonHits();
96  ptError = myMu->ptError();
97  }
98 
99  MuScleFitMuon muon(mu,track->charge(),ptError,hitsTk,hitsMuon);
100 
101  if (debug_>0) {
102  std::cout<<"[TestCorrection::fillMuonCollection] after MuScleFitMuon initialization"<<std::endl;
103  std::cout<<" muon = "<<muon<<std::endl;
104  }
105 
106  muons.push_back(muon);
107  }
108  return muons;
109  }
110 
111 
112 
114 
115  // ----------member data ---------------------------
116 
117  // Collections labels
118  // ------------------
121  TH1F * correctedPt_;
122  TProfile * correctedPtVsEta_;
123 
125 
126  std::unique_ptr<MomentumScaleCorrector> corrector_;
127  std::unique_ptr<ResolutionFunction> resolution_;
128  std::unique_ptr<BackgroundFunction> background_;
129 
133 
134 };
135 
136 #endif // TESTCORRECTION_HH
137 //
138 // constants, enums and typedefs
139 //
140 
141 //
142 // static data member definitions
143 //
144 
145 //
146 // constructors and destructor
147 //
149  MuScleFitBase( iConfig ),
153 {
154  //now do what ever initialization is needed
155  TFile * outputFile = new TFile(theRootFileName_.c_str(), "RECREATE");
156  theFiles_.push_back(outputFile);
157  // outputFile_ = new TFile(theRootFileName_.c_str(), "RECREATE");
158  // outputFile_->cd();
159  outputFile->cd();
160  MuScleFitUtils::resfind = iConfig.getParameter<std::vector<int> >("resfind");
161  fillHistoMap(outputFile, 0);
162  uncorrectedPt_ = new TH1F("uncorrectedPt", "uncorrected pt", 1000, 0, 100);
163  uncorrectedPtVsEta_ = new TProfile("uncorrectedPtVsEta", "uncorrected pt vs eta", 1000, 0, 100, -3., 3.);
164  correctedPt_ = new TH1F("correctedPt", "corrected pt", 1000, 0, 100);
165  correctedPtVsEta_ = new TProfile("correctedPtVsEta", "corrected pt vs eta", 1000, 0, 100, -3., 3.);
166  eventCounter_ = 0;
167  // Create the corrector and set the parameters
168  corrector_.reset(new MomentumScaleCorrector( iConfig.getUntrackedParameter<std::string>("CorrectionsIdentifier") ) );
169  std::cout << "corrector_ = " << &*corrector_ << std::endl;
170  resolution_.reset(new ResolutionFunction(iConfig.getUntrackedParameter<std::string>("ResolutionsIdentifier") ) );
171  std::cout << "resolution_ = " << &*resolution_ << std::endl;
172  background_.reset(new BackgroundFunction(iConfig.getUntrackedParameter<std::string>("BackgroundIdentifier") ) );
173 
174  // Initialize the parameters of MuScleFitUtils from those saved in the functions.
175  // MuScleFitUtils::parScale = corrector_.getFunction(0)->parameters();
178 
179  MuScleFitUtils::parResol = resolution_->parameters();
180 }
181 
183 {
184  theFiles_[0]->cd();
185  TCanvas canvas("ptComparison","pt comparison", 1000, 800);
186  canvas.cd();
187  uncorrectedPt_->GetXaxis()->SetTitle("Pt(GeV)");
188  correctedPt_->SetLineColor(kRed);
189  TLegend * legend = new TLegend(0.7,0.71,0.98,1.);
190  legend->SetTextSize(0.02);
191  legend->SetFillColor(0); // Have a white background
192  legend->AddEntry(uncorrectedPt_, "original pt");
193  legend->AddEntry(correctedPt_, "corrected pt");
194  uncorrectedPt_->Draw();
195  correctedPt_->Draw("same");
196  legend->Draw("same");
197 
198  canvas.Write();
199  uncorrectedPt_->Write();
200  uncorrectedPtVsEta_->Write();
201  correctedPt_->Write();
202  correctedPtVsEta_->Write();
203 
204  writeHistoMap(0);
205  theFiles_[0]->Close();
206 
207  std::cout << "Total analyzed events = " << eventCounter_ << std::endl;
208 }
209 
210 //
211 // member functions
212 //
213 
214 // ------------ method called to for each event ------------
216  using namespace edm;
217 
218  initialize(iSetup);
219 
220  ++eventCounter_;
221  if ( eventCounter_%100 == 0 ) {
222  std::cout << "Event number " << eventCounter_ << std::endl;
223  }
224 
225  // Take the reco-muons, depending on the type selected in the cfg
226  // --------------------------------------------------------------
227 
228  std::vector<MuScleFitMuon> muons;
229 
230  if (theMuonType_==1) { // GlobalMuons
232  iEvent.getByToken (glbMuonsToken_, glbMuons);
233  muons = fillMuonCollection(*glbMuons);
234  }
235  else if (theMuonType_==2) { // StandaloneMuons
237  iEvent.getByToken (saMuonsToken_, saMuons);
238  muons = fillMuonCollection(*saMuons);
239  }
240  else if (theMuonType_==3) { // Tracker tracks
242  iEvent.getByToken (tracksToken_, tracks);
243  muons = fillMuonCollection(*tracks);
244  }
245 
246  // Find the two muons from the resonance, and set ResFound bool
247  // ------------------------------------------------------------
248  std::pair <MuScleFitMuon, MuScleFitMuon> recMuFromBestRes =
251  MuScleFitUtils::SavedPair.push_back( std::make_pair (recMuFromBestRes.first.p4(), recMuFromBestRes.second.p4()) );
252  } else {
253  MuScleFitUtils::SavedPair.push_back( std::make_pair (lorentzVector(0.,0.,0.,0.), lorentzVector(0.,0.,0.,0.)) );
254  }
255 
256  // If resonance found, do the hard work
257  // ------------------------------------
259 
260  // Find weight and reference mass for this muon pair
261  // -------------------------------------------------
262  // double weight = MuScleFitUtils::computeWeight ((recMu1+recMu2).mass());
263 
264  // Use the correction function to correct the pt scale of the muons. Note that this takes into
265  // account the corrections from all iterations.
266  lorentzVector recMu1;
267  recMu1 = correctMuon(recMu1);
268  lorentzVector recMu2;
269  recMu2 = correctMuon(recMu2);
270 
271  reco::Particle::LorentzVector bestRecRes (recMu1+recMu2);
272 
273  //Fill histograms
274  //------------------
275  mapHisto_["hRecBestMu"]->Fill(recMu1);
276  if ((std::abs(recMu1.eta())<2.5) && (recMu1.pt()>2.5)) {
277  mapHisto_["hRecBestMu_Acc"]->Fill(recMu1);
278  }
279  mapHisto_["hRecBestMu"]->Fill(recMu2);
280  if ((std::abs(recMu2.eta())<2.5) && (recMu2.pt()>2.5)) {
281  mapHisto_["hRecBestMu_Acc"]->Fill(recMu2);
282  }
283  mapHisto_["hDeltaRecBestMu"]->Fill(recMu1, recMu2);
284 
285  mapHisto_["hRecBestRes"]->Fill(bestRecRes);
286  if ((std::abs(recMu1.eta())<2.5) && (recMu1.pt()>2.5) && (std::abs(recMu2.eta())<2.5) && (recMu2.pt()>2.5)){
287  mapHisto_["hRecBestRes_Acc"]->Fill(bestRecRes);
288  // Fill histogram of Res mass vs muon variable
289  mapHisto_["hRecBestResVSMu"]->Fill (recMu1, bestRecRes, -1);
290  mapHisto_["hRecBestResVSMu"]->Fill (recMu2, bestRecRes, +1);
291  }
292  }
293 
294  // Loop on the recMuons
295  std::vector<MuScleFitMuon>::const_iterator recMuon = muons.begin();
296  int muonCount = 0;
297  for ( ; recMuon!=muons.end(); ++recMuon, ++muonCount ) {
298 
299  // Fill the histogram with uncorrected pt values
300  uncorrectedPt_->Fill(recMuon->pt());
301  uncorrectedPtVsEta_->Fill(recMuon->pt(), recMuon->eta());
302 
303  // Fill the histogram with corrected pt values
304  std::cout << "correcting muon["<<muonCount<<"] with pt = " << recMuon->pt() << std::endl;
305  double corrPt = (*corrector_)(*recMuon);
306  std::cout << "to pt = " << corrPt << std::endl;
307  correctedPt_->Fill(corrPt);
308  correctedPtVsEta_->Fill(corrPt, recMuon->eta());
309  // correctedPt_->Fill(recMuon->pt());
310  }
311 }
312 
314  double corrPt = corrector_->correct(muon);
315  double ptEtaPhiE[4] = {corrPt, muon.Eta(), muon.Phi(), muon.E()};
316  return MuScleFitUtils::fromPtEtaPhiToPxPyPz(ptEtaPhiE);
317 }
318 
319 // ------------ method called once each job just before starting event loop ------------
321 {
322  // Read the pdf from root file. They are used by massProb when finding the muon pair, needed
323  // for the mass histograms.
325 }
326 
327 
328 
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
edm::InputTag theMuonLabel_
Definition: MuScleFitBase.h:46
lorentzVector correctMuon(const lorentzVector &muon)
TProfile * correctedPtVsEta_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
static std::vector< double > parResol
virtual void initialize(const edm::EventSetup &)
std::vector< Track > TrackCollection
collection of Tracks
Definition: TrackFwd.h:15
edm::EDGetTokenT< reco::TrackCollection > saMuonsToken_
std::map< std::string, Histograms * > mapHisto_
The map of histograms.
Definition: MuScleFitBase.h:77
static bool ResFound
std::unique_ptr< BackgroundFunction > background_
std::vector< Muon > MuonCollection
collection of Muon objects
Definition: MuonFwd.h:9
reco::Particle::LorentzVector lorentzVector
Definition: GenMuonPair.h:9
static std::pair< MuScleFitMuon, MuScleFitMuon > findBestRecoRes(const std::vector< MuScleFitMuon > &muons)
void analyze(const edm::Event &, const edm::EventSetup &) override
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
resolutionFunctionBase< std::vector< double > > * resolutionFunctionVecService(const int identifier)
Service to build the resolution functor corresponding to the passed identifier when receiving a std::...
Definition: Functions.cc:50
T sqrt(T t)
Definition: SSEVec.h:18
std::unique_ptr< MomentumScaleCorrector > corrector_
static lorentzVector fromPtEtaPhiToPxPyPz(const double *ptEtaPhiE)
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
TestCorrection(const edm::ParameterSet &)
static std::vector< std::pair< lorentzVector, lorentzVector > > SavedPair
const int mu
Definition: Constants.h:22
TProfile * uncorrectedPtVsEta_
~TestCorrection() override
void endJob() override
std::vector< MuScleFitMuon > fillMuonCollection(const std::vector< T > &tracks)
void writeHistoMap(const unsigned int iLoop)
Save the histograms map to file.
std::string theRootFileName_
Definition: MuScleFitBase.h:47
void fillHistoMap(TFile *outputFile, unsigned int iLoop)
Create the histograms map.
Definition: MuScleFitBase.cc:7
std::vector< TFile * > theFiles_
The files were the histograms are saved.
Definition: MuScleFitBase.h:74
edm::EDGetTokenT< reco::MuonCollection > glbMuonsToken_
fixed size matrix
HLT enums.
def canvas(sub, attr)
Definition: svgfig.py:482
edm::EDGetTokenT< reco::TrackCollection > tracksToken_
static std::vector< int > resfind
void readProbabilityDistributionsFromFile()
Read probability distributions from a local root file.
EDGetTokenT< ProductType > mayConsume(edm::InputTag const &tag)
static resolutionFunctionBase< std::vector< double > > * resolutionFunctionForVec
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Particle.h:21
Analysis-level muon class.
Definition: Muon.h:51
static resolutionFunctionBase< double * > * resolutionFunction
std::unique_ptr< ResolutionFunction > resolution_