Project
Loading...
Searching...
No Matches
PHOSRunbyrunCalibDevice.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
15#include "CCDB/CcdbApi.h"
16#include "CCDB/CcdbObjectInfo.h"
19
20#include <fairlogger/Logger.h>
21#include <filesystem>
22
23using namespace o2::phos;
24
26{
28 mCalibrator.reset(new PHOSRunbyrunCalibrator());
29
30 if (mOutputDir.compare("/dev/null")) {
31 mOutputDir = o2::utils::Str::rectifyDirectory(mOutputDir);
32 }
33 if (mMetaFileDir.compare("/dev/null")) {
34 mMetaFileDir = o2::utils::Str::rectifyDirectory(mMetaFileDir);
35 }
36
37 // mCalibrator->setSlotLength(slotL);
38 // mCalibrator->setMaxSlotsDelay(delay);
39 mCalibrator->setUpdateAtTheEndOfRunOnly();
40}
41
43{
44
46 auto crTime = pc.services().get<o2::framework::TimingInfo>().creation;
47 if (mRunStartTime == 0 || crTime < mRunStartTime) {
48 mRunStartTime = crTime;
49 if (mWriteRootOutput) {
50 mHistoFileMetaData = std::make_unique<o2::dataformats::FileMetaData>();
51 mHistoFileMetaData->setDataTakingContext(pc.services().get<DataTakingContext>());
52 }
53 }
54 auto tfcounter = o2::header::get<o2::header::DataHeader*>(pc.inputs().get("clusters").header)->tfCounter;
55 auto clusters = pc.inputs().get<gsl::span<Cluster>>("clusters");
56 auto cluTR = pc.inputs().get<gsl::span<TriggerRecord>>("cluTR");
57 o2::base::TFIDInfoHelper::fillTFIDInfo(pc, mCalibrator->getCurrentTFInfo());
58 LOG(detail) << "Processing TF with " << clusters.size() << " clusters and " << cluTR.size() << " TriggerRecords";
59 mCalibrator->process(tfcounter, clusters, cluTR);
60}
61
63{
64 mCalibrator->checkSlotsToFinalize(o2::calibration::INFINITE_TF);
65 if (mWriteRootOutput) {
66 std::string runNumber = mHistoFileMetaData->run;
67 std::string histoFileName = mOutputDir + fmt::format("PHOS_RunByRunCalib_{}.root", runNumber);
68 TFile histoFileOut(histoFileName.c_str(), "recreate");
69 mCalibrator->writeHistos();
70 histoFileOut.Close();
71
72 // write metaFile data
73 if (mMetaFileDir.compare("/dev/null")) {
74 mHistoFileMetaData->fillFileData(histoFileName);
75 mHistoFileMetaData->type = "calib";
76 mHistoFileMetaData->priority = "high";
77
78 std::string metaFileNameTmp = mMetaFileDir + fmt::format("PHOS_RunByRunCalib_{}.tmp", runNumber);
79 std::string metaFileName = mMetaFileDir + fmt::format("PHOS_RunByRunCalib_{}.done", runNumber);
80 try {
81 std::ofstream metaFileOut(metaFileNameTmp);
82 metaFileOut << *mHistoFileMetaData.get();
83 metaFileOut.close();
84 std::filesystem::rename(metaFileNameTmp, metaFileName);
85 } catch (std::exception const& e) {
86 LOG(error) << "Failed to store PHOS meta data file " << metaFileName << ", reason: " << e.what();
87 }
88 LOG(info) << "Stored metadate file " << metaFileName << ".done";
89 } else {
90 LOG(info) << "Did not store metafile as meta-dir=" << mMetaFileDir;
91 }
92 }
93 mRunByRun = mCalibrator->getCalibration();
94 if (checkFitResult()) {
95 LOG(info) << "End of stream reached, sending output to CCDB";
96 // prepare all info to be sent to CCDB
97 auto flName = o2::ccdb::CcdbApi::generateFileName("Runbyrun");
98 std::map<std::string, std::string> md;
99 o2::ccdb::CcdbObjectInfo info("PHS/Calib/Runbyrun", "Runbyrun", flName, md, mRunStartTime - o2::ccdb::CcdbObjectInfo::MINUTE, mRunStartTime + o2::ccdb::CcdbObjectInfo::YEAR);
100 info.setMetaData(md);
101 auto image = o2::ccdb::CcdbApi::createObjectImage(&mRunByRun, &info);
102
103 LOG(info) << "Sending object " << info.getPath() << "/" << info.getFileName()
104 << " of size " << image->size()
105 << " bytes, valid for " << info.getStartValidityTimestamp()
106 << " : " << info.getEndValidityTimestamp();
107
110 } else {
111 LOG(alarm) << "Incorrect fit results: " << mRunByRun[0] << "+-" << mRunByRun[1] << ", "
112 << mRunByRun[2] << "+-" << mRunByRun[3] << ", "
113 << mRunByRun[4] << "+-" << mRunByRun[5] << ", "
114 << mRunByRun[6] << "+-" << mRunByRun[7];
115 }
116 // TODO! Send mRunByRun for QC and trending plots
117 //
118
119 // Get ready for next run
120 mCalibrator->initOutput(); // reset the outputs once they are already sent
121}
123{
124 bool res = true;
125 const float massmin = 0.125;
126 const float massmax = 0.155;
127 for (int mod = 0; mod < 4; mod++) {
128 res &= mRunByRun[2 * mod] < massmax && mRunByRun[2 * mod] > massmin;
129 }
130 return res;
131}
132
133o2::framework::DataProcessorSpec o2::phos::getPHOSRunbyrunCalibDeviceSpec(bool useCCDB, const std::string& outputDir, const std::string& metaFileDir, bool writeRootOutput)
134{
135
136 std::vector<OutputSpec> outputs;
137 outputs.emplace_back(o2::calibration::Utils::gDataOriginCDBPayload, "PHOS_Runbyrun", 0, Lifetime::Sporadic);
138 outputs.emplace_back(o2::calibration::Utils::gDataOriginCDBWrapper, "PHOS_Runbyrun", 0, Lifetime::Sporadic);
139
140 std::vector<InputSpec> inputs;
141 inputs.emplace_back("clusters", "PHS", "CLUSTERS");
142 inputs.emplace_back("cluTR", "PHS", "CLUSTERTRIGREC");
143 auto ccdbRequest = std::make_shared<o2::base::GRPGeomRequest>(true, // orbitResetTime
144 true, // GRPECS=true
145 false, // GRPLHCIF
146 false, // GRPMagField
147 false, // askMatLUT
149 inputs);
150 return DataProcessorSpec{
151 "calib-phos-runbyrun",
152 inputs,
153 outputs,
154 AlgorithmSpec{adaptFromTask<PHOSRunbyrunCalibDevice>(ccdbRequest, outputDir, metaFileDir, writeRootOutput)},
155 Options{}};
156}
Utils and constants for calibration and related workflows.
Device to calculate PHOS time shift (L1phase)
uint32_t res
Definition RawData.h:0
void checkUpdates(o2::framework::ProcessingContext &pc)
static GRPGeomHelper & instance()
void setRequest(std::shared_ptr< GRPGeomRequest > req)
static std::string generateFileName(const std::string &inp)
Definition CcdbApi.cxx:824
static std::unique_ptr< std::vector< char > > createObjectImage(const T *obj, CcdbObjectInfo *info=nullptr)
Definition CcdbApi.h:103
long getEndValidityTimestamp() const
const std::string & getPath() const
void setMetaData(const std::map< std::string, std::string > &md)
long getStartValidityTimestamp() const
const std::string & getFileName() const
static constexpr long YEAR
static constexpr long MINUTE
void snapshot(const Output &spec, T const &object)
decltype(auto) get(R binding, int part=0) const
InputRecord & inputs()
The inputs associated with this processing context.
ServiceRegistryRef services()
The services registry associated with this processing context.
void run(o2::framework::ProcessingContext &pc)
void endOfStream(o2::framework::EndOfStreamContext &ec)
void init(o2::framework::InitContext &ic)
GLeglImageOES image
Definition glcorearb.h:4021
constexpr TFType INFINITE_TF
Definition TimeSlot.h:30
std::vector< ConfigParamSpec > Options
o2::framework::DataProcessorSpec getPHOSRunbyrunCalibDeviceSpec(bool useCCDB, const std::string &outputDir, const std::string &metaFileDir, bool writeRootOutput)
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
static std::string rectifyDirectory(const std::string_view p)
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"
std::vector< Cluster > clusters