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 LOG(detail) << "Processing TF with " << clusters.size() << " clusters and " << cluTR.size() << " TriggerRecords";
58 mCalibrator->process(tfcounter, clusters, cluTR);
59}
60
62{
63 mCalibrator->checkSlotsToFinalize(o2::calibration::INFINITE_TF);
64 if (mWriteRootOutput) {
65 std::string runNumber = mHistoFileMetaData->run;
66 std::string histoFileName = mOutputDir + fmt::format("PHOS_RunByRunCalib_{}.root", runNumber);
67 TFile histoFileOut(histoFileName.c_str(), "recreate");
68 mCalibrator->writeHistos();
69 histoFileOut.Close();
70
71 // write metaFile data
72 if (mMetaFileDir.compare("/dev/null")) {
73 mHistoFileMetaData->fillFileData(histoFileName);
74 mHistoFileMetaData->type = "calib";
75 mHistoFileMetaData->priority = "high";
76
77 std::string metaFileNameTmp = mMetaFileDir + fmt::format("PHOS_RunByRunCalib_{}.tmp", runNumber);
78 std::string metaFileName = mMetaFileDir + fmt::format("PHOS_RunByRunCalib_{}.done", runNumber);
79 try {
80 std::ofstream metaFileOut(metaFileNameTmp);
81 metaFileOut << *mHistoFileMetaData.get();
82 metaFileOut.close();
83 std::filesystem::rename(metaFileNameTmp, metaFileName);
84 } catch (std::exception const& e) {
85 LOG(error) << "Failed to store PHOS meta data file " << metaFileName << ", reason: " << e.what();
86 }
87 LOG(info) << "Stored metadate file " << metaFileName << ".done";
88 } else {
89 LOG(info) << "Did not store metafile as meta-dir=" << mMetaFileDir;
90 }
91 }
92 mRunByRun = mCalibrator->getCalibration();
93 if (checkFitResult()) {
94 LOG(info) << "End of stream reached, sending output to CCDB";
95 // prepare all info to be sent to CCDB
96 auto flName = o2::ccdb::CcdbApi::generateFileName("Runbyrun");
97 std::map<std::string, std::string> md;
98 o2::ccdb::CcdbObjectInfo info("PHS/Calib/Runbyrun", "Runbyrun", flName, md, mRunStartTime - o2::ccdb::CcdbObjectInfo::MINUTE, mRunStartTime + o2::ccdb::CcdbObjectInfo::YEAR);
99 info.setMetaData(md);
100 auto image = o2::ccdb::CcdbApi::createObjectImage(&mRunByRun, &info);
101
102 LOG(info) << "Sending object " << info.getPath() << "/" << info.getFileName()
103 << " of size " << image->size()
104 << " bytes, valid for " << info.getStartValidityTimestamp()
105 << " : " << info.getEndValidityTimestamp();
106
109 } else {
110 LOG(alarm) << "Incorrect fit results: " << mRunByRun[0] << "+-" << mRunByRun[1] << ", "
111 << mRunByRun[2] << "+-" << mRunByRun[3] << ", "
112 << mRunByRun[4] << "+-" << mRunByRun[5] << ", "
113 << mRunByRun[6] << "+-" << mRunByRun[7];
114 }
115 // TODO! Send mRunByRun for QC and trending plots
116 //
117
118 // Get ready for next run
119 mCalibrator->initOutput(); // reset the outputs once they are already sent
120}
122{
123 bool res = true;
124 const float massmin = 0.125;
125 const float massmax = 0.155;
126 for (int mod = 0; mod < 4; mod++) {
127 res &= mRunByRun[2 * mod] < massmax && mRunByRun[2 * mod] > massmin;
128 }
129 return res;
130}
131
132o2::framework::DataProcessorSpec o2::phos::getPHOSRunbyrunCalibDeviceSpec(bool useCCDB, const std::string& outputDir, const std::string& metaFileDir, bool writeRootOutput)
133{
134
135 std::vector<OutputSpec> outputs;
136 outputs.emplace_back(o2::calibration::Utils::gDataOriginCDBPayload, "PHOS_Runbyrun", 0, Lifetime::Sporadic);
137 outputs.emplace_back(o2::calibration::Utils::gDataOriginCDBWrapper, "PHOS_Runbyrun", 0, Lifetime::Sporadic);
138
139 std::vector<InputSpec> inputs;
140 inputs.emplace_back("clusters", "PHS", "CLUSTERS");
141 inputs.emplace_back("cluTR", "PHS", "CLUSTERTRIGREC");
142 auto ccdbRequest = std::make_shared<o2::base::GRPGeomRequest>(true, // orbitResetTime
143 true, // GRPECS=true
144 false, // GRPLHCIF
145 false, // GRPMagField
146 false, // askMatLUT
148 inputs);
149 return DataProcessorSpec{
150 "calib-phos-runbyrun",
151 inputs,
152 outputs,
153 AlgorithmSpec{adaptFromTask<PHOSRunbyrunCalibDevice>(ccdbRequest, outputDir, metaFileDir, writeRootOutput)},
154 Options{}};
155}
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:798
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 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