ATLAS Offline Software
LArNNRawChannelBuilder.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2024 CERN for the benefit of the ATLAS collaboration
3  */
4 
7 
8 #include "GaudiKernel/SystemOfUnits.h"
10 
15 #include <cmath>
16 
17 #include "lwtnn/LightweightGraph.hh"
18 #include "lwtnn/parse_json.hh"
19 #include <map>
20 #include <fstream>
21 
22 
23 
34  if (m_useDBFortQ) {
36  ATH_MSG_ERROR ("useDB requested but neither Run1DSPThresholdsKey nor Run2DSPThresholdsKey initialized.");
37  return StatusCode::FAILURE;
38  }
39  }
40 
41  ATH_CHECK(detStore()->retrieve(m_onlineId,"LArOnlineID"));
42 
43  return StatusCode::SUCCESS;
44 }
45 
46 
47 StatusCode LArNNRawChannelBuilder::execute(const EventContext& ctx) const {
48 
49  //Get event inputs from read handles:
51 
52  //Write output via write handle
53 
54  auto outputContainerLRPtr = std::make_unique<LArRawChannelContainer>();
55 
56  //Get Conditions input
58  const ILArPedestal* peds = *pedHdl;
59 
61  const LArADC2MeV* adc2MeVs = *adc2mevHdl;
62 
64 
65  //Loop over digits:
66  for (const LArDigit* digit : *inputContainer) {
67 
68  const HWIdentifier id = digit->hardwareID();
69  const bool connected = (*cabling)->isOnlineConnected(id);
70 
71 
72  ATH_MSG_VERBOSE("Working on channel " << m_onlineId->channel_name(id));
73 
74  const std::vector<short>& samples = digit->samples();
75  const size_t nSamples = 5; //hardcoded as the network always uses 5 samples
76 
77  if ( (samples.size() - m_firstSample) < nSamples) {
78  ATH_MSG_ERROR("mismatched effective sample size: "<< samples.size() - m_firstSample << ", must be > " << nSamples);
79  return StatusCode::FAILURE;
80  }
81 
82  //The following creates a sub-sample vector of the correct size to be passed on to the NN
83  std::vector<short>subsamples(nSamples, 0.0);
84  for (size_t i = m_firstSample; i < m_firstSample+nSamples; ++i) {
85  subsamples[i-m_firstSample] = samples[i];
86  }
87 
88  const int gain = digit->gain();
89  const float p = peds->pedestal(id, gain);
90 
91 
92  //The following autos will resolve either into vectors or vector-proxies
93  const auto& adc2mev = adc2MeVs->ADC2MEV(id, gain);
94 
96  if (!connected) continue; //No conditions for disconencted channel, who cares?
97  ATH_MSG_ERROR("No valid pedestal for connected channel " << m_onlineId->channel_name(id)
98  << " gain " << gain);
99  return StatusCode::FAILURE;
100  }
101 
102  if (ATH_UNLIKELY(adc2mev.size() < 2)) {
103  if (!connected) continue; //No conditions for disconencted channel, who cares?
104  ATH_MSG_ERROR("No valid ADC2MeV for connected channel " << m_onlineId->channel_name(id)
105  << " gain " << gain);
106  return StatusCode::FAILURE;
107  }
108 
109  // Compute amplitude
110  float An = 0;
111  float A = 0;
112  bool saturated = false;
113 
114  // Check saturation AND discount pedestal
115  std::vector<float>samp_no_ped(nSamples, 0.0);
116  for (size_t i = 0; i < nSamples; ++i) {
117  if (subsamples[i] == 4096 || subsamples[i] == 0) saturated = true;
118  samp_no_ped[i] = subsamples[i]-p;
119  }
120 
121  // LWTNN configuration
122  std::map<std::string, std::map<std::string, double> >inputs;
123  std::map<std::string, std::map<std::string, std::vector<double> > >input_sequences;
124 
125 
126  // Read in the network file
127  std::ifstream in_file(m_nn_json);
128 
129 
130  std::vector<double>nn_in;
131 
132  nn_in.clear();
133 
134  for (auto d : subsamples) {
135 
136  nn_in.push_back((d-p)/4096.0);
137 
138  }
139 
140 
141  auto config = lwt::parse_json_graph(in_file);
142  lwt::LightweightGraph graph(config, m_network_output);
143 
144  // Define some sequences used for the recurrent NN
145  input_sequences["node_0"] = {
146  {m_input_node, nn_in}
147  };
148 
149  // More variables for the two other inputs defined
150  inputs["node_0"] = {{"variable_0", 0}};
151 
152  // Calculate the output value of the NN based on the inputs given
153  auto outputs = graph.compute(inputs, input_sequences);
154 
155  //normalised output
156  An = outputs.begin()->second;
157 
158  //taking the normalisation into account
159  A = An*4096.0;
160 
161  //Apply Ramp
162  const float E = adc2mev[0]+A*adc2mev[1];
163 
164  uint16_t iquaShort = 0;
165  float tau = 0;
166 
167 
169  if (saturated) prov |= LArProv::SATURATED;
170 
171 
172  outputContainerLRPtr->emplace_back(id, static_cast<int>(std::floor(E+0.5)),
173  static_cast<int>(std::floor(tau+0.5)),
174  iquaShort, prov, (CaloGain::CaloGain)gain);
175 
176  }
177 
179  ATH_CHECK(outputContainer.record(std::move(outputContainerLRPtr) ) );
180 
181 
182  return StatusCode::SUCCESS;
183 }
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
ILArPedestal::pedestal
virtual float pedestal(const HWIdentifier &id, int gain) const =0
LArADC2MeV::ADC2MEV
const LArVectorProxy ADC2MEV(const HWIdentifier &id, int gain) const
Definition: LArADC2MeV.h:32
LArNNRawChannelBuilder::initialize
StatusCode initialize() override
Definition: LArNNRawChannelBuilder.cxx:24
LArNNRawChannelBuilder::m_shapeKey
SG::ReadCondHandleKey< ILArShape > m_shapeKey
Definition: LArNNRawChannelBuilder.h:57
LArNNRawChannelBuilder::m_input_node
Gaudi::Property< std::string > m_input_node
Definition: LArNNRawChannelBuilder.h:68
SG::ReadCondHandle
Definition: ReadCondHandle.h:44
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
LArNNRawChannelBuilder::m_run1DSPThresholdsKey
SG::ReadCondHandleKey< LArDSPThresholdsComplete > m_run1DSPThresholdsKey
Definition: LArNNRawChannelBuilder.h:58
LArRawChannelBuilderAlg.h
hist_file_dump.d
d
Definition: hist_file_dump.py:137
ILArPedestal
Definition: ILArPedestal.h:12
LArNNRawChannelBuilder.h
CaloCondBlobAlgs_fillNoiseFromASCII.gain
gain
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:110
ReadCellNoiseFromCool.cabling
cabling
Definition: ReadCellNoiseFromCool.py:154
LArNNRawChannelBuilder::m_ofcKey
SG::ReadCondHandleKey< ILArOFC > m_ofcKey
Definition: LArNNRawChannelBuilder.h:56
ATH_UNLIKELY
#define ATH_UNLIKELY(x)
Definition: AthUnlikelyMacros.h:17
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
checkRpcDigits.digit
digit
Definition: checkRpcDigits.py:186
SG::VarHandleKey::empty
bool empty() const
Test if the key is blank.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:150
HWIdentifier
Definition: HWIdentifier.h:13
postInclude.inputs
inputs
Definition: postInclude.SortInput.py:15
config
Definition: PhysicsAnalysis/AnalysisCommon/AssociationUtils/python/config.py:1
LArNNRawChannelBuilder::m_run2DSPThresholdsKey
SG::ReadCondHandleKey< AthenaAttributeList > m_run2DSPThresholdsKey
Definition: LArNNRawChannelBuilder.h:59
AthCommonDataStore< AthCommonMsg< Gaudi::Algorithm > >::detStore
const ServiceHandle< StoreGateSvc > & detStore() const
The standard StoreGateSvc/DetectorStore Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:95
LArNNRawChannelBuilder::m_rawChannelKey
SG::WriteHandleKey< LArRawChannelContainer > m_rawChannelKey
Definition: LArNNRawChannelBuilder.h:50
LArNNRawChannelBuilder::m_network_output
Gaudi::Property< std::string > m_network_output
Definition: LArNNRawChannelBuilder.h:69
xAOD::saturated
setScaleOne setStatusOne saturated
Definition: gFexGlobalRoI_v1.cxx:51
A
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
xAOD::uint16_t
setWord1 uint16_t
Definition: eFexEMRoI_v1.cxx:93
LArDigit
Liquid Argon digit base class.
Definition: LArDigit.h:25
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
LArNNRawChannelBuilder::m_nn_json
Gaudi::Property< std::string > m_nn_json
Definition: LArNNRawChannelBuilder.h:67
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
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
ILArPedestal::ERRORCODE
@ ERRORCODE
Definition: ILArPedestal.h:47
LArNNRawChannelBuilder::m_useDBFortQ
Gaudi::Property< bool > m_useDBFortQ
Definition: LArNNRawChannelBuilder.h:72
python.CreateTierZeroArgdict.outputs
outputs
Definition: CreateTierZeroArgdict.py:189
LArDSPThresholdsFlat.h
LArNNRawChannelBuilder::m_digitKey
SG::ReadHandleKey< LArDigitContainer > m_digitKey
Definition: LArNNRawChannelBuilder.h:47
LArProv::PEAKNN
@ PEAKNN
Definition: LArProvenance.h:20
LArProv::PEDDB
@ PEDDB
Definition: LArProvenance.h:26
LArNNRawChannelBuilder::m_adc2MeVKey
SG::ReadCondHandleKey< LArADC2MeV > m_adc2MeVKey
Definition: LArNNRawChannelBuilder.h:54
VP1PartSpect::E
@ E
Definition: VP1PartSpectFlags.h:21
LArNNRawChannelBuilder::execute
StatusCode execute(const EventContext &ctx) const override
Definition: LArNNRawChannelBuilder.cxx:47
SG::CondHandleKey::initialize
StatusCode initialize(bool used=true)
LArNNRawChannelBuilder::m_onlineId
const LArOnlineID * m_onlineId
Definition: LArNNRawChannelBuilder.h:65
LArDigitContainer.h
LArProv::RAMPDB
@ RAMPDB
Definition: LArProvenance.h:23
CaloGain::CaloGain
CaloGain
Definition: CaloGain.h:11
LArADC2MeV
Definition: LArADC2MeV.h:21
lwtDev::parse_json_graph
GraphConfig parse_json_graph(std::istream &json)
Definition: parse_json.cxx:71
SG::WriteHandle
Definition: StoreGate/StoreGate/WriteHandle.h:76
SG::WriteHandle::record
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
LArProvenance.h
LArDigits2NtupleDumper.nSamples
nSamples
Definition: LArDigits2NtupleDumper.py:70
LArOnlineID_Base::channel_name
std::string channel_name(const HWIdentifier id) const
Return a string corresponding to a feedthrough name given an identifier.
Definition: LArOnlineID_Base.cxx:219
SG::AllowEmpty
@ AllowEmpty
Definition: StoreGate/StoreGate/VarHandleKey.h:30
LArNNRawChannelBuilder::m_pedestalKey
SG::ReadCondHandleKey< ILArPedestal > m_pedestalKey
Definition: LArNNRawChannelBuilder.h:53
LArNNRawChannelBuilder::m_firstSample
Gaudi::Property< int > m_firstSample
Definition: LArNNRawChannelBuilder.h:62
LArRawChannelContainer.h
LArOnlineID.h
LArNNRawChannelBuilder::m_cablingKey
SG::ReadCondHandleKey< LArOnOffIdMapping > m_cablingKey
Definition: LArNNRawChannelBuilder.h:55
LArProv::SATURATED
@ SATURATED
Definition: LArProvenance.h:31