Project
Loading...
Searching...
No Matches
BunchFilling.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
12#include "Framework/Logger.h"
14#include <TClass.h>
15#include <TFile.h>
16#include <cctype>
17
18using namespace o2;
19
20//_________________________________________________
21BunchFilling::BunchFilling(const std::string& beamA, const std::string& beamC)
22{
23 setBCFilling(beamA, 0);
24 setBCFilling(beamC, 1);
26}
27
28//_________________________________________________
29BunchFilling::BunchFilling(const std::string& interactingBC)
30{
31 setBCFilling(interactingBC, -1);
32 mBeamAC[0] = mBeamAC[1] = mPattern;
33}
34
35//_________________________________________________
37{
38 for (int bc = 0; bc < o2::constants::lhc::LHCMaxBunches; bc++) {
39 if (testBC(bc, dir)) {
40 return bc;
41 }
42 }
43 return -1;
44}
45
46//_________________________________________________
48{
50 if (testBC(bc, dir)) {
51 return bc;
52 }
53 }
54 return -1;
55}
56
57//_________________________________________________
58std::vector<int> BunchFilling::getFilledBCs(int dir) const
59{
60 std::vector<int> vb;
61 for (int bc = 0; bc < o2::constants::lhc::LHCMaxBunches; bc++) {
62 if (testBC(bc, dir)) {
63 vb.push_back(bc);
64 }
65 }
66 return vb;
67}
68
69//_________________________________________________
70void BunchFilling::setBC(int bcID, bool active, int dir)
71{
72 // add interacting BC slot
74 LOG(fatal) << "BCid is limited to " << 0 << '-' << o2::constants::lhc::LHCMaxBunches - 1;
75 }
76 if (dir < 0) {
77 mPattern.set(bcID, active);
78 } else {
79 mBeamAC[dir].set(bcID, active);
80 }
81}
82
83//_________________________________________________
85{
87 for (int dir = -1; dir < 2; dir++) {
88 if (other.testBC(bc, dir)) {
89 // if the other one is filled we need to fill "this" also
90 setBC(bc, true, dir);
91 }
92 }
93 }
94}
95
96//_________________________________________________
97void BunchFilling::setBCTrain(int nBC, int bcSpacing, int firstBC, int dir)
98{
99 // add interacting BC train with given spacing starting at given place, i.e.
100 // train with 25ns spacing should have bcSpacing = 1
101 for (int i = 0; i < nBC; i++) {
102 setBC(firstBC, dir);
103 firstBC += bcSpacing;
104 }
105}
106
107//_________________________________________________
108void BunchFilling::setBCTrains(int nTrains, int trainSpacingInBC, int nBC, int bcSpacing, int firstBC, int dir)
109{
110 // add nTrains trains of interacting BCs with bcSpacing within the train and trainSpacingInBC empty slots
111 // between the trains
112 for (int it = 0; it < nTrains; it++) {
113 setBCTrain(nBC, bcSpacing, firstBC, dir);
114 firstBC += nBC * bcSpacing + trainSpacingInBC;
115 }
116}
117
118//_________________________________________________
119void BunchFilling::setBCFilling(const std::string& patt, int dir)
120{
121 auto& dest = dir < 0 ? mPattern : mBeamAC[dir];
122 dest = createPattern(patt);
123}
124
125//_________________________________________________
126void BunchFilling::print(int dir, bool filledOnly, int bcPerLine) const
127{
128 const std::string names[3] = {"Interacting", "Beam-A", "Beam-C"};
129 for (int id = -1; id < 2; id++) {
130 if (id == dir || (dir < -1 || dir > 1)) {
131 printf("%s bunches\n", names[id + 1].c_str());
132 bool endlOK = false;
133 for (int i = 0; i < o2::constants::lhc::LHCMaxBunches; i++) {
134 bool on = testBC(i, id);
135 if (!filledOnly) {
136 printf("%c", on ? '+' : '-');
137 } else if (on) {
138 printf("%4d ", i);
139 } else {
140 continue;
141 }
142 if (((i + 1) % bcPerLine) == 0) {
143 printf("\n");
144 endlOK = true;
145 } else {
146 endlOK = false;
147 }
148 }
149 if (!endlOK) {
150 printf("\n");
151 }
152 }
153 }
154}
155
156//_______________________________________________
157BunchFilling* BunchFilling::loadFrom(const std::string& fileName, const std::string& objName)
158{
159 // load object from file
160 TFile fl(fileName.data());
161 if (fl.IsZombie()) {
162 LOG(error) << "Failed to open " << fileName;
163 return nullptr;
164 }
165 std::string nm = objName.empty() ? o2::BunchFilling::Class()->GetName() : objName;
166 auto bf = reinterpret_cast<o2::BunchFilling*>(fl.GetObjectChecked(nm.c_str(), o2::BunchFilling::Class()));
167 if (!bf) {
168 LOG(error) << "Did not find object named " << nm;
169 return nullptr;
170 }
171 return bf;
172}
173
174//_________________________________________________
176{
177 // create bit pattern from string according to convention of Run1/2:
178 // The string has the following syntax:
179 // "25L 25(2H2LH 3(23HL))"
180 // - H/h -> 1 L/l -> 0
181 // - spaces, new lines are white characters
182 std::vector<unsigned char> input;
183 for (unsigned char c : p) {
184 if (c == '(' || c == ')' || std::isdigit(c)) {
185 input.push_back(c);
186 } else if (c == 'h' || c == 'H') {
187 input.push_back('H');
188 } else if (c == 'l' || c == 'L') {
189 input.push_back('L');
190 } else if (!std::isspace(c)) {
191 throw std::runtime_error(fmt::format("forbidden character ({}) in input string ({})", c, p));
192 }
193 }
194 input.push_back(0); // flag the end
196 int ibit = 0, level = 0;
197 const unsigned char* in = input.data();
198 if (!parsePattern(in, patt, ibit, level)) {
199 throw std::runtime_error(fmt::format("failed to extract BC pattern from input string ({})", p));
200 }
201 return patt;
202}
203
204//_________________________________________________
205std::string BunchFilling::buckets2PatternString(const std::vector<int>& buckets, int ibeam)
206{
207 // create bunches pattern string from filled buckets, in the format parsed by o2::BunchFilling::createPattern
208 Pattern patt;
209 std::string pattS;
210 for (int b : buckets) {
212 }
213 int nh = 0;
214 for (int i = 0; i < o2::constants::lhc::LHCMaxBunches; i++) {
215 if (patt[i]) {
216 if (nh) {
217 pattS += fmt::format("{}L", nh);
218 nh = 0; // reset holes
219 }
220 pattS += "H";
221 } else {
222 nh++;
223 }
224 }
225 return pattS;
226}
227
228//_________________________________________________
229void BunchFilling::buckets2BeamPattern(const std::vector<int>& buckets, int ibeam)
230{
231 // create bunches pattern string from filled buckets, in the format parsed by o2::BunchFilling::createPattern
232 Pattern& patt = mBeamAC[ibeam];
233 for (int b : buckets) {
234 if (b) {
236 }
237 }
238}
239
240//_________________________________________________
241bool BunchFilling::parsePattern(const unsigned char*& input, BunchFilling::Pattern& patt, int& ibit, int& level)
242{
243 // this is analog of AliTriggerBCMask::Bcm2Bits
244 level++;
245 int repetion = 1;
246 auto setBunch = [&patt, &ibit](bool v) {
247 if (ibit < patt.size()) {
248 patt[ibit++] = v;
249 } else {
250 throw std::runtime_error(fmt::format("provided pattern overflow max bunch {}", patt.size()));
251 }
252 };
253 while (1) {
254 if (*input == 0) {
255 if (level > 1) {
256 LOG(error) << "Missing )";
257 return false;
258 }
259 break;
260 }
261 if (*input == 'H') {
262 for (int i = 0; i < repetion; i++) {
263 setBunch(true);
264 }
265 repetion = 1;
266 input++;
267 } else if (*input == 'L') {
268 for (int i = 0; i < repetion; i++) {
269 setBunch(false);
270 }
271 repetion = 1;
272 input++;
273 } else if (std::isdigit(*input)) {
274 repetion = int((*input++) - '0');
275 while (*input && std::isdigit(*input)) {
276 repetion = repetion * 10 + int(*(input++) - '0');
277 }
278 } else if (*input == '(') {
279 input++;
280 auto ibit1 = ibit;
281 if (!parsePattern(input, patt, ibit, level)) {
282 return false;
283 }
284 auto ibit2 = ibit;
285 for (int i = 0; i < repetion - 1; i++) {
286 for (int j = ibit1; j < ibit2; j++) {
287 setBunch(patt[j]);
288 }
289 }
290 repetion = 1;
291 } else if (*input == ')') {
292 input++;
293 if (level <= 1) {
294 LOG(error) << "Incorrectly placed )";
295 return false;
296 }
297 break;
298 } else {
299 LOG(error) << "Unexpected symbol " << *input;
300 return false;
301 }
302 }
303 level--;
304 return true;
305}
uint64_t bc
Definition RawEventData.h:5
int32_t i
uint32_t j
Definition RawData.h:0
uint32_t c
Definition RawData.h:2
void mergeWith(o2::BunchFilling const &other)
std::bitset< o2::constants::lhc::LHCMaxBunches > Pattern
void setBCTrains(int nTrains, int trainSpacingInBC, int nBC, int bcSpacing, int firstBC, int dir=-1)
static std::string buckets2PatternString(const std::vector< int > &buckets, int ibeam)
void setBCTrain(int nBC, int bcSpacing, int firstBC, int dir=-1)
std::vector< int > getFilledBCs(int dir=-1) const
void print(int dir=-2, bool filledOnly=true, int bcPerLine=20) const
int getLastFilledBC(int dir=-1) const
void setBC(int bcID, bool active=true, int dir=-1)
bool testBC(int bcID, int dir=-1) const
int getFirstFilledBC(int dir=-1) const
void setBCFilling(const std::string &patt, int dir=-1)
BunchFilling()=default
void setInteractingBCsFromBeams()
void buckets2BeamPattern(const std::vector< int > &buckets, int ibeam)
static BunchFilling * loadFrom(const std::string &fileName, const std::string &objName="")
static Pattern createPattern(const std::string &p)
const GLdouble * v
Definition glcorearb.h:832
GLboolean GLboolean GLboolean b
Definition glcorearb.h:1233
constexpr int LHCMaxBunches
constexpr int LHCBunch2P2BC(int bunch, BeamDirection dir)
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
VectorOfTObjectPtrs other
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"