ATLAS Offline Software
Loading...
Searching...
No Matches
PUSplitPufitFex.cxx
Go to the documentation of this file.
1/*
2 * Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3 */
4/******************************************************************************
5 * @package Trigger/TrigAlgorithms/TrigEFMissingET
6 * @file PUSplitPufitFex.cxx
7 *
8 * Implementation of pileup-split pufit fex class
9 * @author Jon Burr
10 *****************************************************************************/
11
12#include "PUSplitPufitFex.h"
18
19namespace HLT
20{
21 namespace MET
22 {
24 {
26 }
27
28 PUSplitPufitFex::PUSplitPufitFex(const std::string &name, ISvcLocator *pSvcLocator)
29 : FexBase(name, pSvcLocator)
30 {
31 }
32
34 {
35 CHECK(m_inputKey.initialize());
36
37 // Update the decor keys if necessary
38 if (m_inputCategoryKey.key().find(".") == std::string::npos)
40 else if (SG::contKeyFromKey(m_inputCategoryKey.key()) != m_inputKey.key())
41 {
42 ATH_MSG_ERROR("Input category key does not match the input key!");
43 return StatusCode::FAILURE;
44 }
45 CHECK(m_inputCategoryKey.initialize());
46 return initializeBase({"NeutralForward", "ChargedHS", "ChargedPU", "UncorrSelNF"});
47 }
48
51 const EventContext &context,
52 MonGroupBuilder &) const
53 {
54 // Retrieve the input
55 auto input = SG::makeHandle(m_inputKey, context);
56 if (!input.isValid())
57 {
58 ATH_MSG_ERROR("Failed to retrieve " << m_inputKey);
59 return StatusCode::FAILURE;
60 }
61 auto categoryAcc = SG::makeHandle<int>(m_inputCategoryKey, context);
62
63 // Create the gridset
65
66 // Fill the grids from the input objects
67 for (const xAOD::IParticle *ipart : *input)
68 {
71 ipart->e(), ipart->eta(), ipart->phi());
72 switch (type)
73 {
75 gridset.get<PUClassification::NeutralForward>() += kin;
76 break;
78 gridset.get<PUClassification::ChargedHS>() += kin;
79 break;
81 gridset.get<PUClassification::ChargedPU>() += kin;
82 break;
83 default:
84 ATH_MSG_ERROR("Invalid PU category: " << type);
85 return StatusCode::FAILURE;
86 }
87 }
88
89 // Calculate the mean and variance from the 'nominal' grid
90 double mean;
91 double variance;
94 // Calculate the threshold
95 double threshold = mean + m_nSigma * sqrt(variance);
96
97 // Mask towers above the threshold
98 std::size_t count = 0;
99 for (PUSplitGrid &grid : gridset.grids)
100 for (PUSplitGrid::Tower &tower : grid)
101 if (tower.sumEt(m_neutralThresholdMode) > threshold)
102 {
103 tower.mask(true);
104 ++count;
105 }
106 if (msgLvl(MSG::DEBUG))
107 {
108 ATH_MSG_DEBUG("NeutralForward: " << gridset[NoDisplacement].get<PUClassification::NeutralForward>().sum());
109 ATH_MSG_DEBUG("ChargedHS: " << gridset[NoDisplacement].get<PUClassification::ChargedHS>().sum());
110 ATH_MSG_DEBUG("ChargedPU: " << gridset[NoDisplacement].get<PUClassification::ChargedPU>().sum());
111 ATH_MSG_DEBUG("Mean, variance, threshold = " << mean << ", " << variance << ", " << threshold);
112 ATH_MSG_DEBUG(count << "/" << gridset[NoDisplacement].get<1>().nTowers() << " towers above threshold");
113 }
114
115
116 // Select the right grid
118 gridset, m_neutralThresholdMode);
119 const PUSplitGrid &grid = gridset[displacement];
120
121 // Build the background sum, the masked tower kinematics (used for their
122 // directions), the list of the expected pileup values in each masked tower
123 // and the variances on each of those expected values
125 std::vector<SignedKinematics> masked;
126 masked.reserve(count);
127 std::vector<double> means;
128 means.reserve(count);
129 std::vector<double> variances(count, variance);
130
131 for (const PUSplitGrid::Tower &tower : grid)
132 {
133 const SignedKinematics &cPUKin = tower.get<PUClassification::ChargedPU>();
134 if (!tower.masked())
135 {
136 // If the tower is masked, then add its neutral component to the background sum
137 const SignedKinematics &kin = tower.get<PUClassification::NeutralForward>();
138 pileupSum.add(kin, getSigma(kin));
139 }
140 else
141 {
142 masked.push_back(tower.kinematics(PUClassification::NFcHS));
143 // Add the expected pileup contribution. Optionally, exclude the cPU
144 // component from this as this is already accounted
146 means.push_back(mean - cPUKin.pt());
147 else
148 means.push_back(mean);
149 }
150 // cPU elements always count as background
151 // (There's a possible TODO here - maybe this should only be done when subtractCPUFromMean is true?)
152 pileupSum.add(cPUKin, getSigma(cPUKin));
153 }
154
155 if (msgLvl(MSG::VERBOSE))
156 {
157 ATH_MSG_VERBOSE("Pileup sum = " << pileupSum.sum);
158 ATH_MSG_VERBOSE("Pileup covariance = " << pileupSum.covariance);
159 ATH_MSG_VERBOSE("Mean background energy = " << mean);
160 ATH_MSG_VERBOSE("Background energy variance = " << variance);
161 ATH_MSG_VERBOSE("Constraint importance = " << m_constraintImportance);
162 ATH_MSG_VERBOSE("Masked tower directions (sin, cos): ");
163 for (const SignedKinematics &kin : masked)
164 ATH_MSG_VERBOSE(" (" << kin.sinPhi() << ", " << kin.cosPhi() << ")");
165 }
166 // Fill components
168 grid.get<PUClassification::ChargedHS>().sum(PufitGrid::SumStrategy::All).fillMETComponent(1, met);
169 grid.get<PUClassification::ChargedPU>().sum(PufitGrid::SumStrategy::All).fillMETComponent(2, met);
171
172 // Now build the final sum
173 METComponent sum;
174 // Add the NF components of all masked towers
176 // Add the cHS components of all towers
178
179 if (variance != 0)
180 {
181 ATH_MSG_DEBUG("Calculate corrections");
182 std::vector<SignedKinematics> corrections = PufitUtils::pufit(
183 pileupSum.sum,
184 pileupSum.covariance,
185 means,
186 variances,
187 masked,
189
190 ATH_MSG_DEBUG("Before corrections: " << sum);
191 // Add the corrections
192 for (const SignedKinematics &kin : corrections)
193 {
194 ATH_MSG_DEBUG("Correction: (px, py, pz, et) = (" << kin.px() << ", " << kin.py() << ", " << kin.pz() << ", " << kin.et() << ")");
195 sum += kin;
196 }
197 }
198 else
199 ATH_MSG_DEBUG("Tower variance is 0 => there were no towers in the trimmed mean calculation with energy > 0. Skip the corrections");
200 ATH_MSG_DEBUG("Final MET: " << sum);
201 sum.fillMET(met);
202
203 return StatusCode::SUCCESS;
204 }
205 } // namespace MET
206} // namespace HLT
#define ATH_MSG_ERROR(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_DEBUG(x)
#define CHECK(...)
Evaluate an expression and check for errors.
Some common helper functions used by decoration handles.
Handle class for reading a decoration on an object.
bool msgLvl(const MSG::Level lvl) const
FexBase(const std::string &name, ISvcLocator *pSvcLocator)
Constructor.
Definition FexBase.cxx:43
StatusCode initializeBase(const std::vector< std::string > &componentNames)
Initialize the base class.
Definition FexBase.cxx:48
Helper struct to build up MET values before moving them into the EDM.
virtual StatusCode initialize() override
Initialize the fex.
Gaudi::Property< std::size_t > m_nEtaBins
The number of bins in eta.
double getSigma(const SignedKinematics &kin) const
Calculate the estimate on the variance of a tower.
Gaudi::Property< float > m_maxEta
The eta range of the grid.
Gaudi::Property< float > m_caloStoch
The coefficient of the stochastic term in the calo resolution estimate.
SG::ReadDecorHandleKey< xAOD::IParticleContainer > m_inputCategoryKey
Gaudi::Property< std::size_t > m_neutralThresholdMode
The neutral threshold mode.
PUSplitPufitFex(const std::string &name, ISvcLocator *pSvcLocator)
Constructor.
Gaudi::Property< bool > m_subtractCPUFromMean
Whether to remove the cPU component from the tower expectations.
Gaudi::Property< float > m_trimFraction
The trimming fraction.
Gaudi::Property< float > m_nSigma
The sigma threshold.
Gaudi::Property< std::size_t > m_nPhiBins
Gaudi::Property< float > m_caloNoise
The coefficient of the noise term in the calo resolution estimate.
Gaudi::Property< float > m_constraintImportance
The relative constraint importance.
virtual StatusCode fillMET(xAOD::TrigMissingET &met, const EventContext &context, MonGroupBuilder &monitors) const override
Calculate and fill the output MET value.
SG::ReadHandleKey< xAOD::IParticleContainer > m_inputKey
Input objects.
Base class for towers belonging to the grids.
PufitGrid & get()
Get one of the underlying grids.
Class to describe the kinematics of an object that can have negative energies.
double absPt() const
unsigned pt
static SignedKinematics fromEnergyEtaPhi(double energy, double eta, double phi)
Factory function to construct from energy, eta, phi (massless)
double pt() const
(signed) pt
Class providing the definition of the 4-vector interface.
void mean(std::vector< double > &bins, std::vector< double > &values, const std::vector< std::string > &files, const std::string &histname, const std::string &tplotname, const std::string &label="")
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
Definition hcg.cxx:130
int count(std::string s, const std::string &regx)
count how many occurances of a regx are in a string
Definition hcg.cxx:146
constexpr PUClassification NFcHS
void trimmedMeanAndVariance(const std::vector< double > &sorted, double trimFraction, double &mean, double &variance)
Calculate the trimmed mean and variance for a vector of tower sumEts.
Eigen::VectorXd pufit(const Eigen::Vector2d &pileupSum, const Eigen::Matrix2d &pileupCovariance, const Eigen::VectorXd &towerExpectations, const Eigen::VectorXd &towerVariances, const Eigen::VectorXd &correctionDirections, double constraintImportance)
Perform the pile-up fit.
GridDisplacement selectGrid(const PufitGridSet &grids)
Select the grid with the highest masked sumEt.
PufitMultiGridSet< PUSplitGrid > PUSplitGridSet
Definition PUSplitGrid.h:39
GridDisplacement
Enum to describe the positioning of the grid.
@ NoDisplacement
The grid is not shifted.
It used to be useful piece of code for replacing actual SG with other store of similar functionality ...
std::string contKeyFromKey(const std::string &key)
Extract the container part of key.
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
TrigMissingET_v1 TrigMissingET
Define the most recent version of the TrigMissingET class.
std::array< Grid, 4 > grids
Helper struct to hold the sum over pileup objects and its covariance.
Definition PufitUtils.h:29
Eigen::Matrix2d covariance
The covariance matrix.
Definition PufitUtils.h:43
CovarianceSum & add(const SignedKinematics &kin, double sigma)
Add a new contribution to the sum.
Eigen::Vector2d sum
The sum.
Definition PufitUtils.h:41