Project
Loading...
Searching...
No Matches
getRunParameters.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// executable to get the interaction rate and duration of a run from CCDB
13
14#include <fstream>
15#include <cstdio>
18#include "CCDB/CcdbApi.h"
24#include "CommonTypes/Units.h"
25#include <boost/program_options.hpp>
26
27using namespace o2::ctp;
28namespace bpo = boost::program_options;
29
30const double orbitDuration = 88.924596234; // us
31
32void writeIRtoFile(float ir)
33{
34
35 FILE* fptr = fopen("IR.txt", "w");
36 if (fptr == nullptr) {
37 LOGP(fatal, "ERROR: Could not open file to write IR!");
38 return;
39 }
40 fprintf(fptr, "%.2f", ir);
41 fclose(fptr);
42}
43
44void writeDurationToFile(long duration)
45{
46
47 FILE* fptr = fopen("Duration.txt", "w");
48 if (fptr == nullptr) {
49 LOGP(fatal, "ERROR: Could not open file to write duration!");
50 return;
51 }
52 fprintf(fptr, "%ld", duration);
53 fclose(fptr);
54}
55
57{
58
59 FILE* fptr = fopen("BField.txt", "w");
60 if (fptr == nullptr) {
61 LOGP(fatal, "ERROR: Could not open file to write B field!");
62 return;
63 }
64 fprintf(fptr, "%.2f", b);
65 fclose(fptr);
66}
67
68void writeDetListToFile(std::string detList)
69{
70 FILE* fptr = fopen("DetList.txt", "w");
71 if (fptr == nullptr) {
72 LOGP(fatal, "ERROR: Could not open file to write detector list!");
73 return;
74 }
75 fprintf(fptr, "%s", detList.c_str());
76 fclose(fptr);
77}
78
79bool initOptionsAndParse(bpo::options_description& options, int argc, char* argv[], bpo::variables_map& vm)
80{
81 options.add_options()(
82 "run,r", bpo::value<int>()->default_value(8), "Run number to inspect")(
83 "enable-debug,d", bpo::value<bool>()->default_value(false)->implicit_value(true), "Enable debug logs")(
84 "help,h", "Produce help message.");
85
86 try {
87 bpo::store(parse_command_line(argc, argv, options), vm);
88
89 // help
90 if (vm.count("help")) {
91 std::cout << options << std::endl;
92 return false;
93 }
94
95 bpo::notify(vm);
96 } catch (const bpo::error& e) {
97 std::cerr << e.what() << "\n\n";
98 std::cerr << "Error parsing command line arguments; Available options:\n";
99
100 std::cerr << options << std::endl;
101 return false;
102 }
103 return true;
104}
105
106int main(int argc, char* argv[])
107{
108 bpo::options_description options("Allowed options");
109 bpo::variables_map vm;
110 if (!initOptionsAndParse(options, argc, argv, vm)) {
111 return -1;
112 }
113
114 auto run = vm["run"].as<int>();
115 auto debug = vm["enable-debug"].as<bool>();
116
117 float ir = 0.f;
118 long duration = 0;
119 // duration as O2end - O2start:
120 auto& ccdb_inst = o2::ccdb::BasicCCDBManager::instance();
121 ccdb_inst.setURL("http://alice-ccdb.cern.ch");
122 std::pair<uint64_t, uint64_t> run_times = ccdb_inst.getRunDuration(run);
123 long run_O2duration = long(run_times.second - run_times.first);
124 // access SOR and EOR timestamps
125 int64_t tsSOR = run_times.first; // ms
126 int64_t tsEOR = run_times.second; // ms
127 LOGP(info, "tsSOR = {} ms, tsEOR = {} ms", tsSOR, tsEOR);
128
129 // first we get the B field
130 LOGP(info, "Getting B field");
131 std::map<std::string, std::string> metadata;
132 ccdb_inst.setFatalWhenNull(true);
133 o2::parameters::GRPMagField* magField = ccdb_inst.getSpecific<o2::parameters::GRPMagField>("GLO/Config/GRPMagField", tsSOR, metadata);
134 o2::units::Current_t magFieldL3Curr = magField->getL3Current();
135 LOGP(info, "run {}: B field = {}", run, magFieldL3Curr);
136 writeBFieldToFile((float)magFieldL3Curr);
137
138 // getting the detector list
139 LOGP(info, "Getting detector participating in the run");
140 std::map<std::string, std::string> metadataRun;
141 metadataRun["runNumber"] = std::to_string(run);
142 o2::parameters::GRPECSObject* ecsObj = ccdb_inst.getSpecific<o2::parameters::GRPECSObject>("GLO/Config/GRPECS", tsSOR, metadataRun);
143 std::string dets = "";
145 if (ecsObj->isDetReadOut(i)) {
146 dets = dets + o2::detectors::DetID::getName(i) + " ";
147 }
148 }
149 LOGP(info, "run {}: detectors in readout = {}", run, dets);
150 writeDetListToFile(dets);
151
152 LOGP(info, "Checking IR and duration");
153
154 // Extract CTP info
155 ccdb_inst.setFatalWhenNull(false);
156 metadata["runNumber"] = Form("%d", run);
157 o2::ctp::CTPRunScalers* scl = ccdb_inst.getSpecific<o2::ctp::CTPRunScalers>("CTP/Calib/Scalers", tsSOR, metadata);
158 if (!scl) {
159 LOGP(info, "CTP/Calib/Scalers object does not exist in production CCDB, trying test CCDB");
160 ccdb_inst.setURL("http://ccdb-test.cern.ch:8080");
161 scl = ccdb_inst.getSpecific<o2::ctp::CTPRunScalers>("CTP/Calib/Scalers", tsSOR, metadata);
162 if (!scl) {
163 LOGP(info, "Cannot get IR for run {} neither from production nor test CCDB, writing -1.f", run);
164 LOGP(info, "In addition, the duration for these runs is O2end - O2start: if the run was short, this might overestimate the duration");
165 ir = -1.f;
167 writeDurationToFile(run_O2duration);
168 return 0;
169 }
170 }
171
172 scl->convertRawToO2();
173 std::vector<CTPScalerRecordO2> mScalerRecordO2 = scl->getScalerRecordO2();
174 int n = mScalerRecordO2.size();
175 if (n != 0) {
176 std::int64_t totScalers = 0;
177 std::vector<int64_t> vOrbit;
178 std::vector<int64_t> vScaler;
179 int i = 0;
180 for (auto& record : mScalerRecordO2) {
181 if (debug) {
182 record.printStream(std::cout);
183 }
184 std::vector<CTPScalerO2>& scalers = record.scalers;
185 o2::InteractionRecord& intRecord = record.intRecord;
186 vOrbit.push_back(intRecord.orbit);
187 if (debug) {
188 LOGP(info, "{} orbit = {} scalers = {}", i, intRecord.orbit, scalers[0].lmBefore);
189 }
190 vScaler.push_back(scalers[0].lmBefore); // use scalers for class 0 (usually TVX). TODO: extract info on class id from trigger config
191 totScalers += scalers[0].lmBefore;
192 ++i;
193 }
194
195 duration = std::round((vOrbit.back() - vOrbit.front()) * orbitDuration * 1e-6); // s
196 ir = float(vScaler.back() - vScaler.front()) / duration;
197 LOGP(info, "run {}: orbit.front = {} orbit.back = {} duration = {} s scalers = {} IR = {} Hz", run, vOrbit.front(), vOrbit.back(), duration, vScaler.back() - vScaler.front(), ir);
198 }
199
200 if (ir < 100000) {
201 LOGP(info, "IR < 100 kHz");
202 } else {
203 LOGP(info, "IR > 100 kHz");
204 }
206 writeDurationToFile(duration);
207
208 return 0;
209}
definition of CTPConfiguration and related CTP structures
int32_t i
Header of the AggregatedRunInfo struct.
Header of the General Run Parameters object for B field values.
definition of CTPScalerRaw, CTPScalerO2
Header to collect definitions for different units.
std::ostringstream debug
static BasicCCDBManager & instance()
std::vector< CTPScalerRecordO2 > & getScalerRecordO2()
Definition Scalers.h:106
static constexpr const char * getName(ID id)
names of defined detectors
Definition DetID.h:145
static constexpr ID First
Definition DetID.h:94
static constexpr int nDetectors
number of defined detectors
Definition DetID.h:96
bool isDetReadOut(DetID id) const
test if detector is read out
o2::units::Current_t getL3Current() const
getters/setters for magnets currents
Definition GRPMagField.h:37
void writeDurationToFile(long duration)
const double orbitDuration
void writeBFieldToFile(float b)
bool initOptionsAndParse(bpo::options_description &options, int argc, char *argv[], bpo::variables_map &vm)
void writeIRtoFile(float ir)
void writeDetListToFile(std::string detList)
GLdouble n
Definition glcorearb.h:1982
GLboolean GLboolean GLboolean b
Definition glcorearb.h:1233
std::string to_string(gsl::span< T, Size > span)
Definition common.h:52
uint32_t orbit
LHC orbit.
#define main
o2::InteractionRecord ir(0, 0)