ATLAS Offline Software
Loading...
Searching...
No Matches
NnClusterizationFactory.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3*/
4
5 #ifndef SICLUSTERIZATIONTOOL_NnClusterizationFactory_C
6 #define SICLUSTERIZATIONTOOL_NnClusterizationFactory_C
7
8 /******************************************************
9 @class NnClusterizationFactory
10 @author Giacinto Piacquadio (PH-ADE-ID)
11 Package : SiClusterizationTool
12 Created : January 2011
13 DESCRIPTION: Load neural networks used for clustering
14 and deal with:
15 1) estimate of number of particles
16 2) estimate of position of cluster / subclusters
17 3) estimate of error in all cases
18 (full PDF or just RMS)
19 ********************************************************/
20
21
22#include "GaudiKernel/ToolHandle.h"
23#include "GaudiKernel/ServiceHandle.h"
25
27
28//this is a typedef: no forward decl possible
38
39#include <RtypesCore.h> //Double_t
40#include <Eigen/Dense>
41#include <vector>
42#include <array>
43#include <string>
44#include <string_view>
45#include <regex>
46
47
48class TTrainedNetwork;
49class TH1;
50
51namespace lwt {
52 class NanReplacer;
53 class LightweightGraph;
54}
55
56namespace Trk {
57 class NeuralNetworkToHistoTool;
58 class Surface;
59}
60
61namespace InDetDD{
62 class SiLocalPosition;
63}
64
65namespace InDet {
66
67 class PixelCluster;
68
69 struct NNinput{
70 operator bool() const {
71 return !matrixOfToT.empty();
72 }
73 int sizeX = 0;
74 int sizeY = 0;
75 std::vector<std::vector<float> > matrixOfToT;
76 std::vector<float> vectorOfPitchesY;
79 float phi = 0;
80 float theta = 0;
81 float etaModule = 0;
82 bool useTrackInfo = 0;
85 };
86
87 static const InterfaceID IID_NnClusterizationFactory("InDet::NnClusterizationFactory", 1, 0);
88
90
91 public:
92
94 static const InterfaceID& interfaceID() { return IID_NnClusterizationFactory; };
95
96 NnClusterizationFactory(const std::string& name,
97 const std::string& n, const IInterface* p);
99
100 virtual StatusCode initialize() override;
101 virtual StatusCode finalize() override { return StatusCode::SUCCESS; };
102
103 std::vector<double> estimateNumberOfParticles(const InDet::PixelCluster& pCluster,
104 Amg::Vector3D & beamSpotPosition) const;
105
106 std::vector<double> estimateNumberOfParticles(const InDet::PixelCluster& pCluster,
107 const Trk::Surface& pixelSurface,
108 const Trk::TrackParameters& trackParsAtSurface) const;
109
110 /* Public-facing method 1: no track parameters */
111 std::vector<Amg::Vector2D> estimatePositions(const InDet::PixelCluster& pCluster,
112 Amg::Vector3D & beamSpotPosition,
113 std::vector<Amg::MatrixX> & errors,
114 int numberSubClusters) const;
115
116 /* Public-facing method 1: with track parameters */
117 std::vector<Amg::Vector2D> estimatePositions(const InDet::PixelCluster& pCluster,
118 const Trk::Surface& pixelSurface,
119 const Trk::TrackParameters& trackParsAtSurface,
120 std::vector<Amg::MatrixX> & errors,
121 int numberSubClusters) const;
122
123 private:
124
125 // Handling lwtnn inputs
126 typedef std::vector<Eigen::VectorXd> InputVector;
127
128 /* Estimate number of particles for both with and w/o tracks */
129 /* Method 1: using older TTrainedNetworks */
130 std::vector<double> estimateNumberOfParticlesTTN(const TTrainedNetworkCollection &nn_collection,
131 const std::vector<double>& inputData) const;
132
133 /* Estimate number of particles for both with and w/o tracks */
134 /* Method 2: using lwtnn for more flexible interfacing with an ordered vector
135 * Vector order MUST match variable order. */
137
138 /* Estimate position for both with and w/o tracks */
139 /* Method 1: using older TTrainedNetworks */
140 std::vector<Amg::Vector2D> estimatePositionsTTN(
141 const TTrainedNetworkCollection &nn_collection,
142 const std::vector<double>& inputData,
143 const NNinput& input,
144 const InDet::PixelCluster& pCluster,
145 int numberSubClusters,
146 std::vector<Amg::MatrixX> & errors) const;
147
148 /* Estimate position for both with and w/o tracks */
149 /* Method 2: using lwtnn for more flexible interfacing with an ordered vector
150 * Vector order MUST match variable order. */
151 std::vector<Amg::Vector2D> estimatePositionsLWTNN(
153 NNinput& rawInput,
154 const InDet::PixelCluster& pCluster,
155 int numberSubClusters,
156 std::vector<Amg::MatrixX> & errors) const;
157
158 // For error formatting in lwtnn cases
159 static double correctedRMSX(double posPixels) ;
160
161 double correctedRMSY(double posPixels, std::vector<float>& pitches) const;
162
163 /* algorithmic component */
165 Amg::Vector3D & beamSpotPosition,
166 double & tanl) const;
167
168 void addTrackInfoToInput(NNinput& input,
169 const Trk::Surface& pixelSurface,
170 const Trk::TrackParameters& trackParsAtSurface,
171 const double tanl) const;
172
173
174 std::vector<double> assembleInputRunI(NNinput& input) const;
175
176
177 std::vector<double> assembleInputRunII(NNinput& input) const;
178
179 InputVector eigenInput(NNinput & input) const;
180
181 std::vector<Amg::Vector2D> getPositionsFromOutput(std::vector<double> & output,
182 const NNinput & input,
183 const InDet::PixelCluster& pCluster) const;
184
185
186 void getErrorMatrixFromOutput(std::vector<double>& outputX,
187 std::vector<double>& outputY,
188 std::vector<Amg::MatrixX>& errorMatrix,
189 int nParticles) const;
190
191
192 Gaudi::Property< std::vector<std::string> > m_nnOrder
193 {this, "NetworkOrder", {
194 "NumberParticles",
195 "ImpactPoints1P",
196 "ImpactPoints2P",
197 "ImpactPoints3P",
198 "ImpactPointErrorsX1",
199 "ImpactPointErrorsX2",
200 "ImpactPointErrorsX3",
201 "ImpactPointErrorsY1",
202 "ImpactPointErrorsY2",
203 "ImpactPointErrorsY3"},
204 "The order in which the networks will appear in the TTrainedNetworkCollection"};
205
211 static constexpr std::array<std::string_view, kNNetworkTypes> s_nnTypeNames{
212 "NumberParticlesNN",
213 "PositionNN",
214 "ErrorXNN",
215 "ErrorYNN" };
216 static constexpr std::array<unsigned int, kNNetworkTypes> m_nParticleGroup{0U,1U,1U,1U}; // unsigned int
217 static const std::array<std::regex, kNNetworkTypes> m_nnNames;
218
219 unsigned int m_nParticleNNId{};
220 std::vector< std::vector<unsigned int> > m_NNId{};
221
222
223 // Function to be called to assemble the inputs
225
226 //Calculate flat vector dimension, according to input
227 size_t calculateVectorDimension(const bool useTrackInfo) const;
228
229 // Function to be called to compute the output
230 using ReturnType = std::vector<Double_t>;
231 using InputType = std::vector<Double_t>;
232 //the following declares a member variable m_calculateOutput which is a function pointer
233 //to a member function of the TTrainedNetwork. Note to anyone brave enough to update this to C++17using std::function:
234 //TTrainedNetwork::calculateNormalized is overloaded so template resolution does not work trivially.
236
237 ToolHandle<ISiLorentzAngleTool> m_pixelLorentzAngleTool
238 {this, "PixelLorentzAngleTool", "SiLorentzAngleTool/PixelLorentzAngleTool", "Tool to retreive Lorentz angle of Pixel"};
239
241 {this, "PixelChargeCalibCondData", "PixelChargeCalibCondData", "Output key"};
242
244 {this, "NnCollectionReadKey", "PixelClusterNN", "The conditions store key for the pixel cluster NNs"};
245
247 {this, "NnCollectionWithTrackReadKey", "PixelClusterNNWithTrack",
248 "The conditions store key for the pixel cluster NNs which needs tracks as input"};
249
251 {this, "NnCollectionJSONReadKey", "PixelClusterNNJSON",
252 "The conditions key for the pixel cluster NNs configured via JSON file and accessed with lwtnn"};
253
254 // this is written into the JSON config "node_index"
255 // this can be found from the LWTNN GraphConfig object used to initalize the collection objects
256 // option size_t index = graph_config.outputs.at("output_node_name").node_index
257 //
258 Gaudi::Property< std::size_t > m_outputNodesPos1
259 {this, "OutputNodePos1", 7,
260 "Output node for the 1 position networks (LWTNN)"};
261
262 Gaudi::Property< std::vector<std::size_t> > m_outputNodesPos2
263 {this, "OutputNodePos2", { 10, 11 },
264 "List of output nodes for the 2 position network (LWTNN)"};
265
266 Gaudi::Property< std::vector<std::size_t> > m_outputNodesPos3
267 {this, "OutputNodePos3", { 13, 14, 15 },
268 "List of output nodes for the 3 position networks (LWTNN)"};
269
270 Gaudi::Property<unsigned int> m_maxSubClusters
271 {this, "MaxSubClusters", 3, "Maximum number of sub cluster supported by the networks." };
272
274 {this, "correctLorShiftBarrelWithoutTracks",0.,"Lorentz shift correction factor when evaluating NN without track input."};
275
276 Gaudi::Property<double> m_correctLorShiftBarrelWithTracks
277 {this, "correctLorShiftBarrelWithTracks",0.,"Lorentz shift correction factor when evaluating NN with track input."};
278
279 Gaudi::Property<bool> m_useToT
280 {this, "useToT",true,"Use Tot rather than charge." }; // @TODO toggle mode depending on whether a PxielCalibSvc is set ?
281
282 Gaudi::Property<bool> m_addIBL
283 {this, "addIBL", false, "Also apply to clusters in IBL." };
284
285 Gaudi::Property<bool> m_doRunI
286 {this, "doRunI", false, "Use runI style network (outputs are not normalised; add pitches; use charge if not m_useToT)"};
287
288 Gaudi::Property<bool> m_useTTrainedNetworks
289 {this, "useTTrainedNetworks", false, "Use earlier (release-21-like) neural networks stored in ROOT files and accessed via TTrainedNetowrk."};
290
291 Gaudi::Property<bool> m_useRecenteringNNWithouTracks
292 {this, "useRecenteringNNWithoutTracks",false,"Recenter x position when evaluating NN without track input."};
293
294 Gaudi::Property<bool> m_useRecenteringNNWithTracks
295 {this, "useRecenteringNNWithTracks",false,"Recenter x position when evaluating NN with track input."};
296
297 Gaudi::Property<unsigned int> m_sizeX
298 {this, "sizeX",7,"Size of pixel matrix along X"};
299
300 Gaudi::Property<unsigned int> m_sizeY
301 {this, "sizeY",7,"Size of pixel matrix along Y"};
302
303 };
304
305 }//end InDet namespace
306
307 #endif
defines and typedefs for IOVSvc
Store pixel constant parameters in PixelModuleData.
AthAlgTool(const std::string &type, const std::string &name, const IInterface *parent)
Constructor with parameters:
Class to represent a position in the natural frame of a silicon sensor, for Pixel and SCT For Pixel: ...
std::vector< double > assembleInputRunII(NNinput &input) const
void addTrackInfoToInput(NNinput &input, const Trk::Surface &pixelSurface, const Trk::TrackParameters &trackParsAtSurface, const double tanl) const
Gaudi::Property< unsigned int > m_maxSubClusters
SG::ReadCondHandleKey< PixelChargeCalibCondData > m_chargeDataKey
std::vector< double > estimateNumberOfParticlesLWTNN(NnClusterizationFactory::InputVector &input) const
virtual StatusCode finalize() override
Gaudi::Property< unsigned int > m_sizeX
double correctedRMSY(double posPixels, std::vector< float > &pitches) const
ReturnType(::TTrainedNetwork::* m_calculateOutput)(const InputType &input) const
NNinput createInput(const InDet::PixelCluster &pCluster, Amg::Vector3D &beamSpotPosition, double &tanl) const
virtual StatusCode initialize() override
Gaudi::Property< double > m_correctLorShiftBarrelWithoutTracks
static const InterfaceID & interfaceID()
AlgTool interface methods.
Gaudi::Property< std::size_t > m_outputNodesPos1
Gaudi::Property< std::vector< std::size_t > > m_outputNodesPos2
ToolHandle< ISiLorentzAngleTool > m_pixelLorentzAngleTool
Gaudi::Property< std::vector< std::size_t > > m_outputNodesPos3
std::vector< Amg::Vector2D > estimatePositionsLWTNN(NnClusterizationFactory::InputVector &input, NNinput &rawInput, const InDet::PixelCluster &pCluster, int numberSubClusters, std::vector< Amg::MatrixX > &errors) const
std::vector< double > assembleInputRunI(NNinput &input) const
SG::ReadCondHandleKey< LWTNNCollection > m_readKeyJSON
std::vector< Amg::Vector2D > estimatePositions(const InDet::PixelCluster &pCluster, Amg::Vector3D &beamSpotPosition, std::vector< Amg::MatrixX > &errors, int numberSubClusters) const
Gaudi::Property< std::vector< std::string > > m_nnOrder
Gaudi::Property< double > m_correctLorShiftBarrelWithTracks
Gaudi::Property< bool > m_useTTrainedNetworks
std::vector< double > estimateNumberOfParticlesTTN(const TTrainedNetworkCollection &nn_collection, const std::vector< double > &inputData) const
static constexpr std::array< unsigned int, kNNetworkTypes > m_nParticleGroup
SG::ReadCondHandleKey< TTrainedNetworkCollection > m_readKeyWithoutTrack
std::vector< double >(InDet::NnClusterizationFactory::* m_assembleInput)(NNinput &input) const
std::vector< Eigen::VectorXd > InputVector
NnClusterizationFactory(const std::string &name, const std::string &n, const IInterface *p)
std::vector< Amg::Vector2D > estimatePositionsTTN(const TTrainedNetworkCollection &nn_collection, const std::vector< double > &inputData, const NNinput &input, const InDet::PixelCluster &pCluster, int numberSubClusters, std::vector< Amg::MatrixX > &errors) const
static const std::array< std::regex, kNNetworkTypes > m_nnNames
Gaudi::Property< bool > m_useRecenteringNNWithouTracks
static constexpr std::array< std::string_view, kNNetworkTypes > s_nnTypeNames
InputVector eigenInput(NNinput &input) const
void getErrorMatrixFromOutput(std::vector< double > &outputX, std::vector< double > &outputY, std::vector< Amg::MatrixX > &errorMatrix, int nParticles) const
Gaudi::Property< bool > m_useRecenteringNNWithTracks
std::vector< std::vector< unsigned int > > m_NNId
std::vector< Amg::Vector2D > getPositionsFromOutput(std::vector< double > &output, const NNinput &input, const InDet::PixelCluster &pCluster) const
static double correctedRMSX(double posPixels)
size_t calculateVectorDimension(const bool useTrackInfo) const
Gaudi::Property< unsigned int > m_sizeY
SG::ReadCondHandleKey< TTrainedNetworkCollection > m_readKeyWithTrack
std::vector< double > estimateNumberOfParticles(const InDet::PixelCluster &pCluster, Amg::Vector3D &beamSpotPosition) const
Abstract Base Class for tracking surfaces.
Eigen::Matrix< double, 3, 1 > Vector3D
Message Stream Member.
Primary Vertex Finder.
static const InterfaceID IID_NnClusterizationFactory("InDet::NnClusterizationFactory", 1, 0)
Ensure that the ATLAS eigen extensions are properly loaded.
ParametersBase< TrackParametersDim, Charged > TrackParameters
std::vector< std::vector< float > > matrixOfToT
std::vector< float > vectorOfPitchesY