|
| 1 | +/**************************************************************************** |
| 2 | + * |
| 3 | + * This is a part of CTPPS offline software. |
| 4 | + * Authors: |
| 5 | + * Edoardo Bossini |
| 6 | + * Filip Dej |
| 7 | + * Laurent Forthomme |
| 8 | + * Christopher Misan |
| 9 | + * |
| 10 | + * NOTE: |
| 11 | + * Given implementation handles calibration files in JSON format, |
| 12 | + * |
| 13 | + ****************************************************************************/ |
| 14 | + |
| 15 | +#include "FWCore/Framework/interface/MakerMacros.h" |
| 16 | +#include "FWCore/Framework/interface/SourceFactory.h" |
| 17 | +#include "FWCore/Framework/interface/ESHandle.h" |
| 18 | +#include "FWCore/Framework/interface/ESProducer.h" |
| 19 | +#include "FWCore/Framework/interface/EventSetupRecordIntervalFinder.h" |
| 20 | +#include "FWCore/Framework/interface/ESProducts.h" |
| 21 | + |
| 22 | +#include "FWCore/ParameterSet/interface/ParameterSet.h" |
| 23 | + |
| 24 | +#include "CondFormats/PPSObjects/interface/PPSTimingCalibrationLUT.h" |
| 25 | +#include "CondFormats/DataRecord/interface/PPSTimingCalibrationLUTRcd.h" |
| 26 | + |
| 27 | +#include <boost/property_tree/json_parser.hpp> |
| 28 | +#include <boost/property_tree/ptree.hpp> |
| 29 | + |
| 30 | +namespace pt = boost::property_tree; |
| 31 | + |
| 32 | +//------------------------------------------------------------------------------ |
| 33 | + |
| 34 | +class PPSTimingCalibrationLUTESSource : public edm::ESProducer, public edm::EventSetupRecordIntervalFinder { |
| 35 | +public: |
| 36 | + PPSTimingCalibrationLUTESSource(const edm::ParameterSet&); |
| 37 | + |
| 38 | + edm::ESProducts<std::unique_ptr<PPSTimingCalibrationLUT> > produce(const PPSTimingCalibrationLUTRcd&); |
| 39 | + |
| 40 | + static void fillDescriptions(edm::ConfigurationDescriptions&); |
| 41 | + |
| 42 | +private: |
| 43 | + void setIntervalFor(const edm::eventsetup::EventSetupRecordKey&, |
| 44 | + const edm::IOVSyncValue&, |
| 45 | + edm::ValidityInterval&) override; |
| 46 | + |
| 47 | + /// Extract calibration data from JSON file (PPS horizontal diamond) |
| 48 | + std::unique_ptr<PPSTimingCalibrationLUT> parsePPSDiamondLUTJsonFile() const; |
| 49 | + |
| 50 | + const std::string filename_; |
| 51 | +}; |
| 52 | + |
| 53 | +//------------------------------------------------------------------------------ |
| 54 | + |
| 55 | +PPSTimingCalibrationLUTESSource::PPSTimingCalibrationLUTESSource(const edm::ParameterSet& iConfig) |
| 56 | + : filename_(iConfig.getParameter<edm::FileInPath>("calibrationFile").fullPath()) { |
| 57 | + setWhatProduced(this); |
| 58 | + findingRecord<PPSTimingCalibrationLUTRcd>(); |
| 59 | +} |
| 60 | + |
| 61 | +//------------------------------------------------------------------------------ |
| 62 | + |
| 63 | +edm::ESProducts<std::unique_ptr<PPSTimingCalibrationLUT> > PPSTimingCalibrationLUTESSource::produce( |
| 64 | + const PPSTimingCalibrationLUTRcd&) { |
| 65 | + return edm::es::products(parsePPSDiamondLUTJsonFile()); |
| 66 | +} |
| 67 | + |
| 68 | +//------------------------------------------------------------------------------ |
| 69 | + |
| 70 | +void PPSTimingCalibrationLUTESSource::setIntervalFor(const edm::eventsetup::EventSetupRecordKey&, |
| 71 | + const edm::IOVSyncValue&, |
| 72 | + edm::ValidityInterval& oValidity) { |
| 73 | + oValidity = edm::ValidityInterval(edm::IOVSyncValue::beginOfTime(), edm::IOVSyncValue::endOfTime()); |
| 74 | +} |
| 75 | + |
| 76 | +//------------------------------------------------------------------------------ |
| 77 | + |
| 78 | +std::unique_ptr<PPSTimingCalibrationLUT> PPSTimingCalibrationLUTESSource::parsePPSDiamondLUTJsonFile() const { |
| 79 | + pt::ptree mother_node; |
| 80 | + pt::read_json(filename_, mother_node); |
| 81 | + |
| 82 | + PPSTimingCalibrationLUT::BinMap binMap; |
| 83 | + for (pt::ptree::value_type& node : mother_node.get_child("calib")) { |
| 84 | + PPSTimingCalibrationLUT::Key key; |
| 85 | + |
| 86 | + key.sector = node.second.get<int>("sector"); |
| 87 | + key.station = node.second.get<int>("station"); |
| 88 | + key.plane = node.second.get<int>("plane"); |
| 89 | + key.channel = node.second.get<int>("channel"); |
| 90 | + std::vector<double> values; |
| 91 | + for (pt::ptree::value_type& sample : node.second.get_child("samples")) { |
| 92 | + values.emplace_back(std::stod(sample.second.data(), nullptr)); |
| 93 | + binMap[key] = values; |
| 94 | + } |
| 95 | + } |
| 96 | + return std::make_unique<PPSTimingCalibrationLUT>(binMap); |
| 97 | +} |
| 98 | + |
| 99 | +void PPSTimingCalibrationLUTESSource::fillDescriptions(edm::ConfigurationDescriptions& descriptions) { |
| 100 | + edm::ParameterSetDescription desc; |
| 101 | + desc.add<edm::FileInPath>("calibrationFile", edm::FileInPath())->setComment("file with calibrations"); |
| 102 | + |
| 103 | + descriptions.add("ppsTimingCalibrationLUTESSource", desc); |
| 104 | +} |
| 105 | + |
| 106 | +DEFINE_FWK_EVENTSETUP_SOURCE(PPSTimingCalibrationLUTESSource); |
0 commit comments