ATLAS Offline Software
ZdcInjPulserAmpMap.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
7 #include <fstream>
8 
9 // Get singleton instance
10 //
12 {
13  static const ZdcInjPulserAmpMap pulser_map;
14  return &pulser_map;
15 }
16 
17 
18 //constructor
19 ZdcInjPulserAmpMap::ZdcInjPulserAmpMap() : asg::AsgMessaging("ZdcInjPulserAmpMap")
20 {
21  msg().setLevel(MSG::INFO);
22 
23  // std::string filePath = PathResolverFindCalibFile("ZdcConditions/ZDC_InjectPulseSteps.json"); // change back file name after other changes get merged + ready for T0 reprocessing
24  std::string filePath = PathResolverFindCalibFile("ZdcConditions/INJpulser_combined_2024.json");
25 
26  if (!filePath.empty())
27  {
28  ATH_MSG_DEBUG( "ZdcInjPulserAmpMap::found ZDC JSON at " << filePath );
29  }
30  else
31  {
32  ATH_MSG_WARNING( "ZdcInjPulserAmpMap constructor, JSON file not found in search path, trying local file" ) ;
33  filePath = "./ZDC_InjectPulseSteps.json";
34  }
35 
36 
37  std::ifstream ifs(filePath);
38  if (!ifs.is_open()) {
39  ATH_MSG_FATAL("ZdcInjPulserAmpMap constructor, JSON file cannot be opened!" ) ;
40  }
41 
43  if (parseJsonFile(ifs)) m_validJSon = true;;
44 }
45 
46 bool ZdcInjPulserAmpMap::parseJsonFile(std::ifstream& ifs)
47 {
48  try {
50 
51  // Check for the existence of the run range section of the json file
52  //
53  auto rangeIter = j.find("RunRanges");
54  if (rangeIter == j.end()) return false;
55 
56  //
57  // We loop over all the defined run ranges and save
58  //
59  for (json range : *rangeIter) {
60  unsigned int first = range.at("First");
61  unsigned int last = range.at("Last");
62  std::string name = range.at("StepsConfig");
63  float scale = range.at("ScaleFactor");
64 
65  m_runRangeDescrs.push_back(std::make_tuple(first, last, name, scale));
66 
67  ATH_MSG_DEBUG( "ZdcInjPulserAmpMap::found run range: first, last = " << first << ", " << last
68  << ", config name = " << name << ", scale factor = " << scale );
69  }
70 
71  auto stepConfigIter = j.find("StepConfigurations");
72  if (stepConfigIter == j.end()) return false;
73 
74  // Now loop over the configurations and save
75  //
76  for (json::iterator configIt = stepConfigIter->begin(); configIt != stepConfigIter->end(); ++configIt) {
77  std::string confName = configIt.key();
78 
79  // First we make a spot in the map
80  //
81  auto [insertIter, insertResult] = m_stepsConfigs.insert(std::make_pair(confName, StepsDescr()));
82  if (!insertResult) return false;
83 
84  // And now we fill it
85  //
86  json steps = configIt.value();
87  if (!steps.is_array()) {
88  return false;
89  }
90 
91  // Unpack the elements of the step confg array
92  //
93  StepsDescr& stepsDesc = insertIter->second;
94  readPulserSteps(stepsDesc, steps);
95 
96  ATH_MSG_DEBUG( "ZdcInjPulserAmpMap::found steps configuration with name " << confName << ", starting LB = "
97  << stepsDesc.first << ", number of LBs = " << stepsDesc.second.size());
98  }
99  }
100  catch (...) {
101  return false;
102  }
103 
104  return true;
105 }
106 
108 {
109  // Loop over all entries in the json array
110  //
111  for (unsigned int index = 0; index < stepsJson.size(); index++) {
112  const json& elem = stepsJson[index];
113 
114  // Get the starting LB entry in the json if it exists
115  //
116  if (index ==0 && elem.size() == 1) {
117  if (elem.find("startLB") != elem.end()) {
118  steps.first = elem["startLB"];
119  }
120  }
121  else {
122  // Fill out the pulser amplitudes for this step
123  //
124  fillVVector(steps.second, elem);
125  }
126  }
127 }
128 
130 {
131  unsigned int nStep = entry.at("nStep");
132  float vStart = entry.at("vStart");
133  float vStep = entry.at("vStep");
134 
135  float voltage = vStart;
136  for (size_t step = 0; step < nStep; step++) {
137  stepVec.push_back(voltage);
138  voltage += vStep;
139  }
140 }
141 
143 {
144  std::string configName = (allowDefault ? "Default" : "_none_");
145  float scaleFactor = 1;
146 
147  for ( auto rangeDescr : m_runRangeDescrs) {
148  if (runNumber >= std::get<0>(rangeDescr) && runNumber < std::get<1>(rangeDescr)) {
149  configName = std::get<2>(rangeDescr);
150  scaleFactor = std::get<3>(rangeDescr);
151 
152  ATH_MSG_DEBUG( "ZdcInjPulserAmpMap::lookupRun(): found config name " << configName << " for run number "
153  << runNumber);
154  }
155  }
156 
157 
158  // Find the corresponding configuration -- we hope
159  //
160  auto findIter = m_stepsConfigs.find(configName);
161  if (findIter == m_stepsConfigs.end()) return Token();
162 
163  // Now we make a new active configuration -- the only thing that needs to be thread-protected
164  //
165  const std::lock_guard<std::mutex> lock(m_lock);
166 
167  // Put a new entry in the active configuration vector
168  //
169  int newIndex = m_activeConfigs.size();
170  m_activeConfigs.push_back(&findIter->second);
171 
172  ATH_MSG_DEBUG( "ZdcInjPulserAmpMap::lookupRun(): creating token for run number " << runNumber
173  << " with index " << newIndex << ", and scaleFactor " << scaleFactor);
174 
175  return Token(newIndex, scaleFactor);
176 }
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
ZdcInjPulserAmpMap::StepsVector
std::vector< float > StepsVector
Definition: ZdcInjPulserAmpMap.h:55
ZdcInjPulserAmpMap.h
ZdcInjPulserAmpMap::parseJsonFile
bool parseJsonFile(std::ifstream &ifs)
Definition: ZdcInjPulserAmpMap.cxx:46
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
json
nlohmann::json json
Definition: HistogramDef.cxx:9
ZdcInjPulserAmpMap::ZdcInjPulserAmpMap
ZdcInjPulserAmpMap()
Definition: ZdcInjPulserAmpMap.cxx:19
index
Definition: index.py:1
parse
std::map< std::string, std::string > parse(const std::string &list)
Definition: egammaLayerRecalibTool.cxx:1054
asg
Definition: DataHandleTestTool.h:28
ZdcInjPulserAmpMap::m_validJSon
bool m_validJSon
Definition: ZdcInjPulserAmpMap.h:68
ZdcInjPulserAmpMap::lookupRun
Token lookupRun(unsigned int runNumber, bool allowDefault=false)
Definition: ZdcInjPulserAmpMap.cxx:142
yodamerge_tmp.scale
scale
Definition: yodamerge_tmp.py:138
ZdcInjPulserAmpMap::m_activeConfigs
std::vector< const StepsDescr * > m_activeConfigs
Definition: ZdcInjPulserAmpMap.h:80
ZdcInjPulserAmpMap::m_filePath
std::string m_filePath
Definition: ZdcInjPulserAmpMap.h:67
ZdcInjPulserAmpMap::fillVVector
void fillVVector(StepsVector &stepVec, const nlohmann::json &entry)
Definition: ZdcInjPulserAmpMap.cxx:129
ZdcInjPulserAmpMap::readPulserSteps
void readPulserSteps(StepsDescr &steps, const json &stepsJson)
Definition: ZdcInjPulserAmpMap.cxx:107
ZdcInjPulserAmpMap::m_runRangeDescrs
std::vector< RunRangeDescr > m_runRangeDescrs
Definition: ZdcInjPulserAmpMap.h:72
beamspotman.steps
int steps
Definition: beamspotman.py:505
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
asg::AsgMessaging::msg
MsgStream & msg() const
The standard message stream.
Definition: AsgMessaging.cxx:49
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
ZdcInjPulserAmpMap::Token
Definition: ZdcInjPulserAmpMap.h:28
GetAllXsec.entry
list entry
Definition: GetAllXsec.py:132
PathResolver.h
hancool.filePath
string filePath
Definition: hancool.py:28
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
ZdcInjPulserAmpMap::getInstance
static const ZdcInjPulserAmpMap * getInstance()
Definition: ZdcInjPulserAmpMap.cxx:11
PathResolverFindCalibFile
std::string PathResolverFindCalibFile(const std::string &logical_file_name)
Definition: PathResolver.cxx:431
DeMoAtlasDataLoss.runNumber
string runNumber
Definition: DeMoAtlasDataLoss.py:64
DeMoScan.index
string index
Definition: DeMoScan.py:364
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
ZdcInjPulserAmpMap::json
nlohmann::json json
Definition: ZdcInjPulserAmpMap.h:51
DeMoScan.first
bool first
Definition: DeMoScan.py:536
ZdcInjPulserAmpMap::m_lock
std::mutex m_lock
Definition: ZdcInjPulserAmpMap.h:62
LArCellBinning.step
step
Definition: LArCellBinning.py:158
ZdcInjPulserAmpMap::m_stepsConfigs
std::map< std::string, StepsDescr > m_stepsConfigs
Definition: ZdcInjPulserAmpMap.h:76
ZdcInjPulserAmpMap
Definition: ZdcInjPulserAmpMap.h:25
ZdcInjPulserAmpMap::StepsDescr
std::pair< unsigned int, StepsVector > StepsDescr
Definition: ZdcInjPulserAmpMap.h:56