ATLAS Offline Software
GTowerRhoSubtractionAlg.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 #include "StoreGate/ReadHandle.h"
8 #include <memory>
10 #include "xAODCore/ShallowCopy.h"
12 #include "TH1F.h"
13 #include "GaudiKernel/SystemOfUnits.h"
14 #include "GTowerHelpers.h"
16 #include <algorithm>
17 
18 namespace
19 {
20  const static SG::AuxElement::ConstAccessor<float> accArea("area");
21  const static SG::AuxElement::Decorator<std::vector<float>> decRho("FPGARhos");
22 } // namespace
23 
24 namespace LVL1
25 {
26  GTowerRhoSubtractionAlg::GTowerRhoSubtractionAlg(const std::string &name, ISvcLocator *pSvcLocator)
27  : AthReentrantAlgorithm(name, pSvcLocator)
28  {
29  declareProperty("InputTowers", m_inputKey = "GCaloTower");
30  declareProperty("OutputTowers", m_outputKey = "GCaloTowerRhoSubtracted");
31  declareProperty("OutputRho", m_outputRhoKey = "GFEXRho");
32  declareProperty("UseNegativeTowers", m_useNegativeTowers = true);
33  declareProperty("MaxTowerEt", m_maxTowerEt = 10 * Gaudi::Units::GeV);
34  declareProperty("ForcePositiveRho", m_forcePosRho = false);
35  }
36 
38 
40  {
41  ATH_CHECK(m_inputKey.initialize());
42  ATH_CHECK(m_outputKey.initialize());
43  ATH_CHECK(m_outputRhoKey.initialize());
44  return StatusCode::SUCCESS;
45  }
46 
47  StatusCode GTowerRhoSubtractionAlg::execute(const EventContext& ctx) const
48  {
49  auto inputTowers = SG::makeHandle(m_inputKey, ctx);
50  if (!inputTowers.isValid())
51  {
52  ATH_MSG_ERROR("Failed to retrieve input towers " << m_inputKey.key());
53  return StatusCode::FAILURE;
54  }
55  auto shallowCopy = xAOD::shallowCopyContainer(*inputTowers);
56  std::unique_ptr<xAOD::JGTowerContainer> outputTowers(shallowCopy.first);
57  std::unique_ptr<xAOD::ShallowAuxContainer> outputTowersAux(shallowCopy.second);
58  xAOD::setOriginalObjectLink(*inputTowers, *outputTowers);
59 
60  // Split towers into FPGAs
61  constexpr static std::size_t nFPGAs = static_cast<std::size_t>(gFEX::FPGA::N_FPGAS);
62  std::array<std::vector<const xAOD::JGTower *>, nFPGAs> fpgas;
63  // Use histograms to calculate RMSs of tower Ets for each FPGA
64  std::array<TH1F, nFPGAs> fpgaHistograms;
65  std::vector<float> fpgaRhos(3, 0.0);
66  for (std::size_t i = 0; i < nFPGAs; ++i)
67  fpgaHistograms.at(i) = TH1F(("hFPGA" + std::to_string(i)).c_str(), "", 50, 0, 5000);
68  for (const xAOD::JGTower *tower : *inputTowers)
69  {
70  gFEX::FPGA fpga = gFEX::getFPGA(tower->eta());
71  if (fpga == gFEX::FPGA::N_FPGAS)
72  {
73  ATH_MSG_WARNING("Could not classify tower as an FPGA!");
74  continue;
75  }
76  fpgas.at(static_cast<std::size_t>(fpga)).push_back(tower);
77  fpgaHistograms.at(static_cast<std::size_t>(fpga)).Fill(tower->et());
78  }
79 
80  // Now perform the subtraction
81  for (std::size_t i = 0; i < nFPGAs; ++i)
82  {
83  float threshold = 3 * fpgaHistograms.at(i).GetRMS();
84  const std::vector<const xAOD::JGTower *> &towers = fpgas.at(i);
85  float rho = calculateRho(towers);
86  fpgaRhos.at(i) = rho;
87  for (const xAOD::JGTower *tower : towers)
88  {
89  float area = accArea(*tower);
90  float etSub = tower->et() - area * rho;
91  // output is a shallow copy of input so the indices are guaranteed to be parallel
92  outputTowers->at(tower->index())->setEt(etSub < threshold ? 0 : etSub);
93  }
94  }
95 
96  auto outputHandle = SG::makeHandle(m_outputKey, ctx);
97  ATH_CHECK(outputHandle.record(std::move(outputTowers), std::move(outputTowersAux)));
98 
99  // Create an EnergySumRoI container to store the rho values
100  auto rhoCont = std::make_unique<xAOD::EnergySumRoI>();
101  auto rhoContAux = std::make_unique<xAOD::EnergySumRoIAuxInfo>();
102  rhoCont->setStore(rhoContAux.get());
103  decRho(*rhoCont) = fpgaRhos;
104  auto outputRhoHandle = SG::makeHandle(m_outputRhoKey, ctx);
105  ATH_CHECK(outputRhoHandle.record(std::move(rhoCont), std::move(rhoContAux)));
106  return StatusCode::SUCCESS;
107  }
108 
109  float GTowerRhoSubtractionAlg::calculateRho(const std::vector<const xAOD::JGTower *> &towers) const
110  {
111  float totalEt = 0;
112  float totalArea = 0;
113  for (const xAOD::JGTower *tower : towers)
114  {
115  if ((m_useNegativeTowers || tower->et() > 0) && tower->et() <= m_maxTowerEt)
116  {
117  totalEt += tower->et();
118  totalArea += accArea(*tower);
119  }
120  }
121  float rho = (totalArea == 0 ? 0.0f : totalEt / totalArea);
122  if (m_forcePosRho && rho < 0)
123  rho = 0;
124  return rho;
125  }
126 
127 } // namespace LVL1
xAOD::JGTower_v1
Description of JGTower_v1.
Definition: JGTower_v1.h:46
ShallowCopy.h
LVL1::GTowerRhoSubtractionAlg::m_forcePosRho
bool m_forcePosRho
Definition: GTowerRhoSubtractionAlg.h:31
LVL1::gFEX::getFPGA
FPGA getFPGA(float eta)
Get the FPGA code from the tower eta.
Definition: GTowerHelpers.cxx:15
GeV
#define GeV
Definition: PhysicsAnalysis/TauID/TauAnalysisTools/Root/HelperFunctions.cxx:17
AthCommonDataStore< AthCommonMsg< Gaudi::Algorithm > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
LVL1::GTowerRhoSubtractionAlg::~GTowerRhoSubtractionAlg
virtual ~GTowerRhoSubtractionAlg() override
Definition: GTowerRhoSubtractionAlg.cxx:37
LVL1::GTowerRhoSubtractionAlg::m_useNegativeTowers
bool m_useNegativeTowers
Definition: GTowerRhoSubtractionAlg.h:29
EnergySumRoIAuxInfo.h
SG::ConstAccessor
Helper class to provide constant type-safe access to aux data.
Definition: ConstAccessor.h:55
GTowerHelpers.h
LVL1::GTowerRhoSubtractionAlg::m_outputKey
SG::WriteHandleKey< xAOD::JGTowerContainer > m_outputKey
Definition: GTowerRhoSubtractionAlg.h:27
ShallowAuxContainer.h
LVL1
eFexTowerBuilder creates xAOD::eFexTowerContainer from supercells (LATOME) and triggerTowers (TREX) i...
Definition: ICMMCPHitsCnvTool.h:18
LVL1::GTowerRhoSubtractionAlg::m_maxTowerEt
float m_maxTowerEt
Definition: GTowerRhoSubtractionAlg.h:30
LVL1::GTowerRhoSubtractionAlg::execute
virtual StatusCode execute(const EventContext &ctx) const override
Definition: GTowerRhoSubtractionAlg.cxx:47
LVL1::GTowerRhoSubtractionAlg::calculateRho
float calculateRho(const std::vector< const xAOD::JGTower * > &towers) const
Definition: GTowerRhoSubtractionAlg.cxx:109
AthReentrantAlgorithm
An algorithm that can be simultaneously executed in multiple threads.
Definition: AthReentrantAlgorithm.h:83
LVL1::GTowerRhoSubtractionAlg::m_inputKey
SG::ReadHandleKey< xAOD::JGTowerContainer > m_inputKey
Definition: GTowerRhoSubtractionAlg.h:26
SG::makeHandle
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
Definition: ReadCondHandle.h:270
WriteHandle.h
Handle class for recording to StoreGate.
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
SG::Decorator
Helper class to provide type-safe access to aux data.
Definition: Decorator.h:59
lumiFormat.i
int i
Definition: lumiFormat.py:85
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
TrigConf::name
Definition: HLTChainList.h:35
ActsTrk::to_string
std::string to_string(const DetectorType &type)
Definition: GeometryDefs.h:34
threshold
Definition: chainparser.cxx:74
GTowerRhoSubtractionAlg.h
LVL1::GTowerRhoSubtractionAlg::m_outputRhoKey
SG::WriteHandleKey< xAOD::EnergySumRoI > m_outputRhoKey
Definition: GTowerRhoSubtractionAlg.h:28
xAOD::shallowCopyContainer
std::pair< std::unique_ptr< T >, std::unique_ptr< ShallowAuxContainer > > shallowCopyContainer(const T &cont, [[maybe_unused]] const EventContext &ctx)
Function making a shallow copy of a constant container.
Definition: ShallowCopy.h:110
RunTileMonitoring.towers
towers
Definition: RunTileMonitoring.py:133
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
xAOD::setOriginalObjectLink
bool setOriginalObjectLink(const IParticle &original, IParticle &copy)
This function should be used by CP tools when they make a deep copy of an object in their correctedCo...
Definition: IParticleHelpers.cxx:30
LVL1::GTowerRhoSubtractionAlg::GTowerRhoSubtractionAlg
GTowerRhoSubtractionAlg(const std::string &name, ISvcLocator *pSvcLocator)
Definition: GTowerRhoSubtractionAlg.cxx:26
IParticleHelpers.h
LVL1::gFEX::FPGA
FPGA
Definition: GTowerHelpers.h:17
python.TrigEgammaMonitorHelper.TH1F
def TH1F(name, title, nxbins, bins_par2, bins_par3=None, path='', **kwargs)
Definition: TrigEgammaMonitorHelper.py:24
DataVector::at
const T * at(size_type n) const
Access an element, as an rvalue.
area
double area(double R)
Definition: ConvertStaveServices.cxx:42
ReadHandle.h
Handle class for reading from StoreGate.
LVL1::gFEX::FPGA::N_FPGAS
@ N_FPGAS
fitman.rho
rho
Definition: fitman.py:532
LVL1::GTowerRhoSubtractionAlg::initialize
virtual StatusCode initialize() override
Definition: GTowerRhoSubtractionAlg.cxx:39