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