Project
Loading...
Searching...
No Matches
LHCClockCalibratorSpec.h
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#ifndef O2_CALIBRATION_LHCCLOCK_CALIBRATOR_H
13#define O2_CALIBRATION_LHCCLOCK_CALIBRATOR_H
14
17
22#include "Framework/Task.h"
27#include "CCDB/CcdbApi.h"
28#include "CCDB/CcdbObjectInfo.h"
31
32#include <TSystem.h>
33
34using namespace o2::framework;
35
36namespace o2
37{
38namespace calibration
39{
40
42{
45
46 public:
47 LHCClockCalibDevice(std::shared_ptr<o2::base::GRPGeomRequest> req, bool useCCDB) : mCCDBRequest(req), mUseCCDB(useCCDB) {}
48
50 {
52 int minEnt = std::max(300, ic.options().get<int>("min-entries"));
53 int nb = std::max(500, ic.options().get<int>("nbins"));
54 auto slotL = ic.options().get<uint32_t>("tf-per-slot");
55 auto delay = ic.options().get<uint32_t>("max-delay");
56 mCalibrator = std::make_unique<o2::tof::LHCClockCalibrator>(minEnt, nb);
57 mCalibrator->setSlotLength(slotL);
58 mCalibrator->setMaxSlotsDelay(delay);
59
60 if (!mUseCCDB) {
61 // calibration objects set to zero
62 mPhase.addLHCphase(0, 0);
64 if (gSystem->AccessPathName("localTimeSlewing.root") == false) {
65 TFile* fsleewing = TFile::Open("localTimeSlewing.root");
66 if (fsleewing) {
67 TimeSlewing* ob = (TimeSlewing*)fsleewing->Get("ccdb_object");
68 mTimeSlewing = *ob;
69 }
70 } else {
71 for (int ich = 0; ich < TimeSlewing::NCHANNELS; ich++) {
72 mTimeSlewing.addTimeSlewingInfo(ich, 0, 0);
73 int sector = ich / TimeSlewing::NCHANNELXSECTOR;
74 int channelInSector = ich % TimeSlewing::NCHANNELXSECTOR;
75 mTimeSlewing.setFractionUnderPeak(sector, channelInSector, 1);
76 }
77 }
78 }
79 }
80
82 {
83
84 LOG(debug) << "We are running LHCPhase";
86 auto data = pc.inputs().get<gsl::span<o2::dataformats::CalibInfoTOF>>("input");
87 o2::base::TFIDInfoHelper::fillTFIDInfo(pc, mCalibrator->getCurrentTFInfo());
88
89 if (mUseCCDB) { // read calibration objects from ccdb with the CCDB fetcher
90 const auto lhcPhaseIn = pc.inputs().get<LHCphase*>("tofccdbLHCphase");
91 const auto channelCalibIn = pc.inputs().get<TimeSlewing*>("tofccdbChannelCalib");
92 if (!mcalibTOFapi) {
93 LHCphase* lhcPhase = new LHCphase(std::move(*lhcPhaseIn));
94 TimeSlewing* channelCalib = new TimeSlewing(std::move(*channelCalibIn));
95 mcalibTOFapi = new o2::tof::CalibTOFapi(long(0), lhcPhase, channelCalib);
96 mUpdateCCDB = false;
97 } else {
98 // if the calib objects were updated, we need to update the mcalibTOFapi
99 if (mUpdateCCDB) {
100 delete mcalibTOFapi;
101 LHCphase* lhcPhase = new LHCphase(*lhcPhaseIn);
102 TimeSlewing* channelCalib = new TimeSlewing(std::move(*channelCalibIn));
103 mcalibTOFapi = new o2::tof::CalibTOFapi(long(0), lhcPhase, channelCalib);
104 mUpdateCCDB = false;
105 }
106 }
107 } else { // we use "fake" initial calibrations
108 if (!mcalibTOFapi) {
109 mcalibTOFapi = new o2::tof::CalibTOFapi(long(0), &mPhase, &mTimeSlewing);
110 }
111 }
112
113 mCalibrator->setCalibTOFapi(mcalibTOFapi);
114 LOG(debug) << "Data size = " << data.size();
115 if (data.size() == 0) {
116 return;
117 }
118
119 if (mUseCCDB) { // setting the timestamp to get the LHCPhase correction; if we don't use CCDB, then it can stay to 0 as set when creating the calibTOFapi above
120 const auto tfOrbitFirst = pc.services().get<o2::framework::TimingInfo>().firstTForbit;
121 mcalibTOFapi->setTimeStamp(0.001 * (o2::base::GRPGeomHelper::instance().getOrbitResetTimeMS() + tfOrbitFirst * o2::constants::lhc::LHCOrbitMUS * 0.001)); // in seconds
122 }
123
124 LOG(debug) << "Processing TF " << mCalibrator->getCurrentTFInfo().tfCounter << " with " << data.size() << " tracks";
125 mCalibrator->process(data);
126 sendOutput(pc.outputs());
127 const auto& infoVec = mCalibrator->getLHCphaseInfoVector();
128 LOG(info) << "Processed TF " << mCalibrator->getCurrentTFInfo().tfCounter << " with " << data.size() << " tracks, for which we created " << infoVec.size() << " objects";
129 }
130
131 void finaliseCCDB(o2::framework::ConcreteDataMatcher& matcher, void* obj) final
132 {
134 mUpdateCCDB = false;
135 if (matcher == ConcreteDataMatcher("TOF", "LHCphaseCal", 0)) {
136 mUpdateCCDB = true;
137 return;
138 }
139 if (matcher == ConcreteDataMatcher("TOF", "ChannelCalibCal", 0)) {
140 mUpdateCCDB = true;
141 return;
142 }
143 }
144
146 {
147 LOG(info) << "Finalizing calibration";
148 mCalibrator->checkSlotsToFinalize(o2::calibration::INFINITE_TF);
149 sendOutput(ec.outputs());
150 }
151
152 private:
153 o2::tof::CalibTOFapi* mcalibTOFapi = nullptr;
154 LHCphase mPhase;
155 TimeSlewing mTimeSlewing;
156 std::unique_ptr<o2::tof::LHCClockCalibrator> mCalibrator;
157 std::shared_ptr<o2::base::GRPGeomRequest> mCCDBRequest;
158 bool mUpdateCCDB = false;
159 bool mUseCCDB = true;
160
161 //________________________________________________________________
162 void sendOutput(DataAllocator& output)
163 {
164 // extract CCDB infos and calibration objects, convert it to TMemFile and send them to the output
165 // TODO in principle, this routine is generic, can be moved to Utils.h
167 const auto& payloadVec = mCalibrator->getLHCphaseVector();
168 auto& infoVec = mCalibrator->getLHCphaseInfoVector(); // use non-const version as we update it
169 assert(payloadVec.size() == infoVec.size());
170
171 for (uint32_t i = 0; i < payloadVec.size(); i++) {
172 auto& w = infoVec[i];
173 auto image = o2::ccdb::CcdbApi::createObjectImage(&payloadVec[i], &w);
174 LOG(info) << "Sending object " << w.getPath() << "/" << w.getFileName() << " of size " << image->size()
175 << " bytes, valid for " << w.getStartValidityTimestamp() << " : " << w.getEndValidityTimestamp();
176 output.snapshot(Output{o2::calibration::Utils::gDataOriginCDBPayload, "TOF_LHCphase", i}, *image.get()); // vector<char>
177 output.snapshot(Output{o2::calibration::Utils::gDataOriginCDBWrapper, "TOF_LHCphase", i}, w); // root-serialized
178 }
179 if (payloadVec.size()) {
180 mCalibrator->initOutput(); // reset the outputs once they are already sent
181 }
182 }
183};
184
185} // namespace calibration
186
187namespace framework
188{
189
190DataProcessorSpec getLHCClockCalibDeviceSpec(bool useCCDB)
191{
194 std::vector<InputSpec> inputs{{"input", "TOF", "CALIBDATA"}};
195 auto ccdbRequest = std::make_shared<o2::base::GRPGeomRequest>(true, // orbitResetTime
196 true, // GRPECS=true
197 false, // GRPLHCIF
198 false, // GRPMagField
199 false, // askMatLUT
201 inputs);
202
203 if (useCCDB) {
204 inputs.emplace_back("tofccdbLHCphase", "TOF", "LHCphaseCal", 0, Lifetime::Condition, ccdbParamSpec("TOF/Calib/LHCphase"));
205 inputs.emplace_back("tofccdbChannelCalib", "TOF", "ChannelCalibCal", 0, Lifetime::Condition, ccdbParamSpec("TOF/Calib/ChannelCalib"));
206 inputs.emplace_back("orbitResetTOF", o2::header::gDataOriginCTP, "ORBITRESETTOF", 0, Lifetime::Condition, ccdbParamSpec("CTP/Calib/OrbitReset"));
207 }
208
209 std::vector<OutputSpec> outputs;
210 outputs.emplace_back(ConcreteDataTypeMatcher{o2::calibration::Utils::gDataOriginCDBPayload, "TOF_LHCphase"}, Lifetime::Sporadic);
211 outputs.emplace_back(ConcreteDataTypeMatcher{o2::calibration::Utils::gDataOriginCDBWrapper, "TOF_LHCphase"}, Lifetime::Sporadic);
212 return DataProcessorSpec{
213 "calib-lhcclock-calibration",
214 inputs,
215 outputs,
216 AlgorithmSpec{adaptFromTask<device>(ccdbRequest, useCCDB)},
217 Options{
218 {"tf-per-slot", VariantType::UInt32, 5u, {"number of TFs per calibration time slot"}},
219 {"max-delay", VariantType::UInt32, 3u, {"number of slots in past to consider"}},
220 {"min-entries", VariantType::Int, 500, {"minimum number of entries to fit single time slot"}},
221 {"nbins", VariantType::Int, 4000, {"number of bins for "}}}};
222}
223
224} // namespace framework
225} // namespace o2
226
227#endif
Class to store the output of the matching to TOF for calibration.
Utils and constants for calibration and related workflows.
int32_t i
Helper for geometry and GRP related CCDB requests.
void output(const std::map< std::string, ChannelStat > &channels)
Definition rawdump.cxx:197
std::ostringstream debug
void checkUpdates(o2::framework::ProcessingContext &pc)
bool finaliseCCDB(o2::framework::ConcreteDataMatcher &matcher, void *obj)
static GRPGeomHelper & instance()
void setRequest(std::shared_ptr< GRPGeomRequest > req)
void endOfStream(o2::framework::EndOfStreamContext &ec) final
This is invoked whenever we have an EndOfStream event.
LHCClockCalibDevice(std::shared_ptr< o2::base::GRPGeomRequest > req, bool useCCDB)
void init(o2::framework::InitContext &ic) final
void run(o2::framework::ProcessingContext &pc) final
void finaliseCCDB(o2::framework::ConcreteDataMatcher &matcher, void *obj) final
static std::unique_ptr< std::vector< char > > createObjectImage(const T *obj, CcdbObjectInfo *info=nullptr)
Definition CcdbApi.h:103
static constexpr long INFINITE_TIMESTAMP_SECONDS
void addLHCphase(int timestamp, float phaseLHC)
void addTimeSlewingInfo(int channel, float tot, float time)
void setFractionUnderPeak(int sector, int channel, float value)
void setTimeStamp(long t)
Definition CalibTOFapi.h:58
GLeglImageOES image
Definition glcorearb.h:4021
GLboolean * data
Definition glcorearb.h:298
GLubyte GLubyte GLubyte GLubyte w
Definition glcorearb.h:852
constexpr o2::header::DataOrigin gDataOriginCTP
Definition DataHeader.h:564
constexpr TFType INFINITE_TF
Definition TimeSlot.h:30
constexpr double LHCOrbitMUS
Defining PrimaryVertex explicitly as messageable.
Definition TFIDInfo.h:20
std::vector< ConfigParamSpec > ccdbParamSpec(std::string const &path, int runDependent, std::vector< CCDBMetadata > metadata={}, int qrate=0)
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
static void fillTFIDInfo(o2::framework::ProcessingContext &pc, o2::dataformats::TFIDInfo &ti)
static constexpr o2::header::DataOrigin gDataOriginCDBWrapper
Definition Utils.h:44
static constexpr o2::header::DataOrigin gDataOriginCDBPayload
Definition Utils.h:43
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"