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
42 
43 //
44 // class declaration
45 //
46 
48 public:
50  ~BeamSpotOnlineFromOfflineConverter() override = default;
51 
52  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
53  cond::Time_t pack(uint32_t, uint32_t);
54 
55 private:
56  void analyze(const edm::Event&, const edm::EventSetup&) override;
57 
58  // ----------member data ---------------------------
60 
61  // parameters that can't be copied from the BeamSpotObject
66 
67  // IoV-structure
68  const bool fIsHLT_;
69  uint32_t fIOVStartRun_;
70  uint32_t fIOVStartLumi_;
73 };
74 
75 //
76 // constructors and destructor
77 //
79  : bsToken_(esConsumes()),
80  lastAnalyzedLumi_(iConfig.getParameter<double>("lastAnalyzedLumi")),
81  lastAnalyzedRun_(iConfig.getParameter<double>("lastAnalyzedRun")),
82  lastAnalyzedFill_(iConfig.getParameter<double>("lastAnalyzedFill")),
83  numTracks_(iConfig.getParameter<int>("numTracks")),
84  numPVs_(iConfig.getParameter<int>("numPVs")),
85  numUsedEvents_(iConfig.getParameter<int>("numUsedEvents")),
86  numMaxPVs_(iConfig.getParameter<int>("numMaxPVs")),
87  meanPVs_(iConfig.getParameter<double>("meanPVs")),
88  meanPVError_(iConfig.getParameter<double>("meanPVError")),
89  rmsPV_(iConfig.getParameter<double>("rmsPVs")),
90  rmsPVError_(iConfig.getParameter<double>("rmsPVError")),
91  startTime_(iConfig.getParameter<std::string>("startTime")),
92  endTime_(iConfig.getParameter<std::string>("endTime")),
93  lumiRange_(iConfig.getParameter<std::string>("lumiRange")),
94  fIsHLT_(iConfig.getParameter<bool>("isHLT")) {
95  if (iConfig.exists("IOVStartRun") && iConfig.exists("IOVStartLumi")) {
96  fIOVStartRun_ = iConfig.getUntrackedParameter<uint32_t>("IOVStartRun");
97  fIOVStartLumi_ = iConfig.getUntrackedParameter<uint32_t>("IOVStartLumi");
99  fuseNewSince_ = true;
100  edm::LogPrint("BeamSpotOnlineFromOfflineConverter") << "useNewSince = True";
101  } else {
102  fuseNewSince_ = false;
103  edm::LogPrint("BeamSpotOnlineFromOfflineConverter") << "useNewSince = False";
104  }
105 }
106 
107 //
108 // member functions
109 //
110 
111 // ------------ Create a since object (cond::Time_t) by packing Run and LS (both uint32_t) ------------
112 cond::Time_t BeamSpotOnlineFromOfflineConverter::pack(uint32_t fIOVStartRun, uint32_t fIOVStartLumi) {
113  return ((uint64_t)fIOVStartRun << 32 | fIOVStartLumi);
114 }
115 
116 // ------------ method called for each event ------------
118  const std::string fLabel = (fIsHLT_) ? "BeamSpotOnlineHLTObjectsRcd" : "BeamSpotOnlineLegacyObjectsRcd";
119  const BeamSpotObjects* inputSpot = &iSetup.getData(bsToken_);
120 
121  BeamSpotOnlineObjects abeam;
122  abeam.copyFromBeamSpotObject(*inputSpot);
126  abeam.setStartTimeStamp(std::time(nullptr));
127  abeam.setEndTimeStamp(std::time(nullptr));
128  abeam.setNumTracks(numTracks_);
129  abeam.setNumPVs(numPVs_);
131  abeam.setMaxPVs(numMaxPVs_);
132  abeam.setMeanPV(meanPVs_);
134  abeam.setRmsPV(rmsPV_);
135  abeam.setRmsErrorPV(rmsPVError_);
136  abeam.setStartTime(startTime_);
137  abeam.setEndTime(endTime_);
138  abeam.setLumiRange(lumiRange_);
139 
140  // Set the creation time of the payload to the current time
141  auto creationTime =
142  std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
144 
145  edm::LogPrint("BeamSpotOnlineFromOfflineConverter") << " Writing results to DB...";
146  edm::LogPrint("BeamSpotOnlineFromOfflineConverter") << abeam;
147 
149  if (poolDbService.isAvailable()) {
150  edm::LogPrint("BeamSpotOnlineFromOfflineConverter") << "poolDBService available";
151  if (poolDbService->isNewTagRequest(fLabel)) {
152  edm::LogPrint("BeamSpotOnlineFromOfflineConverter") << "new tag requested";
153  if (fuseNewSince_) {
154  edm::LogPrint("BeamSpotOnlineFromOfflineConverter") << "Using a new Since: " << fnewSince_;
155  poolDbService->createOneIOV<BeamSpotOnlineObjects>(abeam, fnewSince_, fLabel);
156  } else
157  poolDbService->createOneIOV<BeamSpotOnlineObjects>(abeam, poolDbService->beginOfTime(), fLabel);
158  } else {
159  edm::LogPrint("BeamSpotOnlineFromOfflineConverter") << "no new tag requested";
160  if (fuseNewSince_) {
161  edm::LogPrint("BeamSpotOnlineFromOfflineConverter") << "Using a new Since: " << fnewSince_;
162  poolDbService->appendOneIOV<BeamSpotOnlineObjects>(abeam, fnewSince_, fLabel);
163  } else
164  poolDbService->appendOneIOV<BeamSpotOnlineObjects>(abeam, poolDbService->currentTime(), fLabel);
165  }
166  }
167  edm::LogPrint("BeamSpotOnlineFromOfflineConverter") << "[BeamSpotOnlineFromOfflineConverter] analyze done \n";
168 }
169 
170 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
173  desc.add<bool>("isHLT", true);
174  desc.addOptionalUntracked<uint32_t>("IOVStartRun", 1);
175  desc.addOptionalUntracked<uint32_t>("IOVStartLumi", 1);
176  desc.add<double>("lastAnalyzedLumi", 1000);
177  desc.add<double>("lastAnalyzedRun", 1);
178  desc.add<double>("lastAnalyzedFill", -999);
179  desc.add<int>("numTracks", 0);
180  desc.add<int>("numPVs", 0);
181  desc.add<int>("numUsedEvents", 0);
182  desc.add<int>("numMaxPVs", 0);
183  desc.add<double>("meanPVs", 0.);
184  desc.add<double>("meanPVError", 0.);
185  desc.add<double>("rmsPVs", 0.);
186  desc.add<double>("rmsPVError", 0.);
187  desc.add<std::string>("startTime", std::string(""));
188  desc.add<std::string>("endTime", std::string(""));
189  desc.add<std::string>("lumiRange", std::string(""));
190  descriptions.addWithDefaultLabel(desc);
191 }
192 
193 //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)
void setStartTime(std::string val)
void setCreationTime(cond::Time_t val)
bool isAvailable() const
Definition: Service.h:40