Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:02:42

0001 // -*- C++ -*-
0002 //
0003 // Package:    CondTools/BeamSpot
0004 // Class:      BeamSpotOnlineFromOfflineConverter
0005 //
0006 /**\class BeamSpotOnlineFromOfflineConverter BeamSpotOnlineFromOfflineConverter.cc CondTools/BeamSpot/plugins/BeamSpotOnlineFromOfflineConverter.cc
0007 
0008  Description: EDAnalyzer to create a BeamSpotOnlineHLTObjectsRcd from a BeamSpotObjectsRcd (inserting some parameters manually)
0009 
0010  Implementation:
0011      [Notes on implementation]
0012 */
0013 //
0014 // Original Author:  Marco Musich
0015 //         Created:  Sat, 06 May 2023 21:10:00 GMT
0016 //
0017 //
0018 
0019 // system include files
0020 #include <memory>
0021 #include <string>
0022 #include <fstream>
0023 #include <iostream>
0024 #include <ctime>
0025 
0026 // user include files
0027 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
0028 #include "CondFormats/BeamSpotObjects/interface/BeamSpotObjects.h"
0029 #include "CondFormats/BeamSpotObjects/interface/BeamSpotOnlineObjects.h"
0030 #include "CondFormats/DataRecord/interface/BeamSpotObjectsRcd.h"
0031 #include "CondFormats/DataRecord/interface/BeamSpotOnlineHLTObjectsRcd.h"
0032 #include "CondFormats/DataRecord/interface/BeamSpotOnlineLegacyObjectsRcd.h"
0033 #include "FWCore/Framework/interface/ESHandle.h"
0034 #include "FWCore/Framework/interface/ESWatcher.h"
0035 #include "FWCore/Framework/interface/Event.h"
0036 #include "FWCore/Framework/interface/EventSetup.h"
0037 #include "FWCore/Framework/interface/Frameworkfwd.h"
0038 #include "FWCore/Framework/interface/MakerMacros.h"
0039 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0040 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0041 #include "FWCore/ServiceRegistry/interface/Service.h"
0042 #include "FWCore/Utilities/interface/InputTag.h"
0043 
0044 //
0045 // class declaration
0046 //
0047 
0048 class BeamSpotOnlineFromOfflineConverter : public edm::one::EDAnalyzer<> {
0049 public:
0050   explicit BeamSpotOnlineFromOfflineConverter(const edm::ParameterSet&);
0051   ~BeamSpotOnlineFromOfflineConverter() override = default;
0052 
0053   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0054   cond::Time_t pack(uint32_t, uint32_t);
0055 
0056 private:
0057   void analyze(const edm::Event&, const edm::EventSetup&) override;
0058 
0059   // ----------member data ---------------------------
0060   const edm::ESGetToken<BeamSpotObjects, BeamSpotObjectsRcd> bsToken_;
0061   edm::ESWatcher<BeamSpotObjectsRcd> bsWatcher_;
0062 
0063   // parameters that can't be copied from the BeamSpotObject
0064   const int lastAnalyzedLumi_, lastAnalyzedRun_, lastAnalyzedFill_;
0065   const int numTracks_, numPVs_, numUsedEvents_, numMaxPVs_;
0066   const float meanPVs_, meanPVError_, rmsPV_, rmsPVError_;
0067   const std::string startTime_, endTime_, lumiRange_;
0068 
0069   // IoV-structure
0070   const bool fIsHLT_;
0071   uint32_t fIOVStartRun_;
0072   uint32_t fIOVStartLumi_;
0073   cond::Time_t fnewSince_;
0074   bool fuseNewSince_;
0075   std::string fLabel_;
0076 };
0077 
0078 //
0079 // constructors and destructor
0080 //
0081 BeamSpotOnlineFromOfflineConverter::BeamSpotOnlineFromOfflineConverter(const edm::ParameterSet& iConfig)
0082     : bsToken_(esConsumes()),
0083       lastAnalyzedLumi_(iConfig.getParameter<double>("lastAnalyzedLumi")),
0084       lastAnalyzedRun_(iConfig.getParameter<double>("lastAnalyzedRun")),
0085       lastAnalyzedFill_(iConfig.getParameter<double>("lastAnalyzedFill")),
0086       numTracks_(iConfig.getParameter<int>("numTracks")),
0087       numPVs_(iConfig.getParameter<int>("numPVs")),
0088       numUsedEvents_(iConfig.getParameter<int>("numUsedEvents")),
0089       numMaxPVs_(iConfig.getParameter<int>("numMaxPVs")),
0090       meanPVs_(iConfig.getParameter<double>("meanPVs")),
0091       meanPVError_(iConfig.getParameter<double>("meanPVError")),
0092       rmsPV_(iConfig.getParameter<double>("rmsPVs")),
0093       rmsPVError_(iConfig.getParameter<double>("rmsPVError")),
0094       startTime_(iConfig.getParameter<std::string>("startTime")),
0095       endTime_(iConfig.getParameter<std::string>("endTime")),
0096       lumiRange_(iConfig.getParameter<std::string>("lumiRange")),
0097       fIsHLT_(iConfig.getParameter<bool>("isHLT")) {
0098   if (iConfig.exists("IOVStartRun") && iConfig.exists("IOVStartLumi")) {
0099     fIOVStartRun_ = iConfig.getUntrackedParameter<uint32_t>("IOVStartRun");
0100     fIOVStartLumi_ = iConfig.getUntrackedParameter<uint32_t>("IOVStartLumi");
0101     fnewSince_ = BeamSpotOnlineFromOfflineConverter::pack(fIOVStartRun_, fIOVStartLumi_);
0102     fuseNewSince_ = true;
0103     edm::LogPrint("BeamSpotOnlineFromOfflineConverter") << "useNewSince = True";
0104   } else {
0105     fuseNewSince_ = false;
0106     edm::LogPrint("BeamSpotOnlineFromOfflineConverter") << "useNewSince = False";
0107   }
0108   fLabel_ = (fIsHLT_) ? "BeamSpotOnlineHLTObjectsRcd" : "BeamSpotOnlineLegacyObjectsRcd";
0109 }
0110 
0111 //
0112 // member functions
0113 //
0114 
0115 // ------------ Create a since object (cond::Time_t) by packing Run and LS (both uint32_t)  ------------
0116 cond::Time_t BeamSpotOnlineFromOfflineConverter::pack(uint32_t fIOVStartRun, uint32_t fIOVStartLumi) {
0117   return ((uint64_t)fIOVStartRun << 32 | fIOVStartLumi);
0118 }
0119 
0120 // ------------ method called for each event  ------------
0121 void BeamSpotOnlineFromOfflineConverter::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0122   if (bsWatcher_.check(iSetup)) {
0123     const BeamSpotObjects* inputSpot = &iSetup.getData(bsToken_);
0124 
0125     BeamSpotOnlineObjects abeam;
0126     abeam.copyFromBeamSpotObject(*inputSpot);
0127     abeam.setLastAnalyzedLumi(lastAnalyzedLumi_);
0128     abeam.setLastAnalyzedRun(lastAnalyzedRun_);
0129     abeam.setLastAnalyzedFill(lastAnalyzedFill_);
0130     abeam.setStartTimeStamp(std::time(nullptr));
0131     abeam.setEndTimeStamp(std::time(nullptr));
0132     abeam.setNumTracks(numTracks_);
0133     abeam.setNumPVs(numPVs_);
0134     abeam.setUsedEvents(numUsedEvents_);
0135     abeam.setMaxPVs(numMaxPVs_);
0136     abeam.setMeanPV(meanPVs_);
0137     abeam.setMeanErrorPV(meanPVError_);
0138     abeam.setRmsPV(rmsPV_);
0139     abeam.setRmsErrorPV(rmsPVError_);
0140     abeam.setStartTime(startTime_);
0141     abeam.setEndTime(endTime_);
0142     abeam.setLumiRange(lumiRange_);
0143 
0144     // Set the creation time of the payload to the current time
0145     auto creationTime =
0146         std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch())
0147             .count();
0148     abeam.setCreationTime(creationTime);
0149 
0150     edm::LogPrint("BeamSpotOnlineFromOfflineConverter") << " Writing results to DB...";
0151     edm::LogPrint("BeamSpotOnlineFromOfflineConverter") << abeam;
0152 
0153     edm::Service<cond::service::PoolDBOutputService> poolDbService;
0154     if (poolDbService.isAvailable()) {
0155       edm::LogPrint("BeamSpotOnlineFromOfflineConverter") << "poolDBService available";
0156       if (poolDbService->isNewTagRequest(fLabel_)) {
0157         edm::LogPrint("BeamSpotOnlineFromOfflineConverter") << "new tag requested";
0158         if (fuseNewSince_) {
0159           edm::LogPrint("BeamSpotOnlineFromOfflineConverter") << "Using a new Since: " << fnewSince_;
0160           poolDbService->createOneIOV<BeamSpotOnlineObjects>(abeam, fnewSince_, fLabel_);
0161         } else
0162           poolDbService->createOneIOV<BeamSpotOnlineObjects>(abeam, poolDbService->beginOfTime(), fLabel_);
0163       } else {
0164         edm::LogPrint("BeamSpotOnlineFromOfflineConverter") << "no new tag requested";
0165         if (fuseNewSince_) {
0166           cond::Time_t thisSince = BeamSpotOnlineFromOfflineConverter::pack(
0167               iEvent.getLuminosityBlock().run(), iEvent.getLuminosityBlock().luminosityBlock());
0168           edm::LogPrint("BeamSpotOnlineFromOfflineConverter") << "Using a new Since: " << thisSince;
0169           poolDbService->appendOneIOV<BeamSpotOnlineObjects>(abeam, thisSince, fLabel_);
0170         } else
0171           poolDbService->appendOneIOV<BeamSpotOnlineObjects>(abeam, poolDbService->currentTime(), fLabel_);
0172       }
0173     }
0174     edm::LogPrint("BeamSpotOnlineFromOfflineConverter") << "[BeamSpotOnlineFromOfflineConverter] analyze done \n";
0175   }
0176 }
0177 
0178 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0179 void BeamSpotOnlineFromOfflineConverter::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0180   edm::ParameterSetDescription desc;
0181   desc.add<bool>("isHLT", true);
0182   desc.addOptionalUntracked<uint32_t>("IOVStartRun", 1);
0183   desc.addOptionalUntracked<uint32_t>("IOVStartLumi", 1);
0184   desc.add<double>("lastAnalyzedLumi", 1000);
0185   desc.add<double>("lastAnalyzedRun", 1);
0186   desc.add<double>("lastAnalyzedFill", -999);
0187   desc.add<int>("numTracks", 0);
0188   desc.add<int>("numPVs", 0);
0189   desc.add<int>("numUsedEvents", 0);
0190   desc.add<int>("numMaxPVs", 0);
0191   desc.add<double>("meanPVs", 0.);
0192   desc.add<double>("meanPVError", 0.);
0193   desc.add<double>("rmsPVs", 0.);
0194   desc.add<double>("rmsPVError", 0.);
0195   desc.add<std::string>("startTime", std::string(""));
0196   desc.add<std::string>("endTime", std::string(""));
0197   desc.add<std::string>("lumiRange", std::string(""));
0198   descriptions.addWithDefaultLabel(desc);
0199 }
0200 
0201 //define this as a plug-in
0202 DEFINE_FWK_MODULE(BeamSpotOnlineFromOfflineConverter);