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  src_(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 
28 
31 {
32  // declare output
33  tnp::TagProbePairs pairs;
34 
35  // read from event
37  iEvent.getByLabel(src_, src);
38 
39  // convert
40  for (reco::CandidateView::const_iterator it = src->begin(), ed = src->end(); it != ed; ++it) {
41  const reco::Candidate & mother = *it;
42  if (mother.numberOfDaughters() != 2) throw cms::Exception("CorruptData") << "Tag&Probe pair with " << mother.numberOfDaughters() << " daughters\n";
43  pairs.push_back(tnp::TagProbePair(mother.daughter(0)->masterClone(), mother.daughter(1)->masterClone(),
44  src->refAt(it - src->begin()), mother.mass()));
45  }
46 
47  if ((arbitration_ != None) && (pairs.size() > 1)) {
48  // might need to clean up
49  arbitrate(pairs);
50  }
51 
52  // return
53  return pairs;
54 }
55 
56 void
58 {
59  size_t nclean = pairs.size();
60  for (TagProbePairs::iterator it = pairs.begin(), ed = pairs.end(); it != ed; ++it) {
61  if (it->tag.isNull()) continue; // skip already invalidated pairs
62 
63  bool TTpair=false;
64  for (TagProbePairs::iterator it2 = pairs.begin(); it2 != ed; ++it2) { // first check for Tag-Tag pairs
65  if(it!=it2 && it->probe==it2->tag && it->tag==it2->probe){
66  //std::cout << "----------> probe is tag!!!!" << std::endl;
67  TTpair=true;
68  }
69  }
70  //if(!TTpair) std::cout << "this is not TT!" << std::endl;
71  bool invalidateThis = false;
72  int numberOfProbes=0;
73  for (TagProbePairs::iterator it2 = it + 1; it2 != ed; ++it2) { // it+1 <= ed, otherwise we don't arrive here
74  // arbitrate the case where multiple probes are matched to the same tag
75  if ((arbitration_ != NonDuplicate) && (it->tag == it2->tag)) {
76  if(TTpair){ // we already have found a TT pair, no need to guess
77  it2->tag = reco::CandidateBaseRef(); --nclean;
78  //std::cout << "remove unnecessary pair! -----------" << std::endl;
79  continue;
80  }
81 
82  if (arbitration_ == OneProbe) {
83  // invalidate this one
84  it2->tag = reco::CandidateBaseRef(); --nclean;
85  // remember to invalidate also the other one (but after finishing to check for duplicates)
86  invalidateThis = true;
87  } else if (arbitration_ == BestMass) {
88  // but the best one in the first iterator
89  if (fabs(it2->mass-arbitrationMass_) < fabs(it->mass-arbitrationMass_)) {
90  std::swap(*it, *it2);
91  }
92  // and invalidate it2
93  it2->tag = reco::CandidateBaseRef(); --nclean;
94  } else if (arbitration_ == Random2) {
95  numberOfProbes++;
96  if (numberOfProbes>1) {
97  //std::cout << "more than 2 probes!" << std::endl;
98  invalidateThis=true;
99  it2->tag = reco::CandidateBaseRef();
100  --nclean;
101  } else{
102  // do a coin toss to decide if we want to swap them
103  if (randGen_->Rndm()>0.5) {
104  std::swap(*it, *it2);
105  }
106  // and invalidate it2
107  it2->tag = reco::CandidateBaseRef();
108  --nclean;
109  }
110  }
111  }
112  // arbitrate the case where the same probe is associated to more then one tag
113  if ((arbitration_ == NonDuplicate) && (it->probe == it2->probe)) {
114  // invalidate the pair in which the tag has lower pT
115  if (it2->tag->pt() > it->tag->pt()) std::swap(*it, *it2);
116  it2->tag = reco::CandidateBaseRef(); --nclean;
117  }
118  // arbitrate the OnePair case: disallow the same pair to enter the lineshape twice
119  // this can't be done by reference, unfortunately, so we resort to a simple matching
120  if ((arbitration_ == OnePair) &&
121  std::abs(it->mass - it2->mass) < 1e-4 &&
122  std::abs( it->probe->phi() - it2->tag->phi()) < 1e-5 &&
123  std::abs( it->probe->eta() - it2->tag->eta()) < 1e-5 &&
124  std::abs( it->probe->pt() - it2->tag->pt() ) < 1e-5 &&
125  std::abs(it2->probe->phi() - it->tag->phi()) < 1e-5 &&
126  std::abs(it2->probe->eta() - it->tag->eta()) < 1e-5 &&
127  std::abs(it2->probe->pt() - it->tag->pt() ) < 1e-5) {
128  it2->tag = reco::CandidateBaseRef(); --nclean;
129  }
130  }
131  if (invalidateThis) { it->tag = reco::CandidateBaseRef(); --nclean; }
132  }
133 
134  if (nclean == 0) {
135  pairs.clear();
136  } else if (nclean < pairs.size()) {
137  TagProbePairs cleaned; cleaned.reserve(nclean);
138  for (TagProbePairs::iterator it = pairs.begin(), ed = pairs.end(); it != ed; ++it) {
139  if (it->tag.isNonnull()) cleaned.push_back(*it);
140  }
141  pairs.swap(cleaned);
142  }
143 }
T getParameter(std::string const &) const
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:81
virtual const Candidate * daughter(size_type i) const =0
return daughter at a given position, i = 0, ... numberOfDaughters() - 1 (read only mode) ...
std::vector< TagProbePair > TagProbePairs
virtual double mass() const =0
mass
#define abs(x)
Definition: mlp_lapack.h:159
virtual size_type numberOfDaughters() const =0
number of daughters
int iEvent
Definition: GenABIO.cc:243
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:356
TagProbePairMaker(const edm::ParameterSet &iConfig)
edm::RefToBase< Candidate > CandidateBaseRef
persistent reference to an object in a collection of Candidate objects
Definition: CandidateFwd.h:31
TagProbePairs run(const edm::Event &iEvent) const
fill in tghe T&amp;P pairs for this event
a simple struct to hold tag, probe and mass
void arbitrate(TagProbePairs &pairs) const
virtual const CandidateBaseRef & masterClone() const =0