CMS 3D CMS Logo

MuScleFitFilter.cc
Go to the documentation of this file.
1 // #define DEBUG
2 // System include files
3 // --------------------
4 
5 // User include files
6 // ------------------
12 
19 
20 #include <CLHEP/Vector/LorentzVector.h>
21 
22 // For file output
23 // ---------------
24 #include <fstream>
25 #include <sstream>
26 #include <cmath>
27 #include <memory>
28 
29 #include <vector>
30 
31 #include "TRandom.h"
32 
33 // Class declaration
34 // -----------------
35 
37  public:
38  explicit MuScleFitFilter(const edm::ParameterSet&);
39  ~MuScleFitFilter() override;
40 
41  private:
42  bool filter(edm::Event&, const edm::EventSetup&) override;
43  void endJob() override {};
44 
45  // Member data
46  // -----------
47  int eventsRead;
49  bool debug;
51  std::vector<double> Mmin;
52  std::vector<double> Mmax;
53  int maxWrite;
54  unsigned int minimumMuonsNumber;
55 
56  // Collections labels
57  // ------------------
62 
63 };
64 
65 // Static data member definitions
66 // ------------------------------
67 const double Mmu2 = 0.011163612; // Squared muon mass
68 
69 // Constructor
70 // -----------
72 {
73  debug = iConfig.getUntrackedParameter<bool>("debug",false);
74 
75  if (debug)
76  std::cout << "Constructor" << std::endl;
77 
78  // Parameters
79  // ----------
80  //ParameterSet serviceParameters = iConfig.getParameter<ParameterSet>("ServiceParameters");
81  //theService = new MuonServiceProxy(serviceParameters);
82  theMuonLabel = iConfig.getParameter<edm::InputTag>("MuonLabel");
83  theGlbMuonsToken = mayConsume<reco::MuonCollection>(theMuonLabel);
84  theSaMuonsToken = mayConsume<reco::TrackCollection>(theMuonLabel);
85  theTracksToken = mayConsume<reco::TrackCollection>(theMuonLabel);
86  theMuonType = iConfig.getParameter<int>("muonType");
87 
88  Mmin = iConfig.getUntrackedParameter<std::vector<double> >("Mmin");
89  Mmax = iConfig.getUntrackedParameter<std::vector<double> >("Mmax");
90  maxWrite = iConfig.getUntrackedParameter<int>("maxWrite",100000);
91 
92  minimumMuonsNumber = iConfig.getUntrackedParameter<unsigned int>("minimumMuonsNumber", 2);
93 
94  // The must have the same size and they must not be empty, otherwise abort.
95  if ( !(Mmin.size() == Mmax.size() && !Mmin.empty()) ) abort();
96 
97  // Count the total number of analyzed and written events
98  // -----------------------------------------------------
99  eventsRead = 0;
100  eventsWritten = 0;
101 
102 }
103 
104 
105 // Destructor
106 // ----------
108  std::cout << "Total number of events read = " << eventsRead << std::endl;
109  std::cout << "Total number of events written = " << eventsWritten << std::endl;
110 }
111 
112 // Member functions
113 // ----------------
114 
115 // Method called for each event
116 // ----------------------------
118 
119  // Cut the crap if we have stored enough stuff
120  // -------------------------------------------
121  if ( maxWrite != -1 && eventsWritten>=maxWrite ) return false;
122 
123  // Get the RecTrack and the RecMuon collection from the event
124  // ----------------------------------------------------------
125  std::unique_ptr<reco::MuonCollection> muons(new reco::MuonCollection());
126 
127  if (debug) std::cout << "Looking for muons of the right kind" << std::endl;
128 
129  if (theMuonType==1) { // GlobalMuons
130 
131  // Global muons:
132  // -------------
134  if (debug) std::cout << "Handle defined" << std::endl;
135  event.getByToken(theGlbMuonsToken, glbMuons);
136  if (debug)
137  std::cout << "Global muons: " << glbMuons->size() << std::endl;
138 
139  // Store the muon
140  // --------------
141  reco::MuonCollection::const_iterator glbMuon;
142  for (glbMuon=glbMuons->begin(); glbMuon!=glbMuons->end(); ++glbMuon) {
143  muons->push_back(*glbMuon);
144  if (debug) {
145  std::cout << " Reconstructed muon: pT = " << glbMuon->p4().Pt()
146  << " Eta = " << glbMuon->p4().Eta() << std::endl;
147  }
148  }
149  } else if (theMuonType==2) { // StandaloneMuons
150 
151  // Standalone muons:
152  // -----------------
154  event.getByToken(theSaMuonsToken, saMuons);
155  if (debug)
156  std::cout << "Standalone muons: " << saMuons->size() << std::endl;
157 
158  // Store the muon
159  // --------------
160  reco::TrackCollection::const_iterator saMuon;
161  for (saMuon=saMuons->begin(); saMuon!=saMuons->end(); ++saMuon) {
163  double energy = sqrt(saMuon->p()*saMuon->p()+Mmu2);
164  math::XYZTLorentzVector p4(saMuon->px(), saMuon->py(), saMuon->pz(), energy);
165  muon.setP4(p4);
166  muon.setCharge(saMuon->charge());
167  muons->push_back(muon);
168  }
169  } else if (theMuonType==3) { // Tracker tracks
170 
171  // Tracks:
172  // -------
174  event.getByToken(theTracksToken, tracks);
175  if (debug)
176  std::cout << "Tracker tracks: " << tracks->size() << std::endl;
177 
178  // Store the muon
179  // -------------
180  reco::TrackCollection::const_iterator track;
181  for (track=tracks->begin(); track!=tracks->end(); ++track) {
183  double energy = sqrt(track->p()*track->p()+Mmu2);
184  math::XYZTLorentzVector p4(track->px(), track->py(), track->pz(), energy);
185  muon.setP4(p4);
186  muon.setCharge(track->charge());
187  muons->push_back(muon);
188  }
189  } else {
190  std::cout << "Wrong muon type! Aborting." << std::endl;
191  abort();
192  }
193 
194  // Loop on RecMuon and reconstruct the resonance
195  // ---------------------------------------------
196  reco::MuonCollection::const_iterator muon1;
197  reco::MuonCollection::const_iterator muon2;
198 
199  bool resfound = false;
200 
201  // Require at least N muons of the selected type.
202  if( muons->size() >= minimumMuonsNumber ) {
203 
204  for (muon1=muons->begin(); muon1!=muons->end(); ++muon1) {
205 
206  if (debug) {
207  std::cout << " Reconstructed muon: pT = " << muon1->p4().Pt()
208  << " Eta = " << muon1->p4().Eta() << std::endl;
209  }
210 
211  // Recombine all the possible Z from reconstructed muons
212  // -----------------------------------------------------
213  if (muons->size()>1) {
214  for (muon2 = muon1+1; muon2!=muons->end(); ++muon2) {
215  if ( ((*muon1).charge()*(*muon2).charge())<0 ) { // This also gets rid of muon1==muon2
216  // reco::Particle::LorentzVector Z (muonCorr1 + muonCorr2);
217  reco::Particle::LorentzVector Z (muon1->p4()+muon2->p4());
218  // Loop on all the cuts on invariant mass.
219  // If it passes at least one of the cuts, the event will be accepted.
220  // ------------------------------------------------------------------
221  std::vector<double>::const_iterator mMinCut = Mmin.begin();
222  std::vector<double>::const_iterator mMaxCut = Mmax.begin();
223  for( ; mMinCut != Mmin.end(); ++mMinCut, ++mMaxCut ) {
224  // When the two borders are -1 do not cut.
225  if( *mMinCut == *mMaxCut && *mMaxCut == -1) {
226  resfound = true;
227  if (debug) {
228  std::cout << "Acceptiong event because mMinCut = " << *mMinCut << " = mMaxCut = " << *mMaxCut << std::endl;
229  }
230  }
231  else if (Z.mass()>*mMinCut && Z.mass()<*mMaxCut) {
232  resfound = true;
233  if (debug) {
234  std::cout << "One particle found with mass = " << Z.mass() << std::endl;
235  }
236  }
237  }
238  }
239  }
240  } else if (debug) {
241  std::cout << "Not enough reconstructed muons to make a resonance" << std::endl;
242  }
243  }
244  }
245  else if (debug) {
246  std::cout << "Skipping event because muons = " << muons->size() << " < " << "minimumMuonsNumber("<<minimumMuonsNumber<<")" << std::endl;
247  }
248 
249  // Store the event if it has a dimuon pair with mass within defined boundaries
250  // ---------------------------------------------------------------------------
251  bool write = false;
252  eventsRead++;
253  if ( resfound ) {
254  write = true;
255  eventsWritten++;
256  }
257  return write;
258 
259 }
260 
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
void endJob() override
bool filter(edm::Event &, const edm::EventSetup &) override
std::vector< Muon > MuonCollection
collection of Muon objects
Definition: MuonFwd.h:9
~MuScleFitFilter() override
void setCharge(Charge q) final
set electric charge
Definition: LeafCandidate.h:93
XYZTLorentzVectorD XYZTLorentzVector
Lorentz vector with cylindrical internal representation using pseudorapidity.
Definition: LorentzVector.h:29
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
const double Mmu2
T sqrt(T t)
Definition: SSEVec.h:18
double p4[4]
Definition: TauolaWrapper.h:92
edm::EDGetTokenT< reco::TrackCollection > theTracksToken
edm::EDGetTokenT< reco::MuonCollection > theGlbMuonsToken
edm::EDGetTokenT< reco::TrackCollection > theSaMuonsToken
edm::InputTag theMuonLabel
unsigned int minimumMuonsNumber
std::vector< double > Mmax
std::vector< double > Mmin
def write(self, setup)
MuScleFitFilter(const edm::ParameterSet &)
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Particle.h:21
void setP4(const LorentzVector &p4) final
set 4-momentum
Definition: event.py:1