|
| 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 | +/// \file AggregatedRunInfo.cxx |
| 13 | +/// \author sandro.wenzel@cern.ch |
| 14 | + |
| 15 | +#include "DataFormatsParameters/AggregatedRunInfo.h" |
| 16 | +#include "CCDB/BasicCCDBManager.h" |
| 17 | +#include "DataFormatsParameters/GRPECSObject.h" |
| 18 | +#include "CommonConstants/LHCConstants.h" |
| 19 | +#include "Framework/Logger.h" |
| 20 | + |
| 21 | +using namespace o2::parameters; |
| 22 | + |
| 23 | +o2::parameters::AggregatedRunInfo AggregatedRunInfo::buildAggregatedRunInfo(o2::ccdb::CCDBManagerInstance& ccdb, int runnumber) |
| 24 | +{ |
| 25 | + // TODO: could think about caching results per runnumber to |
| 26 | + // avoid going to CCDB multiple times ---> but should be done inside the CCDBManagerInstance |
| 27 | + |
| 28 | + // we calculate the first orbit of a run based on sor (start-of-run) and eor |
| 29 | + // we obtain these by calling getRunDuration |
| 30 | + auto [sor, eor] = ccdb.getRunDuration(runnumber); |
| 31 | + |
| 32 | + // determine a good timestamp to query OrbitReset for this run |
| 33 | + // --> the middle of the run is very appropriate and safer than just sor |
| 34 | + auto run_mid_timestamp = sor + (eor - sor) / 2; |
| 35 | + |
| 36 | + // query the time of the orbit reset (when orbit is defined to be 0) |
| 37 | + auto ctpx = ccdb.getForTimeStamp<std::vector<Long64_t>>("CTP/Calib/OrbitReset", run_mid_timestamp); |
| 38 | + int64_t tsOrbitReset = (*ctpx)[0]; // us |
| 39 | + |
| 40 | + // get timeframe length from GRPECS |
| 41 | + std::map<std::string, std::string> metadata; |
| 42 | + metadata["runNumber"] = Form("%d", runnumber); |
| 43 | + auto grpecs = ccdb.getSpecific<o2::parameters::GRPECSObject>("GLO/Config/GRPECS", run_mid_timestamp, metadata); |
| 44 | + auto nOrbitsPerTF = grpecs->getNHBFPerTF(); |
| 45 | + |
| 46 | + // calculate SOR orbit |
| 47 | + int64_t orbitSOR = (sor * 1000 - tsOrbitReset) / o2::constants::lhc::LHCOrbitMUS; |
| 48 | + int64_t orbitEOR = (eor * 1000 - tsOrbitReset) / o2::constants::lhc::LHCOrbitMUS; |
| 49 | + |
| 50 | + // adjust to the nearest TF edge to satisfy condition (orbitSOR % nOrbitsPerTF == 0) |
| 51 | + orbitSOR = (orbitSOR / nOrbitsPerTF + 1) * nOrbitsPerTF; // +1 to choose the safe boundary ... towards run middle |
| 52 | + orbitEOR = orbitEOR / nOrbitsPerTF * nOrbitsPerTF; |
| 53 | + |
| 54 | + // fetch SOR directly from CTP entry on CCDB |
| 55 | + bool oldFatalState = ccdb.getFatalWhenNull(); |
| 56 | + ccdb.setFatalWhenNull(false); |
| 57 | + auto ctp_first_run_orbit = ccdb.getForTimeStamp<std::vector<int64_t>>("CTP/Calib/FirstRunOrbit", run_mid_timestamp); |
| 58 | + ccdb.setFatalWhenNull(oldFatalState); |
| 59 | + if (ctp_first_run_orbit && ctp_first_run_orbit->size() >= 3) { |
| 60 | + // if we have CTP first run orbit available, we should use it |
| 61 | + |
| 62 | + // int64_t creation_time = (*ctp_first_run_orbit)[0]; |
| 63 | + int64_t ctp_run_number = (*ctp_first_run_orbit)[1]; |
| 64 | + int64_t ctp_orbitSOR = (*ctp_first_run_orbit)[2]; |
| 65 | + |
| 66 | + if (ctp_run_number != runnumber) { |
| 67 | + LOG(error) << "AggregatedRunInfo: run number inconsistency found (asked: " << runnumber << " vs CTP found: " << ctp_run_number << ")"; |
| 68 | + } |
| 69 | + |
| 70 | + // overwrite orbitSOR |
| 71 | + if (ctp_orbitSOR != orbitSOR) { |
| 72 | + LOG(warn) << "The calculated orbitSOR " << orbitSOR << " differs from CTP orbitSOR " << ctp_orbitSOR; |
| 73 | + // reasons for this is different unit of time storage in RunInformation (ms) and orbitReset (us), etc. |
| 74 | + |
| 75 | + // so we need to adjust the SOR timings to be consistent |
| 76 | + auto sor_new = (int64_t)((tsOrbitReset + ctp_orbitSOR * o2::constants::lhc::LHCOrbitMUS) / 1000.); |
| 77 | + if (sor_new != sor) { |
| 78 | + LOG(warn) << "Adjusting SOR from " << sor << " to " << sor_new; |
| 79 | + sor = sor_new; |
| 80 | + } |
| 81 | + } |
| 82 | + orbitSOR = ctp_orbitSOR; |
| 83 | + } |
| 84 | + |
| 85 | + return AggregatedRunInfo{runnumber, sor, eor, nOrbitsPerTF, tsOrbitReset, orbitSOR, orbitEOR}; |
| 86 | +} |
0 commit comments