CMS 3D CMS Logo

BeamSpotOnlineShifter.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: CondTools/BeamSpot
4 // Class: BeamSpotOnlineShifter
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
49 
50 //
51 // class declaration
52 //
53 
54 class BeamSpotOnlineShifter : public edm::one::EDAnalyzer<edm::one::WatchRuns> {
55 public:
57  ~BeamSpotOnlineShifter() override = default;
58 
59  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
60  cond::Time_t pack(uint32_t, uint32_t);
61  template <class Record>
62  void writeToDB(const edm::Event& iEvent,
63  const edm::EventSetup& iSetup,
65 
66 private:
67  const GlobalPoint getPixelBarycenter(const AlignmentPI::TkAlBarycenters barycenters, const bool isFullPixel);
69  const Alignments* reference,
70  const TrackerTopology& tTopo,
71  const bool isFullPixel = false);
72 
73  void beginRun(const edm::Run&, const edm::EventSetup&) override;
74  void endRun(const edm::Run&, const edm::EventSetup&) override{};
75 
76  void analyze(const edm::Event&, const edm::EventSetup&) override;
77 
78  // ----------member data ---------------------------
79 
82 
85 
86  // IoV-structure
88  const bool fIsHLT_;
89  const bool fullPixel_;
93  const double xShift_;
94  const double yShift_;
95  const double zShift_;
96  uint32_t fIOVStartRun_;
97  uint32_t fIOVStartLumi_;
101 };
102 
103 //
104 // constructors and destructor
105 //
107  : theShift_(GlobalPoint()),
108  fIsHLT_(iConfig.getParameter<bool>("isHLT")),
109  fullPixel_(iConfig.getParameter<bool>("useFullPixel")),
110  trackerTopoTokenBR_(esConsumes<edm::Transition::BeginRun>()),
111  refAliTokenBR_(esConsumes<edm::Transition::BeginRun>(edm::ESInputTag("", "reference"))),
112  tarAliTokenBR_(esConsumes<edm::Transition::BeginRun>(edm::ESInputTag("", "target"))),
113  xShift_(iConfig.getParameter<double>("xShift")),
114  yShift_(iConfig.getParameter<double>("yShift")),
115  zShift_(iConfig.getParameter<double>("zShift")) {
116  if (iConfig.exists("IOVStartRun") && iConfig.exists("IOVStartLumi")) {
117  fIOVStartRun_ = iConfig.getUntrackedParameter<uint32_t>("IOVStartRun");
118  fIOVStartLumi_ = iConfig.getUntrackedParameter<uint32_t>("IOVStartLumi");
120  fuseNewSince_ = true;
121  edm::LogPrint("BeamSpotOnlineShifter") << "useNewSince = True";
122  } else {
123  fuseNewSince_ = false;
124  edm::LogPrint("BeamSpotOnlineShifter") << "useNewSince = False";
125  }
126  fLabel_ = (fIsHLT_) ? "BeamSpotOnlineHLTObjectsRcd" : "BeamSpotOnlineLegacyObjectsRcd";
127 
128  if (fIsHLT_) {
129  hltToken_ = esConsumes();
130  } else {
132  }
133 }
134 
135 //
136 // member functions
137 //
138 
139 // ------------ Create a since object (cond::Time_t) by packing Run and LS (both uint32_t) ------------
140 cond::Time_t BeamSpotOnlineShifter::pack(uint32_t fIOVStartRun, uint32_t fIOVStartLumi) {
141  return ((uint64_t)fIOVStartRun << 32 | fIOVStartLumi);
142 }
143 
144 template <class Record>
146  const edm::EventSetup& iSetup,
148  // input object
149  const BeamSpotOnlineObjects* inputSpot = &iSetup.getData(token);
150 
151  // output object
152  BeamSpotOnlineObjects abeam;
153 
154  // N.B.: theShift is the difference between the target and the reference geometry barycenters
155  // so if effectively the displacement of the new origin of reference frame w.r.t the old one.
156  // This has to be subtracted from the old position of the beamspot:
157  // - if the new reference frame rises, the beamspot drops
158  // - if the new reference frame drops, the beamspot rises
159 
160  abeam.setPosition(inputSpot->x() - theShift_.x(), inputSpot->y() - theShift_.y(), inputSpot->z() - theShift_.z());
161  abeam.setSigmaZ(inputSpot->sigmaZ());
162  abeam.setdxdz(inputSpot->dxdz());
163  abeam.setdydz(inputSpot->dydz());
164  abeam.setBeamWidthX(inputSpot->beamWidthX());
165  abeam.setBeamWidthY(inputSpot->beamWidthY());
166  abeam.setBeamWidthXError(inputSpot->beamWidthXError());
167  abeam.setBeamWidthYError(inputSpot->beamWidthYError());
168 
169  for (unsigned int i = 0; i < 7; i++) {
170  for (unsigned int j = 0; j < 7; j++) {
171  abeam.setCovariance(i, j, inputSpot->covariance(i, j));
172  }
173  }
174 
175  abeam.setType(inputSpot->beamType());
176  abeam.setEmittanceX(inputSpot->emittanceX());
177  abeam.setEmittanceY(inputSpot->emittanceY());
178  abeam.setBetaStar(inputSpot->betaStar());
179 
180  // online BeamSpot object specific
181  abeam.setLastAnalyzedLumi(inputSpot->lastAnalyzedLumi());
182  abeam.setLastAnalyzedRun(inputSpot->lastAnalyzedRun());
183  abeam.setLastAnalyzedFill(inputSpot->lastAnalyzedFill());
184  abeam.setStartTimeStamp(inputSpot->startTimeStamp());
185  abeam.setEndTimeStamp(inputSpot->endTimeStamp());
186  abeam.setNumTracks(inputSpot->numTracks());
187  abeam.setNumPVs(inputSpot->numPVs());
188  abeam.setUsedEvents(inputSpot->usedEvents());
189  abeam.setMaxPVs(inputSpot->maxPVs());
190  abeam.setMeanPV(inputSpot->meanPV());
191  abeam.setMeanErrorPV(inputSpot->meanErrorPV());
192  abeam.setRmsPV(inputSpot->rmsPV());
193  abeam.setRmsErrorPV(inputSpot->rmsErrorPV());
194  abeam.setStartTime(inputSpot->startTime());
195  abeam.setEndTime(inputSpot->endTime());
196  abeam.setLumiRange(inputSpot->lumiRange());
197  abeam.setCreationTime(inputSpot->creationTime());
198 
199  edm::LogPrint("BeamSpotOnlineShifter") << " Writing results to DB...";
200  edm::LogPrint("BeamSpotOnlineShifter") << abeam;
201 
203  if (poolDbService.isAvailable()) {
204  edm::LogPrint("BeamSpotOnlineShifter") << "poolDBService available";
205  if (poolDbService->isNewTagRequest(fLabel_)) {
206  edm::LogPrint("BeamSpotOnlineShifter") << "new tag requested";
207  if (fuseNewSince_) {
208  edm::LogPrint("BeamSpotOnlineShifter") << "Using a new Since: " << fnewSince_;
209  poolDbService->createOneIOV<BeamSpotOnlineObjects>(abeam, fnewSince_, fLabel_);
210  } else
211  poolDbService->createOneIOV<BeamSpotOnlineObjects>(abeam, poolDbService->beginOfTime(), fLabel_);
212  } else {
213  edm::LogPrint("BeamSpotOnlineShifter") << "no new tag requested";
214  if (fuseNewSince_) {
215  cond::Time_t thisSince = BeamSpotOnlineShifter::pack(iEvent.getLuminosityBlock().run(),
216  iEvent.getLuminosityBlock().luminosityBlock());
217  edm::LogPrint("BeamSpotOnlineShifter") << "Using a new Since: " << thisSince;
218  poolDbService->appendOneIOV<BeamSpotOnlineObjects>(abeam, thisSince, fLabel_);
219  } else
220  poolDbService->appendOneIOV<BeamSpotOnlineObjects>(abeam, poolDbService->currentTime(), fLabel_);
221  }
222  }
223  edm::LogPrint("BeamSpotOnlineShifter") << "[BeamSpotOnlineShifter] analyze done \n";
224 }
225 
226 //_____________________________________________________________________________________________
228  const Alignments* reference,
229  const TrackerTopology& tTopo,
230  const bool isFullPixel) {
231  const std::map<AlignmentPI::coordinate, float> theZero = {
232  {AlignmentPI::t_x, 0.0}, {AlignmentPI::t_y, 0.0}, {AlignmentPI::t_z, 0.0}};
233 
234  AlignmentPI::TkAlBarycenters ref_barycenters;
235  ref_barycenters.computeBarycenters(reference->m_align, tTopo, theZero);
236  const auto& ref = this->getPixelBarycenter(ref_barycenters, isFullPixel);
237 
238  AlignmentPI::TkAlBarycenters tar_barycenters;
239  tar_barycenters.computeBarycenters(target->m_align, tTopo, theZero);
240  const auto& tar = this->getPixelBarycenter(tar_barycenters, isFullPixel);
241 
242  return GlobalPoint(tar.x() - ref.x(), tar.y() - ref.y(), tar.z() - ref.z());
243 }
244 
245 //_____________________________________________________________________________________________
247  const bool isFullPixel) {
248  const auto& BPix = barycenters.getPartitionAvg(AlignmentPI::PARTITION::BPIX);
249  const double BPixMods = barycenters.getNModules(AlignmentPI::PARTITION::BPIX);
250 
251  const auto& FPixM = barycenters.getPartitionAvg(AlignmentPI::PARTITION::FPIXm);
252  const double FPixMMods = barycenters.getNModules(AlignmentPI::PARTITION::FPIXm);
253 
254  const auto& FPixP = barycenters.getPartitionAvg(AlignmentPI::PARTITION::FPIXp);
255  const double FPixPMods = barycenters.getNModules(AlignmentPI::PARTITION::FPIXp);
256 
257  const double BPixFrac = BPixMods / (BPixMods + FPixMMods + FPixPMods);
258  const double FPixMFrac = FPixMMods / (BPixMods + FPixMMods + FPixPMods);
259  const double FPixPFrac = FPixPMods / (BPixMods + FPixMMods + FPixPMods);
260 
261  if (isFullPixel) {
262  return GlobalPoint(BPixFrac * BPix.x() + FPixMFrac * FPixM.x() + FPixPFrac * FPixP.x(),
263  BPixFrac * BPix.y() + FPixMFrac * FPixM.y() + FPixPFrac * FPixP.y(),
264  BPixFrac * BPix.z() + FPixMFrac * FPixM.z() + FPixPFrac * FPixP.z());
265  } else {
266  return GlobalPoint(BPix.x(), BPix.y(), BPix.z());
267  }
268 }
269 
270 //_____________________________________________________________________________________________
271 void BeamSpotOnlineShifter::beginRun(const edm::Run& iRun, const edm::EventSetup& iSetup) {
272  const auto& reference = iSetup.getHandle(refAliTokenBR_);
273  const auto& target = iSetup.getHandle(tarAliTokenBR_);
274 
275  const TrackerTopology& tTopo = iSetup.getData(trackerTopoTokenBR_);
276 
277  if (reference.isValid() and target.isValid()) {
278  theShift_ = this->deltaAlignments(&(*reference), &(*target), tTopo, fullPixel_);
279  } else {
281  }
282  edm::LogPrint("BeamSpotOnlineShifter") << "[BeamSpotOnlineShifter] applied shift: " << theShift_ << std::endl;
283 }
284 
285 // ------------ method called for each event ------------
287  using namespace edm;
288  if (fIsHLT_) {
289  if (bsHLTWatcher_.check(iSetup)) {
290  writeToDB<BeamSpotOnlineHLTObjectsRcd>(iEvent, iSetup, hltToken_);
291  }
292  } else {
293  if (bsLegayWatcher_.check(iSetup)) {
294  writeToDB<BeamSpotOnlineLegacyObjectsRcd>(iEvent, iSetup, legacyToken_);
295  }
296  }
297 }
298 
299 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
302  desc.add<bool>("isHLT", true);
303  desc.add<bool>("useFullPixel", false)->setComment("use the full pixel detector to compute the barycenter");
304  desc.add<double>("xShift", 0.0)->setComment("in cm");
305  desc.add<double>("yShift", 0.0)->setComment("in cm");
306  desc.add<double>("zShift", 0.0)->setComment("in cm");
307  desc.addOptionalUntracked<uint32_t>("IOVStartRun", 1);
308  desc.addOptionalUntracked<uint32_t>("IOVStartLumi", 1);
309  descriptions.addWithDefaultLabel(desc);
310 }
311 
312 //define this as a plug-in
double emittanceX() const
get emittance
const double getNModules(AlignmentPI::PARTITION p)
void setBeamWidthYError(double val)
set beam width Y error
int lastAnalyzedLumi() const
Getters Methods.
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
cond::Time_t pack(uint32_t, uint32_t)
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
cond::Time_t creationTime() const
void setCovariance(int i, int j, double val)
set i,j element of the full covariance matrix 7x7
double z() const
get Z beam position
void setStartTimeStamp(cond::Time_t val)
double dydz() const
get dydz slope, crossing angle in YZ
~BeamSpotOnlineShifter() override=default
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
Definition: EventSetup.h:119
void setEmittanceY(double val)
set emittance
double covariance(int i, int j) const
get i,j element of the full covariance matrix 7x7
edm::ESWatcher< BeamSpotOnlineLegacyObjectsRcd > bsLegayWatcher_
T z() const
Definition: PV3DBase.h:61
const edm::ESGetToken< Alignments, TrackerAlignmentRcd > refAliTokenBR_
void computeBarycenters(const std::vector< AlignTransform > &input, const TrackerTopology &tTopo, const std::map< AlignmentPI::coordinate, float > &GPR)
BeamSpotOnlineShifter(const edm::ParameterSet &)
bool exists(std::string const &parameterName) const
checks if a parameter exists
void setBetaStar(double val)
set beta star
const GlobalPoint getPixelBarycenter(const AlignmentPI::TkAlBarycenters barycenters, const bool isFullPixel)
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
const edm::ESGetToken< TrackerTopology, TrackerTopologyRcd > trackerTopoTokenBR_
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
std::string endTime() const
void setEndTimeStamp(cond::Time_t val)
double beamWidthX() const
get average transverse beam width
std::string lumiRange() const
std::string startTime() const
int beamType() const
get beam type
edm::ESGetToken< BeamSpotOnlineObjects, BeamSpotOnlineHLTObjectsRcd > hltToken_
void setType(int type)
set beam type
void endRun(const edm::Run &, const edm::EventSetup &) override
void createOneIOV(const T &payload, cond::Time_t firstSinceTime, const std::string &recordName)
void setEndTime(std::string val)
void beginRun(const edm::Run &, const edm::EventSetup &) override
void writeToDB(const edm::Event &iEvent, const edm::EventSetup &iSetup, const edm::ESGetToken< BeamSpotOnlineObjects, Record > &token)
T getUntrackedParameter(std::string const &, T const &) const
void appendOneIOV(const T &payload, cond::Time_t sinceTime, const std::string &recordName)
T x() const
Definition: PV3DBase.h:59
T y() const
Definition: PV3DBase.h:60
void setEmittanceX(double val)
set emittance
int iEvent
Definition: GenABIO.cc:224
double beamWidthYError() const
get average transverse beam width error X = Y
unsigned long long Time_t
Definition: Time.h:14
bool isNewTagRequest(const std::string &recordName)
void setdydz(double val)
set dydz slope, crossing angle in XZ
cond::Time_t endTimeStamp() const
const edm::ESGetToken< Alignments, TrackerAlignmentRcd > tarAliTokenBR_
Transition
Definition: Transition.h:12
double x() const
get X beam position
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
Log< level::Warning, true > LogPrint
ESHandle< T > getHandle(const ESGetToken< T, R > &iToken) const
Definition: EventSetup.h:130
void setdxdz(double val)
set dxdz slope, crossing angle
edm::ESGetToken< BeamSpotOnlineObjects, BeamSpotOnlineLegacyObjectsRcd > legacyToken_
double beamWidthY() const
get average transverse beam width
double y() const
get Y beam position
void analyze(const edm::Event &, const edm::EventSetup &) override
GlobalPoint getPartitionAvg(AlignmentPI::PARTITION p)
cond::Time_t startTimeStamp() const
edm::ESWatcher< BeamSpotOnlineHLTObjectsRcd > bsHLTWatcher_
unsigned long long uint64_t
Definition: Time.h:13
void setBeamWidthXError(double val)
set beam width X error
const GlobalPoint deltaAlignments(const Alignments *target, const Alignments *reference, const TrackerTopology &tTopo, const bool isFullPixel=false)
void setSigmaZ(double val)
set sigma Z, RMS bunch length
void setLumiRange(std::string val)
double sigmaZ() const
get sigma Z, RMS bunch length
bool check(const edm::EventSetup &iSetup)
Definition: ESWatcher.h:57
double emittanceY() const
get emittance
double betaStar() const
get beta star
HLT enums.
double beamWidthXError() const
get average transverse beam width error ASSUME the same for X and Y
void setStartTime(std::string val)
void setPosition(double x, double y, double z)
set XYZ position
void setBeamWidthX(double val)
set average transverse beam width X
void setCreationTime(cond::Time_t val)
bool isAvailable() const
Definition: Service.h:40
double dxdz() const
get dxdz slope, crossing angle in XZ
void setBeamWidthY(double val)
set average transverse beam width Y
Definition: Run.h:45