ATLAS Offline Software
ZdcInjPulserAmpMap.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #ifndef _ZDCINJPULSERAMPMAP_H
6 #define _ZDCINJPULSERAMPMAP_H
7 
9 #include <nlohmann/json.hpp>
10 
11 #include <string>
12 #include <vector>
13 #include <cmath>
14 
15 
16 #include <iostream>
17 #include <string>
18 #include <tuple>
19 #include <map>
20 #include <mutex>
21 #include <vector>
22 #include <utility>
23 
25 {
26 public:
27  class Token
28  {
29  int m_index;
31 
32  int value() const {return m_index;}
33  float scaleFactor() const {return m_scaleFactor;}
34 
35  Token(int index, float scaleFactor) :
36  m_index(index),
38  {}
39 
40  public:
41  friend class ZdcInjPulserAmpMap;
42 
43  Token() :
44  m_index(-1),
45  m_scaleFactor(1)
46  {}
47 
48  bool isValid() const {return m_index != -1;}
49  };
50 
52 
53 private:
54 
55  using StepsVector = std::vector<float>;
56  using StepsDescr = std::pair<unsigned int, StepsVector>;
57 
58  using RunRangeDescr = std::tuple<unsigned int, unsigned int, std::string, float>;
59 
60  // Mutex for thread safety
61  //
63 
64  //
65  // Data members
66  //
67  std::string m_filePath;
68  bool m_validJSon{false};
69 
70  // List of run ranges in json file
71  //
72  std::vector<RunRangeDescr> m_runRangeDescrs;
73 
74  // Map of step configurations indexed by config name
75  //
76  std::map<std::string, StepsDescr> m_stepsConfigs;
77 
78  // For multi-threaded operation, we keep multiple active configurations with index as the token
79  //
80  std::vector<const StepsDescr*> m_activeConfigs;
81 
82  // Private Methods
83  //
84  bool parseJsonFile(std::ifstream& ifs);
85  void readPulserSteps(StepsDescr& steps, const json& stepsJson);
86  void fillVVector(StepsVector& stepVec, const nlohmann::json& entry);
87 
88  std::pair<unsigned int, StepsVector> getContext(const Token& token) const
89  {
90  if (!m_validJSon) {
91  throw std::runtime_error("Invalid Injected Pulser configuration");
92  }
93  if (!token.isValid() || size_t(token.value()) >= m_activeConfigs.size()) {
94  throw std::runtime_error("Invalid Injected Pulser configuration");
95  }
96 
97  unsigned int index = token.value();
98  const StepsDescr& stepsConfig = *(m_activeConfigs[index]);
99 
100  unsigned int firstLB = stepsConfig.first;
101  const StepsVector& steps = stepsConfig.second;
102 
103  return std::make_pair(firstLB, steps);
104  }
105 
106 public:
107 
109 
110  static const ZdcInjPulserAmpMap* getInstance();
111 
112  const std::string& getFilePath() const {return m_filePath;}
113 
114  Token lookupRun(unsigned int runNumber, bool allowDefault = false);
115 
116  // Return the lumi block number at which we start stepping through the different aplitudes
117  //
118  unsigned int getFirstLumiBlock(const Token& token) const {
119  auto result = getContext(token);
120  return result.first;
121  }
122 
123  unsigned int getNumSteps(const Token& token) const {
124  auto result = getContext(token);
125  return result.second.size();
126  }
127 
128  // Return the cycle within which the lumi block falls
129  //
130  int getCycleNumber(const Token& token, unsigned int lumiBlock) const
131  {
132  auto [firstLB, map] = getContext(token);
133 
134  if (lumiBlock < firstLB) return -1;
135  return std::floor(float(lumiBlock - firstLB)/map.size());
136  }
137 
138  // Return the pulser amplitude for the given lumi block
139  //
140  float getPulserAmplitude(const Token& token, unsigned int lumiBlock) const
141  {
142  auto [firstLB, map] = getContext(token);
143 
144  // We do a cyclic lookup of the pulser amplitude (in volts) starting from the first LB
145  //
146  if (lumiBlock < firstLB) return -1000.;
147  unsigned int vecIndex = (lumiBlock - firstLB) % map.size();
148  return map.at(vecIndex) * token.scaleFactor();
149  }
150 };
151 
152 #endif //
ZdcInjPulserAmpMap::StepsVector
std::vector< float > StepsVector
Definition: ZdcInjPulserAmpMap.h:55
ZdcInjPulserAmpMap::parseJsonFile
bool parseJsonFile(std::ifstream &ifs)
Definition: ZdcInjPulserAmpMap.cxx:46
get_generator_info.result
result
Definition: get_generator_info.py:21
json
nlohmann::json json
Definition: HistogramDef.cxx:9
ZdcInjPulserAmpMap::getCycleNumber
int getCycleNumber(const Token &token, unsigned int lumiBlock) const
Definition: ZdcInjPulserAmpMap.h:130
ZdcInjPulserAmpMap::ZdcInjPulserAmpMap
ZdcInjPulserAmpMap()
Definition: ZdcInjPulserAmpMap.cxx:19
ZdcInjPulserAmpMap::RunRangeDescr
std::tuple< unsigned int, unsigned int, std::string, float > RunRangeDescr
Definition: ZdcInjPulserAmpMap.h:58
ZdcInjPulserAmpMap::getFilePath
const std::string & getFilePath() const
Definition: ZdcInjPulserAmpMap.h:112
index
Definition: index.py:1
BeamSpot::mutex
std::mutex mutex
Definition: InDetBeamSpotVertex.cxx:18
ZdcInjPulserAmpMap::getNumSteps
unsigned int getNumSteps(const Token &token) const
Definition: ZdcInjPulserAmpMap.h:123
ZdcInjPulserAmpMap::getContext
std::pair< unsigned int, StepsVector > getContext(const Token &token) const
Definition: ZdcInjPulserAmpMap.h:88
ZdcInjPulserAmpMap::m_validJSon
bool m_validJSon
Definition: ZdcInjPulserAmpMap.h:68
ZdcInjPulserAmpMap::lookupRun
Token lookupRun(unsigned int runNumber, bool allowDefault=false)
Definition: ZdcInjPulserAmpMap.cxx:142
ZdcInjPulserAmpMap::Token::Token
Token()
Definition: ZdcInjPulserAmpMap.h:43
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
Token
This class provides a token that identifies in a unique way objects on the persistent storage.
Definition: Token.h:21
ZdcInjPulserAmpMap::Token::m_index
int m_index
Definition: ZdcInjPulserAmpMap.h:29
ZdcInjPulserAmpMap::readPulserSteps
void readPulserSteps(StepsDescr &steps, const json &stepsJson)
Definition: ZdcInjPulserAmpMap.cxx:107
AsgMessaging.h
ZdcInjPulserAmpMap::m_runRangeDescrs
std::vector< RunRangeDescr > m_runRangeDescrs
Definition: ZdcInjPulserAmpMap.h:72
ZdcInjPulserAmpMap::Token::isValid
bool isValid() const
Definition: ZdcInjPulserAmpMap.h:48
beamspotman.steps
int steps
Definition: beamspotman.py:505
ZdcInjPulserAmpMap::Token::scaleFactor
float scaleFactor() const
Definition: ZdcInjPulserAmpMap.h:33
ZdcInjPulserAmpMap::Token
Definition: ZdcInjPulserAmpMap.h:28
GetAllXsec.entry
list entry
Definition: GetAllXsec.py:132
ZdcInjPulserAmpMap::Token::Token
Token(int index, float scaleFactor)
Definition: ZdcInjPulserAmpMap.h:35
asg::AsgMessaging
Class mimicking the AthMessaging class from the offline software.
Definition: AsgMessaging.h:40
ZdcInjPulserAmpMap::getFirstLumiBlock
unsigned int getFirstLumiBlock(const Token &token) const
Definition: ZdcInjPulserAmpMap.h:118
ZdcInjPulserAmpMap::getPulserAmplitude
float getPulserAmplitude(const Token &token, unsigned int lumiBlock) const
Definition: ZdcInjPulserAmpMap.h:140
ZdcInjPulserAmpMap::getInstance
static const ZdcInjPulserAmpMap * getInstance()
Definition: ZdcInjPulserAmpMap.cxx:11
DeMoAtlasDataLoss.runNumber
string runNumber
Definition: DeMoAtlasDataLoss.py:64
DeMoScan.index
string index
Definition: DeMoScan.py:364
ZdcInjPulserAmpMap::json
nlohmann::json json
Definition: ZdcInjPulserAmpMap.h:51
ZdcInjPulserAmpMap::m_lock
std::mutex m_lock
Definition: ZdcInjPulserAmpMap.h:62
ZdcInjPulserAmpMap::m_stepsConfigs
std::map< std::string, StepsDescr > m_stepsConfigs
Definition: ZdcInjPulserAmpMap.h:76
ZdcInjPulserAmpMap::Token::value
int value() const
Definition: ZdcInjPulserAmpMap.h:32
ZdcInjPulserAmpMap
Definition: ZdcInjPulserAmpMap.h:25
ZdcInjPulserAmpMap::StepsDescr
std::pair< unsigned int, StepsVector > StepsDescr
Definition: ZdcInjPulserAmpMap.h:56
xAOD::lumiBlock
setTeId lumiBlock
Definition: L2StandAloneMuon_v1.cxx:327
ZdcInjPulserAmpMap::Token::m_scaleFactor
float m_scaleFactor
Definition: ZdcInjPulserAmpMap.h:30