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