DESY Hbb Analysis Framework
Collection.cc
Go to the documentation of this file.
1 // system include files
2 #include <iostream>
3 #include <set>
4 //
5 #include "TRandom2.h"
6 // user include files
19 
21 
22 // member functions specialization - needed to be declared in the same namespace as the class
23 namespace analysis {
24  namespace tools {
25  template <> Collection<Vertex>::Collection(const Objects & objects, const std::string & name);
26  template <> std::vector<Candidate> * Collection<Vertex>::vectorCandidates() const;
27  template <> void Collection<Vertex>::matchTo( const std::vector<Candidate>* vectorcandidates, const std::string & name, const float & deltaR );
28  template <> void Collection<Vertex>::matchTo( const Collection<Candidate> & collection, const float & deltaR );
29  template <> void Collection<Vertex>::matchTo( const Collection<Candidate> & collection, const float & deltaR, const float & delta_pt);
30  template <> void Collection<Vertex>::matchTo( const Collection<TriggerObject> & collection, const float & deltaR );
31  template <> void Collection<Vertex>::matchTo( const std::shared_ptr<Collection<TriggerObject> > collection, const float & deltaR );
32  template <> void Collection<Jet>::matchTo( const Collection<Jet> & collection, const float & deltaR, const float & delta_pt);
33  template <> void Collection<Jet>::associatePartons( const std::shared_ptr<Collection<GenParticle> > & particles, const float & deltaR, const float & ptMin, const bool & pythia8 );
34  template <> void Collection<Jet>::btagAlgo( const std::string & algo );
35  template <> void Collection<Jet>::smearTo(const Collection<Jet> & collection, const double & n_sigma );
36  template <> void Collection<Jet>::addGenJets( const std::shared_ptr<Collection<GenJet> > & cands );
37  }
38 }
39 //
40 // class declaration
41 //
42 
43 using namespace analysis;
44 using namespace analysis::tools;
45 
46 bool pTordering(Candidate j1, Candidate j2) {return (j1.pt()>j2.pt());}
47 
48 //
49 // constructors and destructor
50 //
51 template <class Object>
53 {
54  size_ = 0;
55  candidates_.clear();
56 }
57 template <class Object>
58 Collection<Object>::Collection(const Objects & objects, const std::string & name)
59 {
60  objects_ = objects;
61  size_ = (int) objects_.size();
62  name_ = name;
63  candidates_.clear();
64  for ( int i = 0; i < size_ ; ++i ) candidates_.push_back(objects_[i]);
65 }
66 
67 template <>
68 Collection<Vertex>::Collection(const Objects & objects, const std::string & name)
69 {
70  objects_ = objects;
71  size_ = (int) objects_.size();
72  name_ = name;
73 }
74 
75 
76 template <class Object>
78 {
79 // std::cout<< this << " Collection destroyed" << std::endl;
80 
81  // do anything here that needs to be done at desctruction time
82  // (e.g. close files, deallocate resources etc.)
83 }
84 
85 
86 //
87 // member functions
88 //
89 //template <class Object>
90 //std::vector<Object> * Collection<Object>::vector()
91 template <class Object>
92 std::vector< std::shared_ptr<Object> > Collection<Object>::vector()
93 {
94  std::vector< std::shared_ptr<Object> > objects;
95  for ( auto & obj : objects_ )
96  {
97  std::shared_ptr<Object> sp_obj = std::shared_ptr<Object> ( new Object(obj));
98  objects.push_back(sp_obj);
99  }
100  return objects;
101 }
102 template <class Object>
103 void Collection<Object>::btagAlgo(const std::string & algo )
104 {
105 }
106 template <>
107 void Collection<Jet>::btagAlgo(const std::string & algo )
108 {
109  for ( auto & jet : objects_ )
110  jet.btagAlgo(algo);
111 }
112 
113 
114 template <class Object>
115 void Collection<Object>::addGenJets(const std::shared_ptr<Collection<GenJet> > & cands)
116 {
117 }
118 
119 template <>
120 void Collection<Jet>::addGenJets(const std::shared_ptr<Collection<GenJet> > & cands)
121 {
122  if ( objects_.size() < 1 ) return;
123  std::vector< std::shared_ptr<GenJet> > vec = cands->vector();
124  for ( auto & jet : objects_ )
125  jet.genJets(vec);
126 
127 }
128 
129 
130 template <class Object>
131 void Collection<Object>::associatePartons(const std::shared_ptr<Collection<GenParticle> > & particles, const float & deltaR, const float & ptMin, const bool & pythia8 )
132 {
133 }
134 
135 template <>
136 void Collection<Jet>::associatePartons(const std::shared_ptr<Collection<GenParticle> > & particles, const float & deltaR, const float & ptMin, const bool & pythia8 )
137 {
138  if ( objects_.size() < 1 ) return;
139  auto vec = particles->vector();
140 
141  for ( auto & jet : objects_ )
142  jet.associatePartons(vec,deltaR,ptMin,pythia8);
143 
144  // resolving ambiguities
145  // if a parton belongs to more than one jet, than remove it
146  // from the jet with largest deltaR wrt the parton.
147 // std::map<Jet *, std::vector<int> > removeFromJet;
148  std::map<Jet *, std::set<int> > removeFromJet;
149 
150  for ( size_t j1 = 0 ; j1 < objects_.size()-1 ; ++j1 )
151  {
152  Jet * jet1 = &(objects_.at(j1));
153  auto partons1 = jet1->partons();
154  for ( auto it1 = partons1.begin(); it1 != partons1.end(); it1++ )
155  {
156  auto parton1 = *it1;
157  auto p1 = std::distance(partons1.begin(), it1 );
158  for ( size_t j2 = j1+1 ; j2 < objects_.size() ; ++j2 )
159  {
160  Jet * jet2 = &(objects_.at(j2));
161  auto partons2 = jet2->partons();
162  for ( auto it2 = partons2.begin(); it2 != partons2.end(); ++it2 )
163  {
164  auto parton2 = *it2;
165  int p2 = std::distance(partons2.begin(), it2 );
166  if ( parton1 == parton2 )
167  {
168  float dR1 = jet1->p4().DeltaR(parton1->p4());
169  float dR2 = jet2->p4().DeltaR(parton2->p4());
170  if ( dR1 < dR2 ) { removeFromJet[jet2].insert(p2) ;}
171  else { removeFromJet[jet1].insert(p1) ;}
172 // else { removeFromJet.insert(std::pair<Jet*,int>(jet1, p1)) ;}
173  }
174  }
175  }
176 
177  }
178  }
179 
180  // Remove ambiguous partons...
181  for ( auto & jet : removeFromJet )
182  {
183  int counts = 0;
184  for ( auto & p : jet.second )
185  {
186  jet.first -> removeParton(p-counts);
187  ++counts;
188  }
189  }
190 
191 }
192 
193 
194 template <class Object>
195 void Collection<Object>::matchTo( const std::vector<Candidate>* vectorcandidates, const std::string & name, const float & deltaR )
196 {
197  for ( auto & obj : objects_ )
198  obj.matchTo(vectorcandidates,name,deltaR);
199 }
200 
201 template <class Object>
202 void Collection<Object>::matchTo( const Collection<Candidate> & collection, const float & deltaR )
203 {
204  for ( auto & obj : objects_ )
205  obj.matchTo(collection.vectorCandidates(),collection.name(), deltaR);
206 }
207 
208 template <class Object>
209 void Collection<Object>::matchTo( const Collection<Candidate> & collection, const float & delta_pT, const float & deltaR){
210  for (auto & obj : objects_){
211  obj.matchTo(collection.vectorCandidates(),collection.name(),delta_pT,deltaR);
212  }
213 }
214 
215 template <>
216 void Collection<Jet>::matchTo( const Collection<Jet> & collection, const float & delta_pT, const float & deltaR){
217  for (auto & obj : objects_){
218  obj.matchTo(collection.vectorCandidates(),collection.name(),delta_pT*obj.jerPtResolution()*obj.pt(),deltaR);
219  }
220 }
221 
222 template <class Object>
223 void Collection<Object>::matchTo( const Collection<TriggerObject> & collection, const float & deltaR )
224 {
225  for ( auto & obj : objects_ )
226  obj.matchTo(collection.vectorCandidates(),collection.name(),deltaR);
227 }
228 
229 template <class Object>
230 void Collection<Object>::matchTo( const std::shared_ptr<Collection<TriggerObject> > collection, const float & deltaR )
231 {
232  this->matchTo(*collection, deltaR);
233 }
234 
235 template <>
236 void Collection<Jet>::smearTo( const Collection<Jet> & collection, const double & n_sigma ){
237 
238  //Check for variation - n_sigma:
239  // +1 : 1 Sigma Up variation -1 : 1 Sigma Down variation
240  // +2 : 2 Sigma Up variation -2 : 2 Sigma Down variation
241  //TODO!
242  //Very temprorary solution. Should be changed to jerSf(n_sigma)
243  // and return already a variation, but could be a conflict with
244  // setter...!!!
245 
246  TLorentzVector out;
247  double smear_pt = 0;
248  double smear_e = 0;
249  double sf = 0;
250  for(auto & jet : objects_){
251  if(n_sigma >= 0){
252  sf = n_sigma * (jet.jerSFup() - jet.jerSF()) + jet.jerSF();
253  }
254  else if (n_sigma < 0){
255  sf = std::abs(n_sigma) * (jet.jerSFdown() - jet.jerSF()) + jet.jerSF();
256  }
257 // std::cout<<"\nBefore smearing: "<<jet.px()<<" "<<jet.py()<<" "<<jet.pt()<<std::endl;
258  if(jet.matched(collection.name())){
259  smear_pt = jet.matched(collection.name())->pt() + sf * (jet.pt() - jet.matched(collection.name())->pt());
260  smear_pt = std::max(0.,smear_pt);
261  smear_e = jet.matched(collection.name())->e() + sf * (jet.e() - jet.matched(collection.name())->e());
262  smear_e = std::max(0.,smear_e);
263 // std::cout<<"\n n_sigma = "<<n_sigma<<" sf = "<<sf<<" Delta = "<<(jet.pt() - jet.matched(collection.name())->pt())<<" smeared = "<<sf * (jet.pt() - jet.matched(collection.name())->pt())<<std::endl;
264 // std::cout<<"Pt: ini = "<<jet.pt()<<" smeared = "<<smear_pt<< std::endl;
265  }
266  else {
267  if(sf > 1) {
268  smear_pt = gRandom->Gaus(jet.pt(),std::sqrt(sf*sf-1)*jet.jerPtResolution()*jet.pt());
269  smear_e = gRandom->Gaus(jet.e(),std::sqrt(sf*sf-1)*jet.jerPtResolution()*jet.pt());
270  }
271  else {
272  smear_pt = jet.pt();
273  smear_e = jet.e();
274  }
275  }
276  out.SetPtEtaPhiE(smear_pt,jet.eta(),jet.phi(),smear_e);
277  jet.p4(out);
278 // std::cout<<"After smearing: "<<jet.px()<<" "<<jet.py()<<" "<<jet.pt()<<std::endl;
279 
280  }
281  //Re-ordering
282  std::sort(objects_.begin(),objects_.end(),pTordering);
283 }
284 
285 template <class Object>
286 std::vector<Candidate>* Collection<Object>::vectorCandidates() const
287 {
288  return &candidates_;
289 }
290 
291 // try to find how the enable_if works to avoid this specialization
292 // typename std::enable_if<std::is_base_of<Candidate, Object>::value, std::vector<Candidate>* >::type
293 // std::is_base_of<Foo, Bar>::value
294 template <>
295 std::vector<Candidate> * Collection<Vertex>::vectorCandidates() const
296 {
297  return nullptr;
298 }
299 // ------------ method called for each event ------------
300 
301 
302 // ------------ declare the template classes ------------
303 template class Collection<Candidate>;
304 template class Collection<Jet>;
305 template class Collection<MET>;
306 template class Collection<Muon>;
307 template class Collection<Vertex>;
308 template class Collection<TriggerObject>;
309 template class Collection<GenParticle>;
310 template class Collection<GenJet>;
311 template class Collection<JetTag>;
312 template class Collection<L1TMuon>;
313 template class Collection<L1TJet>;
314 template class Collection<RecoMuon>;
315 template class Collection<RecoTrack>;
TLorentzVector p4() const
returns the 4-momentum (TLorentzVector)
Definition: Candidate.cc:143
std::vector< Candidate > * vectorCandidates() const
Definition: Collection.cc:286
std::vector< std::shared_ptr< GenParticle > > partons() const
returns the vector of pointers to the generated partons
Definition: Jet.cc:65
bool pTordering(Candidate j1, Candidate j2)
Definition: Collection.cc:46
void btagAlgo(const std::string &)
Definition: Collection.cc:103
std::string name() const
Definition: Collection.h:89
float pt() const
returns the transverse momentum
Definition: Candidate.cc:133
void associatePartons(const std::shared_ptr< Collection< GenParticle > > &, const float &deltaR=0.4, const float &ptMin=1., const bool &pythia8=true)
Definition: Collection.cc:131
void matchTo(const std::vector< Candidate > *vectorcandidates, const std::string &name, const float &deltaR=0.5)
Definition: Collection.cc:195
void addGenJets(const std::shared_ptr< Collection< GenJet > > &)
Definition: Collection.cc:115
std::vector< Object > Objects
Definition: Collection.h:43
void smearTo(const Collection< Jet > &collection, const double &n_sigma=0)
std::vector< std::shared_ptr< Object > > vector()
Definition: Collection.cc:92