CMS 3D CMS Logo

Tau3MuReco.cc
Go to the documentation of this file.
1 #include <TMath.h>
2 
4 
6 
8 
10  : m_kMatchingDeltaR(iConfig.getParameter<double>("RecoAnalysisMatchingDeltaR")),
11  m_kMatchingPt(iConfig.getParameter<double>("RecoAnalysisMatchingPt")),
12  m_kTauMassCut(iConfig.getParameter<double>("RecoAnalysisTauMassCut")),
13  m_kTauMass(iConfig.getParameter<double>("RecoAnalysisTauMass")),
14  m_kMuonMass(iConfig.getParameter<double>("RecoAnalysisMuonMass")),
15  m_kMuonSourceToken(iC.consumes<reco::MuonCollection>(iConfig.getParameter<edm::InputTag>("MuonSourceTag"))),
16  m_kTrackSourceToken(iC.consumes<reco::TrackCollection>(iConfig.getParameter<edm::InputTag>("TrackSourceTag"))) {}
17 
19 
21  const edm::EventSetup& iSetup,
26 
29 
30  reco::MuonCollection::const_iterator muon;
31 
32  iEvent.getByToken(m_kMuonSourceToken, muons);
33  iEvent.getByToken(m_kTrackSourceToken, tracks);
34 
35  for (muon = muons->begin(); muon != muons->end(); ++muon) {
36  m_TrackCollection->push_back(*(muon->track().get()));
37  m_MuonCollection->push_back(*muon);
38  }
39 
40  if (m_TrackCollection->size() > 3) {
41  //find the right three ones coming from tau
42  if (findCorrectPairing() == false) { //maybe implement something like in ==3 (throw away ....)
43  LogDebug("Tau3MuReco") << "Could not find correct combination!" << std::endl;
44  return false;
45  }
46 
47  return true;
48  }
49 
50  if (m_TrackCollection->size() == 3) {
52  return true;
53  else //throw away one muon which don't match
55  }
56 
57  if (m_TrackCollection->size() == 2) {
58  //search the third track
59 
60  //get 3rd muon canidate from tracks
61  if (find3rdTrack(iEvent, iSetup, *(tracks.product())) == false) {
62  LogDebug("Tau3MuReco") << "A 3rd Track can not be assigned!" << std::endl;
63  return false;
64  } else
65  return true;
66  }
67 
68  // cannot use this event, because less than 2 muons have been found
69 
70  LogDebug("Tau3MuReco") << "Not enough (" << m_TrackCollection->size() << ") muons found! Event skipped!" << std::endl;
71 
72  return false;
73 }
74 
75 //private
77  reco::TrackCollection::const_iterator iter;
78 
79  for (iter = m_TrackCollection->begin(); iter != m_TrackCollection->end(); iter++) {
80  //check if the track has the right charge
81  //and check if dR is smaller than fMatchingDeltaR
82  if ((*iter).charge() == track.charge() && getDeltaR(*iter, track) < m_kMatchingDeltaR &&
83  fabs(((*iter).pt()) - (track.pt())) <= (track.pt() * m_kMatchingPt)) {
84  LogDebug("Tau3MuReco") << "Found muon track in Tracks with DeltaR: " << getDeltaR(*iter, track)
85  << " Pt: " << track.pt() << " Muons Pt is: " << (*iter).pt() << std::endl;
86  return true;
87  }
88  }
89  return false;
90 }
91 
93  const edm::EventSetup& iSetup,
95  //check size of TrackVector (should be two!)
96  if (m_TrackCollection->size() != 2)
97  return false;
98 
99  //more then two tracks should be in the event
100  if (Tracks.size() <= 2)
101  return false;
102 
103  double SumDeltaR = 0;
104 
105  double MuonDeltaR = getDeltaR(m_TrackCollection->at(0), m_TrackCollection->at(1));
106 
107  //Loop overall tracks
108 
109  LogDebug("Tau3MuReco") << "Number of tracks found: " << Tracks.size() << std::endl;
110 
111  std::multimap<double, const reco::Track> TrackMultiMap;
112 
113  unsigned short muonCounter = 0;
114 
115  reco::TrackCollection::const_iterator track;
116 
117  for (track = Tracks.begin(); track != Tracks.end(); track++) {
118  if (check4MuonTrack(*track)) {
119  muonCounter++;
120  continue;
121  }
122 
123  SumDeltaR = MuonDeltaR;
124 
125  SumDeltaR += getDeltaR(m_TrackCollection->at(1), *track);
126  SumDeltaR += getDeltaR(*track, m_TrackCollection->at(0));
127 
128  std::pair<double, const reco::Track> actTrack(SumDeltaR, *track);
129 
130  TrackMultiMap.insert(actTrack);
131  }
132 
133  //two tracks should be clearly identified as muons by check4MuonTrack
134  //else event is not useable
135  if (muonCounter < 2) {
136  LogDebug("Tau3MuReco") << "Not enough muons (" << muonCounter << ") assigned to a track! Event skipped!"
137  << std::endl;
138  return false;
139  }
140 
141  std::multimap<double, const reco::Track>::iterator it = TrackMultiMap.begin();
142 
143  if (it == TrackMultiMap.end()) {
144  LogDebug("Tau3MuReco") << "Not enough tracks (0) left! Event skipped!" << std::endl;
145  return false;
146  }
147 
148  //get 2mu+track with minimal DeltaR Sum (MultiMaps are sorted)
149  m_TrackCollection->push_back((*it).second);
150 
151  //and check charge of this track
152  //and check invariant mass of this combination
153  //and make a vertex fit
154 
155  char Charge = m_TrackCollection->at(0).charge() * m_TrackCollection->at(1).charge();
156 
157  unsigned int count = 0;
158 
159  //Charge > 0 means the two muons have same charge, so the third track has to have the opposit charge
160  while ((Charge > 0 && ((*it).second).charge() == (m_TrackCollection->at(0)).charge()) ||
162  count++;
163 
164  LogDebug("Tau3MuReco") << "Track canidate: " << count << std::endl;
165 
166  if (Charge > 0 && ((*it).second).charge() != (m_TrackCollection->at(0)).charge())
167  LogDebug("Tau3MuReco") << "\tWrong charge!" << std::endl;
168  LogDebug("Tau3MuReco") << "\tInvariant Mass deviation! " << fabs(getInvariantMass(m_TrackCollection) - m_kTauMass)
169  << std::endl;
170  LogDebug("Tau3MuReco") << "\tTrack Pt: " << (*it).second.pt() << std::endl;
171  LogDebug("Tau3MuReco") << "\tDelta R: " << (*it).first << std::endl;
172  LogDebug("Tau3MuReco") << "\tChi2: " << ((*it).second).normalizedChi2() << std::endl;
173 
174  ++it;
175 
176  //was not the best canidate
177  m_TrackCollection->pop_back();
178 
179  if (it == TrackMultiMap.end())
180  return false;
181 
182  //get next to minimal (Delta R Sum) track
183  m_TrackCollection->push_back((*it).second);
184  }
185 
186  LogDebug("Tau3MuReco") << "Found corresponding 3rd track: " << std::endl;
187  LogDebug("Tau3MuReco") << "Track canidate: " << count << std::endl;
188  LogDebug("Tau3MuReco") << "\tInvariant Mass deviation! " << fabs(getInvariantMass(m_TrackCollection) - m_kTauMass)
189  << std::endl;
190  LogDebug("Tau3MuReco") << "\tDelta R: " << (*it).first << std::endl;
191  LogDebug("Tau3MuReco") << "\tNormChi2: " << ((*it).second).normalizedChi2() << std::endl;
192 
193  //choose this track, because it is the best canidate
194  return true;
195 }
196 
198  Combinatorics myCombinatorics(m_TrackCollection->size(), 3);
199 
200  std::vector<std::vector<UInt_t> > CombinationVec = myCombinatorics.GetCombinations();
201 
202  std::vector<std::vector<UInt_t> >::iterator it = CombinationVec.begin();
203 
204  char Charge = 0;
205 
206  reco::TrackCollection tempTrackCollection;
207  reco::MuonCollection tempMuonCollection;
208 
209  do {
210  if (it == CombinationVec.end())
211  return false;
212 
213  Charge = 0;
214 
215  tempMuonCollection.clear();
216  tempTrackCollection.clear();
217 
218  for (UInt_t i = 0; i < (*it).size(); i++) {
219  Charge += m_TrackCollection->at((*it).at(i)).charge();
220  tempTrackCollection.push_back(m_TrackCollection->at((*it).at(i)));
221  tempMuonCollection.push_back(m_MuonCollection->at((*it).at(i)));
222  }
223 
224  LogDebug("Tau3MuReco") << "Charge is: " << (int)Charge << " Have to be -1 or 1!!!" << std::endl;
225  LogDebug("Tau3MuReco") << "Invariant mass is: " << fabs(getInvariantMass(&tempTrackCollection) - m_kTauMass)
226  << " Have to be smaller than " << m_kTauMassCut << std::endl;
227 
228  it++;
229  } while (abs(Charge) != 1 || fabs(getInvariantMass(&tempTrackCollection) - m_kTauMass) > m_kTauMassCut);
230 
231  *m_MuonCollection = tempMuonCollection;
232  *m_TrackCollection = tempTrackCollection;
233 
234  return true;
235 }
236 
238  double deltaR12 = getDeltaR(m_TrackCollection->at(0), m_TrackCollection->at(1));
239  double deltaR23 = getDeltaR(m_TrackCollection->at(1), m_TrackCollection->at(2));
240  double deltaR31 = getDeltaR(m_TrackCollection->at(2), m_TrackCollection->at(0));
241 
242  //if DeltaR12 is the smallest, than the 3rd one seems to be wrong
243  //if DeltaR23 is the smallest, than the 2nd one seems to be wrong
244  //if DeltaR31 is the smallest, than the 1st one seems to be wrong
245 
246  unsigned char temp;
247  double junk;
248 
249  deltaR12 < deltaR23 ? temp = 3 : temp = 1;
250  deltaR12 < deltaR23 ? junk = deltaR12 : junk = deltaR23;
251 
252  if (deltaR31 < junk)
253  temp = 2;
254 
255  m_TrackCollection->erase(m_TrackCollection->begin() + temp - 1);
256 
257  return true;
258 }
259 
261  unsigned int numOfParticles = tracks->size();
262 
263  double SumPx = 0;
264  double SumPy = 0;
265  double SumPz = 0;
266 
267  double SumE = 0;
268 
269  for (unsigned int i = 0; i < numOfParticles; i++) {
270  SumPx += tracks->at(i).px();
271  SumPy += tracks->at(i).py();
272  SumPz += tracks->at(i).pz();
273 
274  SumE += sqrt(pow(tracks->at(i).p(), 2) + pow(MuonMass, 2));
275  }
276 
277  double invmass = sqrt(pow(SumE, 2) - pow(SumPx, 2) - pow(SumPy, 2) - pow(SumPz, 2));
278 
279  return invmass;
280 }
281 
282 double Tau3MuReco::getDeltaR(const reco::Track& track1, const reco::Track& track2) {
283  double dEta = track1.eta() - track2.eta();
284  double dPhi = track1.phi() - track2.phi();
285 
286  while (dPhi >= TMath::Pi())
287  dPhi -= (2.0 * TMath::Pi());
288  while (dPhi < (-1.0 * TMath::Pi()))
289  dPhi += (2.0 * TMath::Pi());
290 
291  return sqrt(pow(dEta, 2) + pow(dPhi, 2));
292 }
#define LogDebug(id)
Tau3MuReco(const edm::ParameterSet &iConfig, edm::ConsumesCollector &&iC)
Definition: Tau3MuReco.cc:9
const double Pi
double getDeltaR(const reco::Track &track1, const reco::Track &track2)
Definition: Tau3MuReco.cc:282
std::vector< std::vector< UInt_t > > GetCombinations()
double getInvariantMass(const reco::TrackCollection *tracks, const double MuonMass=0.106)
Definition: Tau3MuReco.cc:260
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:525
const double m_kTauMass
Definition: Tau3MuReco.h:38
bool find3rdTrack(const edm::Event &iEvent, const edm::EventSetup &iSetup, const reco::TrackCollection &Tracks)
Definition: Tau3MuReco.cc:92
const double m_kMatchingPt
Definition: Tau3MuReco.h:36
reco::TrackCollection * m_TrackCollection
Definition: Tau3MuReco.h:45
std::vector< Track > TrackCollection
collection of Tracks
Definition: TrackFwd.h:14
double phi() const
azimuthal angle of momentum vector
Definition: TrackBase.h:614
bool findCorrectPairing()
Definition: Tau3MuReco.cc:197
std::vector< Muon > MuonCollection
collection of Muon objects
Definition: MuonFwd.h:9
int iEvent
Definition: GenABIO.cc:224
double eta() const
pseudorapidity of momentum vector
Definition: TrackBase.h:617
const double m_kMatchingDeltaR
Definition: Tau3MuReco.h:35
T sqrt(T t)
Definition: SSEVec.h:19
double pt() const
track transverse momentum
Definition: TrackBase.h:602
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
bool doTau3MuReco(const edm::Event &iEvent, const edm::EventSetup &iSetup, reco::MuonCollection *muonCollection, reco::TrackCollection *trackCollection)
Definition: Tau3MuReco.cc:20
T const * product() const
Definition: Handle.h:69
const double m_kTauMassCut
Definition: Tau3MuReco.h:37
const double MuonMass
const double m_kMuonMass
Definition: Tau3MuReco.h:39
fixed size matrix
bool removeIncorrectMuon()
Definition: Tau3MuReco.cc:237
HLT enums.
const edm::EDGetTokenT< reco::MuonCollection > m_kMuonSourceToken
Definition: Tau3MuReco.h:41
const edm::EDGetTokenT< reco::TrackCollection > m_kTrackSourceToken
Definition: Tau3MuReco.h:42
reco::MuonCollection * m_MuonCollection
Definition: Tau3MuReco.h:44
int charge() const
track electric charge
Definition: TrackBase.h:575
bool check4MuonTrack(const reco::Track &track)
Definition: Tau3MuReco.cc:76
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:30