Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-12-22 01:53:11

0001 /****************************************************************************
0002  *
0003  * This is a part of CTPPS offline software.
0004  * Authors:
0005  *   Edoardo Bossini
0006  *   Filip Dej
0007  *   Laurent Forthomme
0008  *   Christopher Misan
0009  *
0010  * NOTE:
0011  *   Given implementation handles calibration files in JSON format,
0012  *
0013  ****************************************************************************/
0014 
0015 #include "FWCore/Framework/interface/MakerMacros.h"
0016 #include "FWCore/Framework/interface/SourceFactory.h"
0017 #include "FWCore/Framework/interface/ESHandle.h"
0018 #include "FWCore/Framework/interface/ESProducer.h"
0019 #include "FWCore/Framework/interface/EventSetupRecordIntervalFinder.h"
0020 #include "FWCore/Framework/interface/ESProducts.h"
0021 
0022 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0023 
0024 #include "CondFormats/PPSObjects/interface/PPSTimingCalibrationLUT.h"
0025 #include "CondFormats/DataRecord/interface/PPSTimingCalibrationLUTRcd.h"
0026 
0027 #include <boost/property_tree/json_parser.hpp>
0028 #include <boost/property_tree/ptree.hpp>
0029 
0030 namespace pt = boost::property_tree;
0031 
0032 //------------------------------------------------------------------------------
0033 
0034 class PPSTimingCalibrationLUTESSource : public edm::ESProducer, public edm::EventSetupRecordIntervalFinder {
0035 public:
0036   PPSTimingCalibrationLUTESSource(const edm::ParameterSet&);
0037 
0038   edm::ESProducts<std::unique_ptr<PPSTimingCalibrationLUT> > produce(const PPSTimingCalibrationLUTRcd&);
0039 
0040   static void fillDescriptions(edm::ConfigurationDescriptions&);
0041 
0042 private:
0043   void setIntervalFor(const edm::eventsetup::EventSetupRecordKey&,
0044                       const edm::IOVSyncValue&,
0045                       edm::ValidityInterval&) override;
0046 
0047   /// Extract calibration data from JSON file (PPS horizontal diamond)
0048   std::unique_ptr<PPSTimingCalibrationLUT> parsePPSDiamondLUTJsonFile() const;
0049 
0050   const std::string filename_;
0051 };
0052 
0053 //------------------------------------------------------------------------------
0054 
0055 PPSTimingCalibrationLUTESSource::PPSTimingCalibrationLUTESSource(const edm::ParameterSet& iConfig)
0056     : filename_(iConfig.getParameter<edm::FileInPath>("calibrationFile").fullPath()) {
0057   setWhatProduced(this);
0058   findingRecord<PPSTimingCalibrationLUTRcd>();
0059 }
0060 
0061 //------------------------------------------------------------------------------
0062 
0063 edm::ESProducts<std::unique_ptr<PPSTimingCalibrationLUT> > PPSTimingCalibrationLUTESSource::produce(
0064     const PPSTimingCalibrationLUTRcd&) {
0065   return edm::es::products(parsePPSDiamondLUTJsonFile());
0066 }
0067 
0068 //------------------------------------------------------------------------------
0069 
0070 void PPSTimingCalibrationLUTESSource::setIntervalFor(const edm::eventsetup::EventSetupRecordKey&,
0071                                                      const edm::IOVSyncValue&,
0072                                                      edm::ValidityInterval& oValidity) {
0073   oValidity = edm::ValidityInterval(edm::IOVSyncValue::beginOfTime(), edm::IOVSyncValue::endOfTime());
0074 }
0075 
0076 //------------------------------------------------------------------------------
0077 
0078 std::unique_ptr<PPSTimingCalibrationLUT> PPSTimingCalibrationLUTESSource::parsePPSDiamondLUTJsonFile() const {
0079   pt::ptree mother_node;
0080   pt::read_json(filename_, mother_node);
0081 
0082   PPSTimingCalibrationLUT::BinMap binMap;
0083   for (pt::ptree::value_type& node : mother_node.get_child("calib")) {
0084     PPSTimingCalibrationLUT::Key key;
0085 
0086     key.sector = node.second.get<int>("sector");
0087     key.station = node.second.get<int>("station");
0088     key.plane = node.second.get<int>("plane");
0089     key.channel = node.second.get<int>("channel");
0090     std::vector<double> values;
0091     for (pt::ptree::value_type& sample : node.second.get_child("samples")) {
0092       values.emplace_back(std::stod(sample.second.data(), nullptr));
0093       binMap[key] = values;
0094     }
0095   }
0096   return std::make_unique<PPSTimingCalibrationLUT>(binMap);
0097 }
0098 
0099 void PPSTimingCalibrationLUTESSource::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0100   edm::ParameterSetDescription desc;
0101   desc.add<edm::FileInPath>("calibrationFile", edm::FileInPath())->setComment("file with calibrations");
0102 
0103   descriptions.add("ppsTimingCalibrationLUTESSource", desc);
0104 }
0105 
0106 DEFINE_FWK_EVENTSETUP_SOURCE(PPSTimingCalibrationLUTESSource);