ATLAS Offline Software
Loading...
Searching...
No Matches
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{
26public:
27 class Token
28 {
31
32 int value() const {return m_index;}
33 float scaleFactor() const {return m_scaleFactor;}
34
39
40 public:
41 friend class ZdcInjPulserAmpMap;
42
44 m_index(-1),
46 {}
47
48 bool isValid() const {return m_index != -1;}
49 };
50
51 using json = nlohmann::json;
52
53private:
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 //
62 std::mutex m_lock;
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
106public:
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 //
This class provides a token that identifies in a unique way objects on the persistent storage.
Definition Token.h:21
Token(int index, float scaleFactor)
std::map< std::string, StepsDescr > m_stepsConfigs
std::vector< const StepsDescr * > m_activeConfigs
bool parseJsonFile(std::ifstream &ifs)
std::pair< unsigned int, StepsVector > getContext(const Token &token) const
std::pair< unsigned int, StepsVector > StepsDescr
float getPulserAmplitude(const Token &token, unsigned int lumiBlock) const
std::tuple< unsigned int, unsigned int, std::string, float > RunRangeDescr
const std::string & getFilePath() const
int getCycleNumber(const Token &token, unsigned int lumiBlock) const
void readPulserSteps(StepsDescr &steps, const json &stepsJson)
static const ZdcInjPulserAmpMap * getInstance()
std::vector< float > StepsVector
std::vector< RunRangeDescr > m_runRangeDescrs
Token lookupRun(unsigned int runNumber, bool allowDefault=false)
unsigned int getFirstLumiBlock(const Token &token) const
void fillVVector(StepsVector &stepVec, const nlohmann::json &entry)
unsigned int getNumSteps(const Token &token) const
Class mimicking the AthMessaging class from the offline software.
STL class.
Definition index.py:1