CMS 3D CMS Logo

BeamSpotOnlineFromOfflineConverter.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: CondTools/BeamSpot
4 // Class: BeamSpotOnlineFromOfflineConverter
5 //
13 //
14 // Original Author: Marco Musich
15 // Created: Sat, 06 May 2023 21:10:00 GMT
16 //
17 //
18 
19 // system include files
20 #include <memory>
21 #include <string>
22 #include <fstream>
23 #include <iostream>
24 #include <ctime>
25 
26 // user include files
43 
44 //
45 // class declaration
46 //
47 
49 public:
51  ~BeamSpotOnlineFromOfflineConverter() override = default;
52 
53  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
54  cond::Time_t pack(uint32_t, uint32_t);
55 
56 private:
57  void analyze(const edm::Event&, const edm::EventSetup&) override;
58 
59  // ----------member data ---------------------------
62 
63  // parameters that can't be copied from the BeamSpotObject
68 
69  // IoV-structure
70  const bool fIsHLT_;
71  uint32_t fIOVStartRun_;
72  uint32_t fIOVStartLumi_;
76 };
77 
78 //
79 // constructors and destructor
80 //
82  : bsToken_(esConsumes()),
83  lastAnalyzedLumi_(iConfig.getParameter<double>("lastAnalyzedLumi")),
84  lastAnalyzedRun_(iConfig.getParameter<double>("lastAnalyzedRun")),
85  lastAnalyzedFill_(iConfig.getParameter<double>("lastAnalyzedFill")),
86  numTracks_(iConfig.getParameter<int>("numTracks")),
87  numPVs_(iConfig.getParameter<int>("numPVs")),
88  numUsedEvents_(iConfig.getParameter<int>("numUsedEvents")),
89  numMaxPVs_(iConfig.getParameter<int>("numMaxPVs")),
90  meanPVs_(iConfig.getParameter<double>("meanPVs")),
91  meanPVError_(iConfig.getParameter<double>("meanPVError")),
92  rmsPV_(iConfig.getParameter<double>("rmsPVs")),
93  rmsPVError_(iConfig.getParameter<double>("rmsPVError")),
94  startTime_(iConfig.getParameter<std::string>("startTime")),
95  endTime_(iConfig.getParameter<std::string>("endTime")),
96  lumiRange_(iConfig.getParameter<std::string>("lumiRange")),
97  fIsHLT_(iConfig.getParameter<bool>("isHLT")) {
98  if (iConfig.exists("IOVStartRun") && iConfig.exists("IOVStartLumi")) {
99  fIOVStartRun_ = iConfig.getUntrackedParameter<uint32_t>("IOVStartRun");
100  fIOVStartLumi_ = iConfig.getUntrackedParameter<uint32_t>("IOVStartLumi");
102  fuseNewSince_ = true;
103  edm::LogPrint("BeamSpotOnlineFromOfflineConverter") << "useNewSince = True";
104  } else {
105  fuseNewSince_ = false;
106  edm::LogPrint("BeamSpotOnlineFromOfflineConverter") << "useNewSince = False";
107  }
108  fLabel_ = (fIsHLT_) ? "BeamSpotOnlineHLTObjectsRcd" : "BeamSpotOnlineLegacyObjectsRcd";
109 }
110 
111 //
112 // member functions
113 //
114 
115 // ------------ Create a since object (cond::Time_t) by packing Run and LS (both uint32_t) ------------
116 cond::Time_t BeamSpotOnlineFromOfflineConverter::pack(uint32_t fIOVStartRun, uint32_t fIOVStartLumi) {
117  return ((uint64_t)fIOVStartRun << 32 | fIOVStartLumi);
118 }
119 
120 // ------------ method called for each event ------------
122  if (bsWatcher_.check(iSetup)) {
123  const BeamSpotObjects* inputSpot = &iSetup.getData(bsToken_);
124 
125  BeamSpotOnlineObjects abeam;
126  abeam.copyFromBeamSpotObject(*inputSpot);
130  abeam.setStartTimeStamp(std::time(nullptr));
131  abeam.setEndTimeStamp(std::time(nullptr));
132  abeam.setNumTracks(numTracks_);
133  abeam.setNumPVs(numPVs_);
135  abeam.setMaxPVs(numMaxPVs_);
136  abeam.setMeanPV(meanPVs_);
138  abeam.setRmsPV(rmsPV_);
139  abeam.setRmsErrorPV(rmsPVError_);
140  abeam.setStartTime(startTime_);
141  abeam.setEndTime(endTime_);
142  abeam.setLumiRange(lumiRange_);
143 
144  // Set the creation time of the payload to the current time
145  auto creationTime =
146  std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch())
147  .count();
149 
150  edm::LogPrint("BeamSpotOnlineFromOfflineConverter") << " Writing results to DB...";
151  edm::LogPrint("BeamSpotOnlineFromOfflineConverter") << abeam;
152 
154  if (poolDbService.isAvailable()) {
155  edm::LogPrint("BeamSpotOnlineFromOfflineConverter") << "poolDBService available";
156  if (poolDbService->isNewTagRequest(fLabel_)) {
157  edm::LogPrint("BeamSpotOnlineFromOfflineConverter") << "new tag requested";
158  if (fuseNewSince_) {
159  edm::LogPrint("BeamSpotOnlineFromOfflineConverter") << "Using a new Since: " << fnewSince_;
160  poolDbService->createOneIOV<BeamSpotOnlineObjects>(abeam, fnewSince_, fLabel_);
161  } else
162  poolDbService->createOneIOV<BeamSpotOnlineObjects>(abeam, poolDbService->beginOfTime(), fLabel_);
163  } else {
164  edm::LogPrint("BeamSpotOnlineFromOfflineConverter") << "no new tag requested";
165  if (fuseNewSince_) {
167  iEvent.getLuminosityBlock().run(), iEvent.getLuminosityBlock().luminosityBlock());
168  edm::LogPrint("BeamSpotOnlineFromOfflineConverter") << "Using a new Since: " << thisSince;
169  poolDbService->appendOneIOV<BeamSpotOnlineObjects>(abeam, thisSince, fLabel_);
170  } else
171  poolDbService->appendOneIOV<BeamSpotOnlineObjects>(abeam, poolDbService->currentTime(), fLabel_);
172  }
173  }
174  edm::LogPrint("BeamSpotOnlineFromOfflineConverter") << "[BeamSpotOnlineFromOfflineConverter] analyze done \n";
175  }
176 }
177 
178 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
181  desc.add<bool>("isHLT", true);
182  desc.addOptionalUntracked<uint32_t>("IOVStartRun", 1);
183  desc.addOptionalUntracked<uint32_t>("IOVStartLumi", 1);
184  desc.add<double>("lastAnalyzedLumi", 1000);
185  desc.add<double>("lastAnalyzedRun", 1);
186  desc.add<double>("lastAnalyzedFill", -999);
187  desc.add<int>("numTracks", 0);
188  desc.add<int>("numPVs", 0);
189  desc.add<int>("numUsedEvents", 0);
190  desc.add<int>("numMaxPVs", 0);
191  desc.add<double>("meanPVs", 0.);
192  desc.add<double>("meanPVError", 0.);
193  desc.add<double>("rmsPVs", 0.);
194  desc.add<double>("rmsPVError", 0.);
195  desc.add<std::string>("startTime", std::string(""));
196  desc.add<std::string>("endTime", std::string(""));
197  desc.add<std::string>("lumiRange", std::string(""));
198  descriptions.addWithDefaultLabel(desc);
199 }
200 
201 //define this as a plug-in
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
void setStartTimeStamp(cond::Time_t val)
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
Definition: EventSetup.h:119
bool exists(std::string const &parameterName) const
checks if a parameter exists
void setEndTimeStamp(cond::Time_t val)
void createOneIOV(const T &payload, cond::Time_t firstSinceTime, const std::string &recordName)
~BeamSpotOnlineFromOfflineConverter() override=default
void setEndTime(std::string val)
void analyze(const edm::Event &, const edm::EventSetup &) override
T getUntrackedParameter(std::string const &, T const &) const
void appendOneIOV(const T &payload, cond::Time_t sinceTime, const std::string &recordName)
int iEvent
Definition: GenABIO.cc:224
unsigned long long Time_t
Definition: Time.h:14
bool isNewTagRequest(const std::string &recordName)
void copyFromBeamSpotObject(const BeamSpotObjects &bs)
Setters Methods.
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
Log< level::Warning, true > LogPrint
const edm::ESGetToken< BeamSpotObjects, BeamSpotObjectsRcd > bsToken_
unsigned long long uint64_t
Definition: Time.h:13
void setLumiRange(std::string val)
bool check(const edm::EventSetup &iSetup)
Definition: ESWatcher.h:57
void setStartTime(std::string val)
void setCreationTime(cond::Time_t val)
bool isAvailable() const
Definition: Service.h:40
edm::ESWatcher< BeamSpotObjectsRcd > bsWatcher_