CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TagProbePairMaker.cc
Go to the documentation of this file.
3 
5  srcToken_(iC.consumes<reco::CandidateView>(iConfig.getParameter<edm::InputTag>("tagProbePairs"))),
6  randGen_(0)
7 {
8  std::string arbitration = iConfig.getParameter<std::string>("arbitration");
9  if (arbitration == "None") {
11  } else if (arbitration == "OneProbe") {
13  } else if (arbitration == "OnePair") {
15  } else if (arbitration == "NonDuplicate") {
17  } else if (arbitration == "BestMass") {
19  arbitrationMass_ = iConfig.getParameter<double>("massForArbitration");
20  } else if (arbitration == "Random2") {
22  randGen_ = new TRandom2(7777);
23  } else throw cms::Exception("Configuration") << "TagProbePairMakerOnTheFly: the only currently "
24  << "allowed values for 'arbitration' are "
25  << "'None', 'OneProbe', 'BestMass', 'Random2'\n";
26 
27  if (iConfig.existsAs<bool>("phiCutForTwoLeg")) {
28  phiCutForTwoLeg_ = iConfig.getParameter<bool>("phiCutForTwoLeg");
29  //std::cout << "Set phiCutForTwoLeg_ to " << phiCutForTwoLeg_ << std::endl;
30  } else {
31  phiCutForTwoLeg_ = false;
32  //std::cout << "Set phiCutForTwoLeg_ to default " << phiCutForTwoLeg_ << std::endl;
33  }
34 }
35 
36 
39 {
40  // declare output
41  tnp::TagProbePairs pairs;
42 
43  // read from event
45  iEvent.getByToken(srcToken_, src);
46 
47  // convert
48  for (reco::CandidateView::const_iterator it = src->begin(), ed = src->end(); it != ed; ++it) {
49  const reco::Candidate & mother = *it;
50  if (mother.numberOfDaughters() != 2) throw cms::Exception("CorruptData") << "Tag&Probe pair with " << mother.numberOfDaughters() << " daughters\n";
51  pairs.push_back(tnp::TagProbePair(mother.daughter(0)->masterClone(), mother.daughter(1)->masterClone(),
52  src->refAt(it - src->begin()), mother.mass()));
53  }
54 
55  if ((arbitration_ != None) && (pairs.size() > 1)) {
56  // might need to clean up
57  arbitrate(pairs);
58  }
59 
60  if (phiCutForTwoLeg_ && pairs.size() > 0) {
61  int eventNum = iEvent.id().event();
62  std::cout << "Calling phiCutByEventNumber on eventNum=" << eventNum << std::endl;
63  phiCutByEventNumber(pairs,eventNum);
64  }
65 
66  // return
67  return pairs;
68 }
69 
70 void
72 {
73  unsigned int currentNum = 0;
74 
75  size_t nclean = pairs.size();
76  for (TagProbePairs::iterator it = pairs.begin(), ed = pairs.end(); it != ed; ++it) {
77  if (it->tag.isNull()) continue; // skip already invalidated pairs
78  if (eventNumber%2) {
79  std::cout << "Odd event number " << eventNumber << ", require 0 < phi(tag) < pi... ";
80  if (!(it->tag->phi() > 0. && it->tag->phi() < 3.141592654)) {
81  std::cout << "Rejecting pair number " << currentNum++ << " with tag phi " << it->tag->phi();
82  nclean--;
83  it->tag = reco::CandidateBaseRef(); --nclean;
84  } else {
85  std::cout << "Keeping pair number " << currentNum++ << " with tag phi " << it->tag->phi();
86  }
87  } else {
88  std::cout << "Even event number " << eventNumber << ", require -pi < phi(tag) < 0... ";
89  // if (!(it->tag->phi() > 3.141592654 && it->tag->phi() < 2*3.141592654)) {
90  if (!(it->tag->phi() > -3.141592654 && it->tag->phi() < 0)) {
91  std::cout << "Rejecting pair number " << currentNum++ << " with tag phi " << it->tag->phi();
92  nclean--;
93  it->tag = reco::CandidateBaseRef(); --nclean;
94  } else {
95  std::cout << "Keeping pair number " << currentNum++ << " with tag phi " << it->tag->phi();
96  }
97  }
98  std::cout << std::endl;
99  }
100 
101  if (nclean == 0) {
102  pairs.clear();
103  } else if (nclean < pairs.size()) {
104  TagProbePairs cleaned; cleaned.reserve(nclean);
105  for (TagProbePairs::iterator it = pairs.begin(), ed = pairs.end(); it != ed; ++it) {
106  if (it->tag.isNonnull()) cleaned.push_back(*it);
107  }
108  pairs.swap(cleaned);
109  }
110 }
111 
112 void
114 {
115  size_t nclean = pairs.size();
116  for (TagProbePairs::iterator it = pairs.begin(), ed = pairs.end(); it != ed; ++it) {
117  if (it->tag.isNull()) continue; // skip already invalidated pairs
118 
119  bool TTpair=false;
120  for (TagProbePairs::iterator it2 = pairs.begin(); it2 != ed; ++it2) { // first check for Tag-Tag pairs
121  if(it!=it2 && it->probe==it2->tag && it->tag==it2->probe){
122  //std::cout << "----------> probe is tag!!!!" << std::endl;
123  TTpair=true;
124  }
125  }
126  //if(!TTpair) std::cout << "this is not TT!" << std::endl;
127  bool invalidateThis = false;
128  int numberOfProbes=0;
129  for (TagProbePairs::iterator it2 = it + 1; it2 != ed; ++it2) { // it+1 <= ed, otherwise we don't arrive here
130  // arbitrate the case where multiple probes are matched to the same tag
131  if ((arbitration_ != NonDuplicate) && (it->tag == it2->tag)) {
132  if(TTpair){ // we already have found a TT pair, no need to guess
133  it2->tag = reco::CandidateBaseRef(); --nclean;
134  //std::cout << "remove unnecessary pair! -----------" << std::endl;
135  continue;
136  }
137 
138  if (arbitration_ == OneProbe) {
139  // invalidate this one
140  it2->tag = reco::CandidateBaseRef(); --nclean;
141  // remember to invalidate also the other one (but after finishing to check for duplicates)
142  invalidateThis = true;
143  } else if (arbitration_ == BestMass) {
144  // but the best one in the first iterator
145  if (fabs(it2->mass-arbitrationMass_) < fabs(it->mass-arbitrationMass_)) {
146  std::swap(*it, *it2);
147  }
148  // and invalidate it2
149  it2->tag = reco::CandidateBaseRef(); --nclean;
150  } else if (arbitration_ == Random2) {
151  numberOfProbes++;
152  if (numberOfProbes>1) {
153  //std::cout << "more than 2 probes!" << std::endl;
154  invalidateThis=true;
155  it2->tag = reco::CandidateBaseRef();
156  --nclean;
157  } else{
158  // do a coin toss to decide if we want to swap them
159  if (randGen_->Rndm()>0.5) {
160  std::swap(*it, *it2);
161  }
162  // and invalidate it2
163  it2->tag = reco::CandidateBaseRef();
164  --nclean;
165  }
166  }
167  }
168  // arbitrate the case where the same probe is associated to more then one tag
169  if ((arbitration_ == NonDuplicate) && (it->probe == it2->probe)) {
170  // invalidate the pair in which the tag has lower pT
171  if (it2->tag->pt() > it->tag->pt()) std::swap(*it, *it2);
172  it2->tag = reco::CandidateBaseRef(); --nclean;
173  }
174  // arbitrate the OnePair case: disallow the same pair to enter the lineshape twice
175  // this can't be done by reference, unfortunately, so we resort to a simple matching
176  if ((arbitration_ == OnePair) &&
177  std::abs(it->mass - it2->mass) < 1e-4 &&
178  std::abs( it->probe->phi() - it2->tag->phi()) < 1e-5 &&
179  std::abs( it->probe->eta() - it2->tag->eta()) < 1e-5 &&
180  std::abs( it->probe->pt() - it2->tag->pt() ) < 1e-5 &&
181  std::abs(it2->probe->phi() - it->tag->phi()) < 1e-5 &&
182  std::abs(it2->probe->eta() - it->tag->eta()) < 1e-5 &&
183  std::abs(it2->probe->pt() - it->tag->pt() ) < 1e-5) {
184  it2->tag = reco::CandidateBaseRef(); --nclean;
185  }
186  }
187  if (invalidateThis) { it->tag = reco::CandidateBaseRef(); --nclean; }
188  }
189 
190  if (nclean == 0) {
191  pairs.clear();
192  } else if (nclean < pairs.size()) {
193  TagProbePairs cleaned; cleaned.reserve(nclean);
194  for (TagProbePairs::iterator it = pairs.begin(), ed = pairs.end(); it != ed; ++it) {
195  if (it->tag.isNonnull()) cleaned.push_back(*it);
196  }
197  pairs.swap(cleaned);
198  }
199 }
T getParameter(std::string const &) const
EventNumber_t event() const
Definition: EventID.h:41
virtual const Candidate * daughter(size_type i) const =0
return daughter at a given position, i = 0, ... numberOfDaughters() - 1 (read only mode) ...
bool existsAs(std::string const &parameterName, bool trackiness=true) const
checks if a parameter exists as a given type
Definition: ParameterSet.h:186
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:464
std::vector< TagProbePair > TagProbePairs
virtual double mass() const =0
mass
virtual size_type numberOfDaughters() const =0
number of daughters
int iEvent
Definition: GenABIO.cc:230
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
edm::RefToBase< Candidate > CandidateBaseRef
persistent reference to an object in a collection of Candidate objects
Definition: CandidateFwd.h:31
edm::EventID id() const
Definition: EventBase.h:60
TagProbePairs run(const edm::Event &iEvent) const
fill in tghe T&amp;P pairs for this event
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:85
tuple cout
Definition: gather_cfg.py:121
a simple struct to hold tag, probe and mass
TagProbePairMaker(const edm::ParameterSet &iConfig, edm::ConsumesCollector &&iC)
void phiCutByEventNumber(TagProbePairs &pairs, int eventNumber) const
void arbitrate(TagProbePairs &pairs) const
virtual const CandidateBaseRef & masterClone() const =0