ATLAS Offline Software
Jet.icc
Go to the documentation of this file.
1 // emacs, this is -*- C++ -*-
2 
3 /*
4  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
5 */
6 
7 /////////// ///////////
8 // Public: Signal State //
9 /////////// ///////////
10 
11 #include <sstream>
12 #include <algorithm>
13 
14 inline const std::vector<Jet::mkey_t> Jet::infoKeys() const
15 {
16  // the vector might has empty elements, because not necessarily all
17  // taginfos are filled for every event
18  std::vector<mkey_t> vKeys;
19  if ( bool(m_tagInfoStore) )
20  for ( tagstore_t::iterator itr = m_tagInfoStore->begin();
21  itr != m_tagInfoStore->end(); ++itr )
22  if ( bool(*itr) )
23  vKeys.push_back( (*itr)->infoType() );
24  return vKeys;
25 }
26 
27 inline Jet::signalstate_t Jet::constituentSignalState() const
28 {
29  return m_constituentSigState;
30 }
31 
32 
33 /////////// ///////////
34 // Public: Four-momentum Setters //
35 /////////// ///////////
36 
37 inline void Jet::setP(const hlv_t& rVec) { this->set4Mom(rVec); }
38 
39 
40 /////////// ///////////
41 // Public: Jet Algebra Operators //
42 /////////// ///////////
43 
44 
45 inline void Jet::setE (double e ) { this->momentumBase().setE (e ); }
46 inline void Jet::setPx(double px) { this->momentumBase().setPx(px); }
47 inline void Jet::setPy(double py) { this->momentumBase().setPy(py); }
48 inline void Jet::setPz(double pz) { this->momentumBase().setPz(pz); }
49 
50 inline void Jet::setE (double e , signalstate_t s) { this->momentumBase(s).setE (e ); }
51 inline void Jet::setPx(double px, signalstate_t s) { this->momentumBase(s).setPx(px); }
52 inline void Jet::setPy(double py, signalstate_t s) { this->momentumBase(s).setPy(py); }
53 inline void Jet::setPz(double pz, signalstate_t s) { this->momentumBase(s).setPz(pz); }
54 
55 inline bool Jet::operator==(const Jet& rJet) const
56 { return this->isIdentical(rJet); }
57 inline bool Jet::operator==(const Jet* pJet) const
58 { return this->isIdentical(pJet); }
59 inline bool Jet::operator!=(const Jet& rJet) const
60 { return !(this->isIdentical(rJet)); }
61 inline bool Jet::operator!=(const Jet* pJet) const
62 { return !(this->isIdentical(pJet)); }
63 
64 inline bool Jet::isIdentical(const Jet& rJet) const
65 { return this->isIdentical(&rJet); }
66 
67 inline Jet& Jet::operator=(const Jet& rJet)
68 {
69  // Check for illegal self-assignment
70  // Otherwise constituents will be duplicated
71  if(this != &rJet)
72  copy_from_jet(&rJet, CopyAll);
73  return *this;
74 }
75 
76 
77 /////////// ///////////
78 // Public: Access to Constituents //
79 /////////// ///////////
80 
81 // inline const Jet::constituent_coll_t& Jet::getConstituentStore() const
82 // { return *((this->navigableBase()).getPtrToConstituents()); }
83 
84 inline Jet::const_iterator Jet::begin() const
85 { return (this->navigableBase()).begin(); }
86 inline Jet::const_iterator Jet::end() const
87 { return (this->navigableBase()).end(); }
88 
89 inline size_t Jet::size() const
90 { return (this->navigableBase()).size(); }
91 
92 inline bool Jet::contains(const constituent_t* pConst) const
93 { return (this->navigableBase()).contains(pConst); }
94 inline bool Jet::contains(const collection_t* pColl,index_t ind) const
95 { return (this->navigableBase()).contains(pColl,ind); }
96 
97 inline double Jet::getWeight(const constituent_t* pConst) const
98 { return this->getParameter(pConst); }
99 inline double Jet::getWeight(const collection_t* pColl,index_t ind) const
100 { return this->getParameter(pColl,ind); }
101 inline double Jet::getWeight(const_iterator fConst) const
102 { return this->getParameter(fConst); }
103 
104 inline Jet* Jet::getOverlap(const Jet& rJet, bool noKine /*= false*/) const
105 { return this->getOverlap(&rJet, noKine); }
106 
107 /** @detail Iterator to first constituent.
108  @warning To be phased out! Use @c Jet::begin() instead!
109 */
110 inline Jet::const_iterator Jet::firstConstituent() const
111 { return this->begin(); }
112 /** @detail Terminating constituent iterator.
113  @warning To be phased out! Use @c Jet::end() instead!
114  */
115 inline Jet::const_iterator Jet::lastConstituent() const
116 { return this->end(); }
117 
118 /////////// ///////////
119 // Protected: Access to Constituents //
120 /////////// ///////////
121 
122 inline double Jet::getParameter(const constituent_t* pConst) const
123 { return (this->navigableBase()).getParameter(pConst); }
124 inline double Jet::getParameter(const collection_t* pColl,index_t ind) const
125 { return (this->navigableBase()).getParameter(pColl,ind); }
126 inline double Jet::getParameter(const_iterator fConst) const
127 { return (this->navigableBase()).getParameter(fConst); }
128 
129 inline const Jet::collection_t*
130 Jet::getContainer(const constituent_t* pConst) const
131 { return (this->navigableBase()).getContainer(pConst); }
132 inline const Jet::collection_t*
133 Jet::getContainer(const_iterator fConst) const
134 { return (this->navigableBase()).getContainer(fConst); }
135 
136 inline bool Jet::getIndex(const constituent_t* pConst,index_t& ind) const
137 { return (this->navigableBase()).getIndex(pConst,ind); }
138 inline bool Jet::getIndex(const_iterator fConst,index_t& ind) const
139 { return (this->navigableBase()).getIndex(fConst,ind); }
140 
141 /////////// ///////////
142 // Public: Constituent Management //
143 /////////// ///////////
144 
145 inline bool Jet::removeConstituent(const collection_t* pColl,index_t ind)
146 {
147  return
148  ind < pColl->size()
149  ? this->removeConstituent((pColl->operator[])(ind))
150  : false;
151 }
152 
153 inline bool Jet::removeConstituent(bool reset)
154 {
155  // reset four-momentum if needed
156  if ( reset ) this->set4Mom(hlv_t(0.,0.,0.,0.));
157  return this->remove();
158 }
159 
160 /////////// ///////////
161 // Protected: Constituent Management //
162 /////////// ///////////
163 
164 inline void Jet::putElement(const collection_t* pColl,
165  const constituent_t* pConst,
166  double wght,
167  size_t sizeHint)
168 { (this->navigableBase()).putElement(pColl,pConst,wght,sizeHint); m_constituentsN++;}
169 inline void Jet::putElement(const collection_t* pColl,
170  index_t ind,
171  double wght,
172  size_t sizeHint)
173 { (this->navigableBase()).putElement(pColl,ind,wght,sizeHint); m_constituentsN++;}
174 
175 inline void Jet::insertElement(const collection_t* pColl,
176  const constituent_t* pConst,
177  double wght,
178  size_t sizeHint)
179 { (this->navigableBase()).insertElement(pColl,pConst,wght,sizeHint); m_constituentsN++;}
180 inline void Jet::insertElement(const collection_t* pColl,
181  index_t ind,
182  double wght,
183  size_t sizeHint)
184 { (this->navigableBase()).insertElement(pColl,ind,wght,sizeHint); m_constituentsN++;}
185 
186 // inline void Jet::reweight(const constituent_t* pConst,double wght)
187 // { (this->navigableBase()).reweight(pConst,wght); }
188 // inline void Jet::reweight(const collection_t* pColl,index_t ind,double wght)
189 // { (this->navigableBase()).reweight(pColl,ind,wght); }
190 
191 inline bool Jet::remove(const constituent_t* pConst)
192 { if ( (this->navigableBase()).remove(pConst) ) {m_constituentsN--;return true;} return false; }
193 inline bool Jet::remove(const collection_t* pColl,index_t ind)
194 { if ( (this->navigableBase()).remove(pColl,ind) ){ m_constituentsN--;return true;} return false; }
195 inline bool Jet::remove()
196 { if ( (this->navigableBase()).removeAll() ){ m_constituentsN=0;return true;} return false;}
197 
198 /////////// ///////////
199 // Public: Navigation //
200 /////////// ///////////
201 
202 inline void Jet::fillToken(INavigationToken& navToken) const
203 { (this->navigableBase()).fillToken(navToken); }
204 inline void Jet::fillToken(INavigationToken& navToken,
205  const std::any& par) const
206 { (this->navigableBase()).fillToken(navToken,par); }
207 
208 /////////// ///////////
209 // Public: Access to Jet Shapes //
210 /////////// ///////////
211 
212 inline void Jet::setMoment(const mkey_t& shapeName,shape_t shape,
213  bool createIfMissing) const
214 { this->setShape(shapeName,shape,createIfMissing); }
215 
216 inline Jet::shape_t Jet::getMoment(const mkey_t& shapeName,
217  bool createIfMissing) const
218 { return this->getShape(shapeName,createIfMissing); }
219 
220 inline std::vector<Jet::mkey_t> Jet::getMomentKeys() const
221 { return this->getShapeKeys(); }
222 
223 
224 inline bool Jet::checkKey(const mcat_t& cat,
225  const mkey_t& key,
226  size_t& aInd,
227  bool createIfMissing) const
228 {
229  // get index for data word
230  aInd = keyDesc()->getIndex(cat,key,createIfMissing);
231  return keyDesc()->isValid(aInd);
232 }
233 
234 /////////// ///////////
235 // Public: Access to Tag Info //
236 /////////// ///////////
237 
238 inline const Jet::tagstore_t Jet::jetTagInfoVector(void) const
239 {
240 
241  //BRANCHmerge
242  tagstore_t store;
243  //
244  // the vector might has empty elements, because not necessarily all
245  // taginfos are filled for every event. Need to return a copy
246  // of the TagInfoStore with all zeros removed
247  if ( bool(m_tagInfoStore) )
248  for (const taginfo_t* tag : std::as_const(*m_tagInfoStore)) {
249  if ( tag != nullptr )
250  store.push_back( tag );
251  }
252  return store;
253 
254 }
255 
256 inline const Jet::taginfo_t* Jet::tagInfo(const mkey_t& key) const
257 {
258  size_t aInd(keyDesc()->getIndex(JetKeyConstants::TagCat,key,false));
259  return bool(m_tagInfoStore) && aInd < m_tagInfoStore->size()
260  ? (m_tagInfoStore->operator[])(aInd) : (const taginfo_t*)0;
261 
262 }
263 
264 template<typename TAGINFO>
265 const TAGINFO* Jet::getTagInfo(const mkey_t& key,bool /*useLink*/) const
266 {
267  return this->getObject<TAGINFO,tagstore_t>(key,m_tagInfoStore,JetKeyConstants::TagCat, false);
268 }
269 
270 template<class TAGINFO>
271 const TAGINFO* Jet::tagInfo(const mkey_t& key) const
272 {
273  return this->getTagInfo<TAGINFO>(key);
274 }
275 
276 template<class TAGINFO>
277 void Jet::addInfo(const TAGINFO* tag)
278 {
279  this->setTagInfo( tag->infoType(), tag );
280 }
281 
282 
283 
284 
285 /////////// ///////////
286 // Public: Associations //
287 /////////// ///////////
288 
289 template<typename ASSOC> const ASSOC*
290 Jet::getAssociation(const mkey_t& key, bool /*useLink*/ ) const
291 {
292  return this->getObject<ASSOC,assostore_t>(key,m_assocStore,JetKeyConstants::AssoCat, false);
293 }
294 
295 template <class T>
296 void Jet::setAssociation(const T* pAssoc,bool /*useLink*/)
297 {
298  // use check function
299  size_t aInd(size_t(-1));
300  std::string key = pAssoc->name();
301  //
302  if ( ! bool(m_assocStore) )
303  // checkKey automatically build new store !!!
304  m_assocStore = new assostore_t();
305 
306  if ( this->checkKeyStore(JetKeyConstants::AssoCat,key,m_assocStore,aInd, true) ) {
307  removeAssociation(aInd);
308  (m_assocStore->operator[])(aInd) = pAssoc;
309  }
310 }
311 
312 inline void Jet::removeAssociation( const mkey_t& key )
313 {
314  if ( bool(m_assocStore) ) {
315  size_t aInd;
316  if ( this->checkKeyStore(JetKeyConstants::AssoCat,key,m_assocStore,aInd, false) ) {
317  removeAssociation(aInd);
318  }
319  }
320 }
321 
322 inline void Jet::removeAssociation (const size_t index)
323 {
324  delete (m_assocStore->operator[])(index);
325  (m_assocStore->operator[])(index) = 0;
326 }
327 
328 /////////////////////////////////////////
329 /// constituent and jet signal states ///
330 /////////////////////////////////////////
331 inline void Jet::setConstituentSignalState( Jet::signalstate_t s )
332 {
333  m_constituentSigState = s;
334 }
335 
336 /////////////////////////////////////////
337 /// Compatibility with JetAnalysisEDM
338 /////////////////////////////////////////
339 
340 // Write helper class to allow specialization for std::vector<T>
341 template <typename T>
342 struct JetAnalysisEDMHelper {
343  static T get(const Jet* j, const std::string & mName){
344  return j->getMoment(mName);
345  }
346  static void set(const Jet* j, const std::string & mName, const T & v){
347  j->setMoment(mName,v);
348  }
349 };
350 
351 template <typename T>
352 struct JetAnalysisEDMHelper<std::vector<T> > {
353  static std::vector<T> get(const Jet* j, const std::string & mName){
354  std::vector<std::string> allMoms = j->getMomentKeys() ;
355  std::vector<std::string> matchingMoms;
356 
357  std::vector<T> val;
358 
359  std::string prefix = mName+"_";
360  size_t msize = prefix.size();
361  //size_t nMatch=0;
362 
363  // match all moments starting by "mName_"
364  for( size_t i=0; i< allMoms.size(); i++){
365  std::string & mom = allMoms[i];
366  if(mom.compare(0,msize, prefix)==0) matchingMoms.push_back(mom);
367  }
368  // We need "mName_0", "mName_1", etc... but matchingMoms also contains moments
369  // in the form "mName_something_0", ... etc. So sort them
370  std::sort(matchingMoms.begin(), matchingMoms.end() );
371  val.reserve( matchingMoms.size());
372 
373  for( size_t i=0; i< matchingMoms.size(); i++){
374  std::stringstream s;s<< prefix<< i;
375  if( s.str() == matchingMoms[i]) val.push_back( j->getMoment(matchingMoms[i] ));
376  else break; //
377  }
378  return val;
379  }
380 
381 
382  static void set(const Jet* j, const std::string & mName, const std::vector<T> & v){
383  for(size_t i=0;i<v.size();i++){
384  std::stringstream s;
385  s << mName << "_"<< i;
386  j->setMoment(s.str(),v[i]);
387  }
388  }
389 };
390 
391 template<typename T>
392 T Jet::get(const std::string & mName) const { return JetAnalysisEDMHelper<T>::get(this,mName); }
393 
394 template<typename T>
395 void Jet::set(const std::string & mName, const T& v) const { return JetAnalysisEDMHelper<T>::set(this,mName,v); }