ATLAS Offline Software
Loading...
Searching...
No Matches
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"
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
28using std::string;
29using std::setw;
30#ifndef NO_JET_VARIABLER
31using fastjet::contrib::VariableRPlugin;
32#endif
34
35//**********************************************************************
36
37JetFinder::JetFinder(const std::string& name)
38: AsgTool(name),
39 m_bld("JetFromPseudojet",this),
40 m_fjalg(fastjet::undefined_jet_algorithm),
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...");
58 if ( m_fjalg == fastjet::undefined_jet_algorithm ) {
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
80 ATH_CHECK( m_eventinfokey.initialize() );
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;
90 ATH_CHECK( m_cnameRKey.initialize() );
91 ATH_CHECK( m_cnameWKey.initialize() );
92
93 return StatusCode::SUCCESS;
94}
95
96//**********************************************************************
97
98int 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
120 xAOD::JetContainer& jets,
121 xAOD::JetInput::Type inputtype,
122 bool doSave,
123 fastjet::ClusterSequence*& pcs) const {
124 if ( m_fjalg == fastjet::undefined_jet_algorithm ) {
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();
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);
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
242void JetFinder::save(fastjet::ClusterSequence* pcs) const {
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
270void 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//**********************************************************************
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
Handle class for reading from StoreGate.
Handle class for recording to StoreGate.
std::vector< fastjet::PseudoJet > PseudoJetVector
static const Attributes_t empty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
std::string m_jetalg
Definition JetFinder.h:84
float m_massscale
Definition JetFinder.h:87
int m_ranopt
Definition JetFinder.h:90
virtual int findNoSave(const PseudoJetContainer &cont, xAOD::JetContainer &finalJets, xAOD::JetInput::Type contype, fastjet::ClusterSequence *&cs) const override
virtual void print() const override
Print the state of the tool.
int _find(const PseudoJetContainer &cont, xAOD::JetContainer &finalJets, xAOD::JetInput::Type contype, bool doSave, fastjet::ClusterSequence *&) const
SG::WriteHandleKey< fastjet::ClusterSequence > m_cnameWKey
Definition JetFinder.h:81
float m_ptmin
Definition JetFinder.h:88
float m_ghostarea
Definition JetFinder.h:89
float m_jetrad
Definition JetFinder.h:85
bool m_isVariableR
Definition JetFinder.h:96
bool isVariableR() const
JetFinder(const std::string &name)
Definition JetFinder.cxx:37
void save(fastjet::ClusterSequence *pcs) const
SG::ReadHandleKey< fastjet::ClusterSequence > m_cnameRKey
Definition JetFinder.h:80
float m_minrad
Definition JetFinder.h:86
virtual StatusCode initialize() override
Dummy implementation of the initialisation function.
Definition JetFinder.cxx:54
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
ToolHandle< IJetFromPseudojet > m_bld
Definition JetFinder.h:92
SG::ReadHandleKey< xAOD::EventInfo > m_eventinfokey
Definition JetFinder.h:79
fastjet::JetAlgorithm m_fjalg
Definition JetFinder.h:95
const std::vector< PseudoJet > * casVectorPseudoJet() const
AsgTool(const std::string &name)
Constructor specifying the tool instance's name.
Definition AsgTool.cxx:58
uint64_t mcEventNumber() const
The MC generator's event number.
bool eventType(EventType type) const
Check for one particular bitmask value.
@ IS_SIMULATION
true: simulation, false: data
uint32_t runNumber() const
The current event's run number.
uint32_t mcChannelNumber() const
The MC generator's channel number.
uint64_t eventNumber() const
The current event's event number.
void setAlgorithmType(JetAlgorithmType::ID a)
Definition Jet_v1.cxx:258
void setAttribute(const std::string &name, const T &v)
void setSizeParameter(float p)
Definition Jet_v1.cxx:257
const std::string & name(Level lvl)
Convenience function for translating message levels to strings.
Definition MsgLevel.cxx:19
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
ID algId(const std::string &n)
Converts a string into a JetAlgorithmType::ID.
fastjet::JetAlgorithm fastJetDef(ID id)
ID
//////////////////////////////////////// JetAlgorithmType::ID defines most common physics jet finding...
Jet_v1 Jet
Definition of the current "jet version".
EventInfo_v1 EventInfo
Definition of the latest event info version.
JetContainer_v1 JetContainer
Definition of the current "jet container version".
MsgStream & msg
Definition testRead.cxx:32