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