ATLAS Offline Software
JetFinder.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // JetFinder.cxx
6 
7 #include "JetRec/JetFinder.h"
8 #include "fastjet/PseudoJet.hh"
9 #include "fastjet/ClusterSequence.hh"
10 #include "fastjet/ClusterSequenceArea.hh"
11 #include "fastjet/config.h"
12 #include "fastjet/AreaDefinition.hh"
13 #ifndef NO_JET_VARIABLER
14 #include "fastjet/contrib/VariableRPlugin.hh"
15 #endif
19 #include "JetEDM/FastJetUtils.h"
20 #include "JetEDM/PseudoJetVector.h"
21 #include "JetEDM/ClusterSequence.h"
22 
23 #include "JetEDM/FastJetLink.h" // templated Jet_v1::setPseudoJet
24 #include "xAODJet/Jet_PseudoJet.icc" // templated Jet_v1::setPseudoJet
25 
26 #include <algorithm>
27 
28 using std::string;
29 using std::setw;
30 #ifndef NO_JET_VARIABLER
31 using fastjet::contrib::VariableRPlugin;
32 #endif
33 using xAOD::JetContainer;
34 
35 //**********************************************************************
36 
37 JetFinder::JetFinder(const std::string& name)
38 : AsgTool(name),
39  m_bld("JetFromPseudojet",this),
41  m_isVariableR(false) {
42  declareProperty("JetAlgorithm", m_jetalg="AntiKt");
43  declareProperty("JetRadius", m_jetrad =0.4);
44  declareProperty("VariableRMinRadius", m_minrad =-1.0);
45  declareProperty("VariableRMassScale", m_massscale =-1.0);
46  declareProperty("PtMin", m_ptmin =0.0);
47  declareProperty("GhostArea", m_ghostarea =0.0);
48  declareProperty("RandomOption", m_ranopt =1);
49  declareProperty("JetBuilder", m_bld);
50 }
51 
52 //**********************************************************************
53 
55  ATH_MSG_DEBUG("Initializing...");
59  ATH_MSG_ERROR("Invalid jet algorithm name: " << m_jetalg);
60  ATH_MSG_ERROR("Allowed values are Kt, CamKt, AntiKt, etc.");
61  return StatusCode::FAILURE;
62  }
63  if ( m_jetrad <=0 ) {
64  ATH_MSG_ERROR("Invalid jet size parameter: " << m_jetrad);
65  return StatusCode::FAILURE;
66  }
67 
68  fastjet::JetDefinition jetdef(m_fjalg, m_jetrad);
70  fastjet::ClusterSequence cs(empty, jetdef);
71  cs.inclusive_jets(m_ptmin);
72  m_isVariableR = m_minrad>=0.0 && m_massscale>=0.0;
73 #ifdef NO_JET_VARIABLER
74  if ( isVariableR() ) {
75  ATH_MSG_ERROR("Variable-R jet findng is not supported in theis build.");
76  }
77 #endif
78 
79  // Input DataHandles
81 
82  std::string sdrop = "ToolSvc.";
83  std::string myname = name();
84  std::string::size_type ipos = myname.find(sdrop);
85  if ( ipos != std::string::npos ) myname.replace(ipos, sdrop.size(), "");
86  std::string cname = "ClusterSequence_JetFinder_" + myname;
87 
88  m_cnameRKey = cname;
89  m_cnameWKey = cname;
92 
93  return StatusCode::SUCCESS;
94 }
95 
96 //**********************************************************************
97 
98 int JetFinder::find(const PseudoJetContainer& pjContainer,
99  xAOD::JetContainer & finalJets,
100  xAOD::JetInput::Type inputtype ) const {
101 
102  constexpr bool doSave = true;
103  fastjet::ClusterSequence* pcs{nullptr};
104  return _find(pjContainer, finalJets, inputtype, doSave, pcs);
105 }
106 
107 //**********************************************************************
108 
110  xAOD::JetContainer & finalJets,
111  xAOD::JetInput::Type inputtype,
112  fastjet::ClusterSequence*& pcs) const {
113 
114  constexpr bool doSave = false;
115  return _find(pjContainer, finalJets, inputtype, doSave, pcs);
116 }
117 
118 
119 int JetFinder::_find(const PseudoJetContainer& pjContainer,
121  xAOD::JetInput::Type inputtype,
122  bool doSave,
123  fastjet::ClusterSequence*& pcs) const {
125  ATH_MSG_ERROR("Jet finder is not properly initiialized.");
126  return 1;
127  }
128 
129  const PseudoJetVector* inps = pjContainer.casVectorPseudoJet();
130 
131  ATH_MSG_DEBUG("Jet finding input count: " << inps->size());
132  fastjet::JetDefinition jetdef(m_fjalg, m_jetrad);
133 #ifndef NO_JET_VARIABLER
134  const VariableRPlugin* pvrp = nullptr;
135  if ( isVariableR() ) {
136  VariableRPlugin::ClusterType vct = VariableRPlugin::AKTLIKE;
137  switch(m_fjalg) {
138  case fastjet::kt_algorithm: vct = VariableRPlugin::KTLIKE; break;
139  case fastjet::antikt_algorithm: vct = VariableRPlugin::AKTLIKE; break;
140  case fastjet::cambridge_algorithm: vct = VariableRPlugin::CALIKE; break;
141  default:
142  ATH_MSG_ERROR("Invalid algorithm type for variable-R finding.");
143  }
144  pvrp = new VariableRPlugin(m_massscale, m_minrad, m_jetrad, vct, false);
145  jetdef = fastjet::JetDefinition(pvrp);
146  jetdef.delete_plugin_when_unused ();
147  }
148 #else
149  if ( isVariableR() ) {
150  ATH_MSG_ERROR("Variable-R jet findng is not supported in theis build.");
151  }
152 #endif
153  if ( m_ghostarea <= 0 ) {
154  ATH_MSG_DEBUG("Creating input cluster sequence");
155  pcs = new fastjet::ClusterSequence(*inps, jetdef);
156  } else {
157  fastjet::GhostedAreaSpec gspec(5.0, 1, m_ghostarea);
158  std::vector<int> inseeds;
159  if ( m_ranopt == 1 ) {
160  // Use run/event number as random number seeds.
161 
162  auto handle = SG::makeHandle(m_eventinfokey);
163  if (!handle.isValid()){
164  ATH_MSG_ERROR("Unable to retrieve event info");
165  return 1;
166  }
167  const xAOD::EventInfo* pevinfo = handle.cptr();
168 
169 
170  if ( pevinfo != nullptr ) {
171  auto ievt = pevinfo->eventNumber();
172  auto irun = pevinfo->runNumber();
173  if ( pevinfo->eventType(xAOD::EventInfo::IS_SIMULATION)) {
174  // For MC, use the channel and MC event number
175  ievt = pevinfo->mcEventNumber();
176  irun = pevinfo->mcChannelNumber();
177  }
178  inseeds.push_back(ievt);
179  inseeds.push_back(irun);
180  } else {
181  ATH_MSG_ERROR("Unable to retrieve event info");
182  }
183  } // if (m_ranopt==1)
184  ATH_MSG_DEBUG("Active area specs:");
185  ATH_MSG_DEBUG(" Requested ghost area: " << m_ghostarea);
186  ATH_MSG_DEBUG(" Actual ghost area: " << gspec.actual_ghost_area());
187  ATH_MSG_DEBUG(" Max eta: " << gspec.ghost_etamax());
188  ATH_MSG_DEBUG(" # ghosts: " << gspec.n_ghosts());
189  ATH_MSG_DEBUG(" # rapidity bins: " << gspec.nrap());
190  ATH_MSG_DEBUG(" # phi bins: " << gspec.nphi());
191 
192  if ( inseeds.size() == 2 ) {
193  ATH_MSG_DEBUG(" Random seeds: " << inseeds[0] << ", "
194  << inseeds[1]);
195  } else {
196  ATH_MSG_WARNING("Random generator size is not 2: " << inseeds.size());
197  ATH_MSG_DEBUG(" Random seeds: ");
198  for ( auto seed : inseeds ) ATH_MSG_DEBUG(" " << seed);
199  }
200  fastjet::AreaDefinition adef(fastjet::active_area_explicit_ghosts, gspec);
201  //fastjet::AreaDefinition adef(fastjet::active_area, gspec);
202  ATH_MSG_DEBUG("Creating input area cluster sequence");
203  pcs = new fastjet::ClusterSequenceArea(*inps, jetdef,
204  adef.with_fixed_seed(inseeds));
205  }
206 
207  ATH_MSG_DEBUG("Calling fastjet");
208  PseudoJetVector outs = sorted_by_pt(pcs->inclusive_jets(m_ptmin));
209  ATH_MSG_DEBUG("Found jet count: " << outs.size());
210  // for ( PseudoJetVector::const_iterator ijet=outs.begin(); ijet!=outs.end(); ++ijet ) {
211  for (const auto & pj: outs ) {
212  xAOD::Jet* pjet = m_bld->add(pj, pjContainer, jets, inputtype);
213 
214  // transfer the contituents of pseudojet (which are pseudojets)
215  // to constiuents of jet (which are IParticles)
216  // pjContainer.extractConstituents(*pjet, pj);
217 
219  pjet->setAlgorithmType(ialg);
220  pjet->setSizeParameter(m_jetrad);
221  if ( isVariableR() ) {
222  pjet->setAttribute("VariableRMinRadius", m_minrad);
223  pjet->setAttribute("VariableRMassScale", m_massscale);
224  }
225  pjet->setAttribute("JetGhostArea", m_ghostarea);
226  }
227  ATH_MSG_DEBUG("Reconstructed jet count: " << jets.size() << " clusterseq="<<pcs);
228 
229  for(const xAOD::Jet* j : jets){
230  ATH_MSG_DEBUG("jets reconstructed no of constituents "
231  << j->numConstituents());
232  }
233  // offline stores pcs storegate, trigger: passes it back to caller
234  if(doSave){save(pcs);}
235 
236  return 0;
237 }
238 
239 
240 //**********************************************************************
241 
243 
244  auto handle_out = SG::makeHandle(m_cnameWKey);
245  if ( ! handle_out.record( std::unique_ptr<fastjet::ClusterSequence>(pcs)) ) {
246  ATH_MSG_WARNING("Unable to record " << m_cnameWKey.key());
247  } else {
248  ATH_MSG_DEBUG("Recorded " << m_cnameWKey.key() << " " << pcs );
249  }
250  auto handle_in = SG::makeHandle(m_cnameRKey);
251  bool present = false;
252  if ( handle_in.isValid()) {
253  present = true;
254  }
255  ATH_MSG_DEBUG("Check presence: " << present);
256  ATH_MSG_DEBUG("Will delete self: " << pcs->will_delete_self_when_unused());
257  const fastjet::SharedPtr<fastjet::PseudoJetStructureBase>& shrptr = pcs->structure_shared_ptr();
258  ATH_MSG_DEBUG(" Pointer: " << shrptr.get());
259  ATH_MSG_DEBUG(" Use count: " << shrptr.use_count());
260 }
261 
262 //**********************************************************************
263 
265  return m_isVariableR;
266 }
267 
268 //**********************************************************************
269 
270 void JetFinder::print() const {
271  ATH_MSG_INFO(" Jet algorithm: " << m_jetalg);
272  if ( isVariableR() ) {
273  ATH_MSG_INFO(" Variable-R: true");
274  ATH_MSG_INFO(" min radius: " << m_minrad);
275  ATH_MSG_INFO(" max radius: " << m_jetrad);
276  ATH_MSG_INFO(" mass scale: " << m_massscale);
277  } else {
278  ATH_MSG_INFO(" Jet size parameter: " << m_jetrad);
279  }
280  ATH_MSG_INFO(" Jet min pT [MeV]: " << m_ptmin);
281  ATH_MSG_INFO(" Ghost area: " << m_ghostarea);
282  ATH_MSG_INFO(" Output level: " << MSG::name(msg().level()));
283 }
284 
285 //**********************************************************************
JetFinder.h
WriteHandle.h
Handle class for recording to StoreGate.
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
xAOD::EventInfo_v1::eventNumber
uint64_t eventNumber() const
The current event's event number.
fastjet
Definition: FastJetLinkBase.h:22
xAOD::JetAlgorithmType::kt_algorithm
@ kt_algorithm
Definition: JetContainerInfo.h:31
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
JetFinder::JetFinder
JetFinder(const std::string &name)
Definition: JetFinder.cxx:37
PseudoJetVector.h
JetFinder::m_fjalg
fastjet::JetAlgorithm m_fjalg
Definition: JetFinder.h:95
ClusterSequence.h
JetFinder::m_cnameRKey
SG::ReadHandleKey< fastjet::ClusterSequence > m_cnameRKey
Definition: JetFinder.h:80
PseudoJetContainer
Definition: PseudoJetContainer.h:48
SG::VarHandleKey::key
const std::string & key() const
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:141
xAOD::EventInfo_v1::IS_SIMULATION
@ IS_SIMULATION
true: simulation, false: data
Definition: EventInfo_v1.h:151
empty
bool empty(TH1 *h)
Definition: computils.cxx:294
xAOD::EventInfo_v1::runNumber
uint32_t runNumber() const
The current event's run number.
JetFinder::isVariableR
bool isVariableR() const
Definition: JetFinder.cxx:264
python.iconfTool.models.loaders.level
level
Definition: loaders.py:20
xAOD::EventInfo_v1::mcChannelNumber
uint32_t mcChannelNumber() const
The MC generator's channel number.
SG::makeHandle
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
Definition: ReadCondHandle.h:270
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
JetFinder::find
virtual int find(const PseudoJetContainer &cont, xAOD::JetContainer &finalJets, xAOD::JetInput::Type contype) const override
Method to find jets from a vector of pseudojet inputs.
Definition: JetFinder.cxx:98
xAOD::JetAlgorithmType::fastJetDef
fastjet::JetAlgorithm fastJetDef(ID id)
Definition: FastJetUtils.cxx:20
jet::ClusterSequence
fastjet::ClusterSequence ClusterSequence
Definition: ClusterSequence.h:21
JetFinder::m_jetrad
float m_jetrad
Definition: JetFinder.h:85
xAOD::Jet_v1::setSizeParameter
void setSizeParameter(float p)
Definition: Jet_v1.cxx:257
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
JetFinder::m_jetalg
std::string m_jetalg
Definition: JetFinder.h:84
JetFinder::m_minrad
float m_minrad
Definition: JetFinder.h:86
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
MSG::name
const std::string & name(Level lvl)
Convenience function for translating message levels to strings.
Definition: MsgLevel.cxx:19
JetFinder::m_ghostarea
float m_ghostarea
Definition: JetFinder.h:89
SG::VarHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:103
xAOD::JetAlgorithmType::ID
ID
//////////////////////////////////////// JetAlgorithmType::ID defines most common physics jet finding...
Definition: JetContainerInfo.h:29
xAOD::JetAlgorithmType::undefined_jet_algorithm
@ undefined_jet_algorithm
Definition: JetContainerInfo.h:40
xAOD::JetInput::Type
Type
Definition: JetContainerInfo.h:54
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
xAOD::Jet_v1::setAttribute
void setAttribute(const std::string &name, const T &v)
JetFinder::m_eventinfokey
SG::ReadHandleKey< xAOD::EventInfo > m_eventinfokey
Definition: JetFinder.h:79
JetFinder::initialize
virtual StatusCode initialize() override
Dummy implementation of the initialisation function.
Definition: JetFinder.cxx:54
JetFinder::m_massscale
float m_massscale
Definition: JetFinder.h:87
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
ReadHandle.h
Handle class for reading from StoreGate.
xAOD::JetAlgorithmType::cambridge_algorithm
@ cambridge_algorithm
Definition: JetContainerInfo.h:32
JetFinder::m_ptmin
float m_ptmin
Definition: JetFinder.h:88
EventInfo.h
xAOD::JetAlgorithmType::antikt_algorithm
@ antikt_algorithm
Definition: JetContainerInfo.h:33
JetFinder::m_ranopt
int m_ranopt
Definition: JetFinder.h:90
xAOD::EventInfo_v1
Class describing the basic event information.
Definition: EventInfo_v1.h:43
xAOD::Jet_v1
Class describing a jet.
Definition: Jet_v1.h:57
PseudoJetVector
std::vector< fastjet::PseudoJet > PseudoJetVector
Definition: JetConstituentFiller.cxx:17
JetFinder::save
void save(fastjet::ClusterSequence *pcs) const
Definition: JetFinder.cxx:242
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
AthCommonMsg< AlgTool >::msg
MsgStream & msg() const
Definition: AthCommonMsg.h:24
JetFinder::_find
int _find(const PseudoJetContainer &cont, xAOD::JetContainer &finalJets, xAOD::JetInput::Type contype, bool doSave, fastjet::ClusterSequence *&) const
Definition: JetFinder.cxx:119
xAOD::JetAlgorithmType::algId
ID algId(const std::string &n)
Converts a string into a JetAlgorithmType::ID.
Definition: JetContainerInfo.cxx:76
JetFinder::findNoSave
virtual int findNoSave(const PseudoJetContainer &cont, xAOD::JetContainer &finalJets, xAOD::JetInput::Type contype, fastjet::ClusterSequence *&cs) const override
Definition: JetFinder.cxx:109
PseudoJetContainer::casVectorPseudoJet
const std::vector< PseudoJet > * casVectorPseudoJet() const
Definition: PseudoJetContainer.cxx:135
defineDB.jets
list jets
Definition: JetTagCalibration/share/defineDB.py:24
xAOD::JetContainer
JetContainer_v1 JetContainer
Definition of the current "jet container version".
Definition: JetContainer.h:17
FastJetUtils.h
Jet_PseudoJet.icc
python.CreateTierZeroArgdict.pcs
pcs
Definition: CreateTierZeroArgdict.py:200
JetFinder::print
virtual void print() const override
Print the state of the tool.
Definition: JetFinder.cxx:270
JetFinder::m_cnameWKey
SG::WriteHandleKey< fastjet::ClusterSequence > m_cnameWKey
Definition: JetFinder.h:81
xAOD::Jet_v1::setAlgorithmType
void setAlgorithmType(JetAlgorithmType::ID a)
Definition: Jet_v1.cxx:258
JetFinder::m_isVariableR
bool m_isVariableR
Definition: JetFinder.h:96
JetFinder::m_bld
ToolHandle< IJetFromPseudojet > m_bld
Definition: JetFinder.h:92
xAOD::EventInfo_v1::mcEventNumber
uint64_t mcEventNumber() const
The MC generator's event number.
xAOD::EventInfo_v1::eventType
bool eventType(EventType type) const
Check for one particular bitmask value.