File indexing completed on 2024-09-07 04:35:41
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020 #include <memory>
0021 #include <string>
0022 #include <fstream>
0023 #include <iostream>
0024 #include <ctime>
0025
0026
0027 #include "CondCore/AlignmentPlugins/interface/AlignmentPayloadInspectorHelper.h"
0028 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
0029 #include "CondFormats/Alignment/interface/Alignments.h"
0030 #include "CondFormats/AlignmentRecord/interface/TrackerAlignmentRcd.h"
0031 #include "CondFormats/BeamSpotObjects/interface/BeamSpotObjects.h"
0032 #include "CondFormats/BeamSpotObjects/interface/BeamSpotOnlineObjects.h"
0033 #include "CondFormats/DataRecord/interface/BeamSpotObjectsRcd.h"
0034 #include "CondFormats/DataRecord/interface/BeamSpotOnlineHLTObjectsRcd.h"
0035 #include "CondFormats/DataRecord/interface/BeamSpotOnlineLegacyObjectsRcd.h"
0036 #include "DataFormats/GeometryVector/interface/GlobalPoint.h"
0037 #include "DataFormats/TrackerCommon/interface/TrackerTopology.h"
0038 #include "FWCore/Framework/interface/ESHandle.h"
0039 #include "FWCore/Framework/interface/ESWatcher.h"
0040 #include "FWCore/Framework/interface/Event.h"
0041 #include "FWCore/Framework/interface/EventSetup.h"
0042 #include "FWCore/Framework/interface/Frameworkfwd.h"
0043 #include "FWCore/Framework/interface/MakerMacros.h"
0044 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0045 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0046 #include "FWCore/ServiceRegistry/interface/Service.h"
0047 #include "FWCore/Utilities/interface/InputTag.h"
0048 #include "Geometry/Records/interface/TrackerTopologyRcd.h"
0049
0050
0051
0052
0053
0054 class BeamSpotOnlineShifter : public edm::one::EDAnalyzer<edm::one::WatchRuns> {
0055 public:
0056 explicit BeamSpotOnlineShifter(const edm::ParameterSet&);
0057 ~BeamSpotOnlineShifter() override = default;
0058
0059 static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0060 cond::Time_t pack(uint32_t, uint32_t);
0061 template <class Record>
0062 void writeToDB(const edm::Event& iEvent,
0063 const edm::EventSetup& iSetup,
0064 const edm::ESGetToken<BeamSpotOnlineObjects, Record>& token);
0065
0066 private:
0067 const GlobalPoint getPixelBarycenter(const AlignmentPI::TkAlBarycenters barycenters, const bool isFullPixel);
0068 const GlobalPoint deltaAlignments(const Alignments* target,
0069 const Alignments* reference,
0070 const TrackerTopology& tTopo,
0071 const bool isFullPixel = false);
0072
0073 void beginRun(const edm::Run&, const edm::EventSetup&) override;
0074 void endRun(const edm::Run&, const edm::EventSetup&) override {}
0075
0076 void analyze(const edm::Event&, const edm::EventSetup&) override;
0077
0078
0079
0080 edm::ESGetToken<BeamSpotOnlineObjects, BeamSpotOnlineHLTObjectsRcd> hltToken_;
0081 edm::ESGetToken<BeamSpotOnlineObjects, BeamSpotOnlineLegacyObjectsRcd> legacyToken_;
0082
0083 edm::ESWatcher<BeamSpotOnlineHLTObjectsRcd> bsHLTWatcher_;
0084 edm::ESWatcher<BeamSpotOnlineLegacyObjectsRcd> bsLegayWatcher_;
0085
0086
0087 GlobalPoint theShift_;
0088 const bool fIsHLT_;
0089 const bool fullPixel_;
0090 const edm::ESGetToken<TrackerTopology, TrackerTopologyRcd> trackerTopoTokenBR_;
0091 const edm::ESGetToken<Alignments, TrackerAlignmentRcd> refAliTokenBR_;
0092 const edm::ESGetToken<Alignments, TrackerAlignmentRcd> tarAliTokenBR_;
0093 const double xShift_;
0094 const double yShift_;
0095 const double zShift_;
0096 uint32_t fIOVStartRun_;
0097 uint32_t fIOVStartLumi_;
0098 cond::Time_t fnewSince_;
0099 bool fuseNewSince_;
0100 std::string fLabel_;
0101 };
0102
0103
0104
0105
0106 BeamSpotOnlineShifter::BeamSpotOnlineShifter(const edm::ParameterSet& iConfig)
0107 : theShift_(GlobalPoint()),
0108 fIsHLT_(iConfig.getParameter<bool>("isHLT")),
0109 fullPixel_(iConfig.getParameter<bool>("useFullPixel")),
0110 trackerTopoTokenBR_(esConsumes<edm::Transition::BeginRun>()),
0111 refAliTokenBR_(esConsumes<edm::Transition::BeginRun>(edm::ESInputTag("", "reference"))),
0112 tarAliTokenBR_(esConsumes<edm::Transition::BeginRun>(edm::ESInputTag("", "target"))),
0113 xShift_(iConfig.getParameter<double>("xShift")),
0114 yShift_(iConfig.getParameter<double>("yShift")),
0115 zShift_(iConfig.getParameter<double>("zShift")) {
0116 if (iConfig.exists("IOVStartRun") && iConfig.exists("IOVStartLumi")) {
0117 fIOVStartRun_ = iConfig.getUntrackedParameter<uint32_t>("IOVStartRun");
0118 fIOVStartLumi_ = iConfig.getUntrackedParameter<uint32_t>("IOVStartLumi");
0119 fnewSince_ = BeamSpotOnlineShifter::pack(fIOVStartRun_, fIOVStartLumi_);
0120 fuseNewSince_ = true;
0121 edm::LogPrint("BeamSpotOnlineShifter") << "useNewSince = True";
0122 } else {
0123 fuseNewSince_ = false;
0124 edm::LogPrint("BeamSpotOnlineShifter") << "useNewSince = False";
0125 }
0126 fLabel_ = (fIsHLT_) ? "BeamSpotOnlineHLTObjectsRcd" : "BeamSpotOnlineLegacyObjectsRcd";
0127
0128 if (fIsHLT_) {
0129 hltToken_ = esConsumes();
0130 } else {
0131 legacyToken_ = esConsumes();
0132 }
0133 }
0134
0135
0136
0137
0138
0139
0140 cond::Time_t BeamSpotOnlineShifter::pack(uint32_t fIOVStartRun, uint32_t fIOVStartLumi) {
0141 return ((uint64_t)fIOVStartRun << 32 | fIOVStartLumi);
0142 }
0143
0144 template <class Record>
0145 void BeamSpotOnlineShifter::writeToDB(const edm::Event& iEvent,
0146 const edm::EventSetup& iSetup,
0147 const edm::ESGetToken<BeamSpotOnlineObjects, Record>& token) {
0148
0149 const BeamSpotOnlineObjects* inputSpot = &iSetup.getData(token);
0150
0151
0152 BeamSpotOnlineObjects abeam;
0153
0154
0155
0156
0157
0158
0159
0160 abeam.setPosition(inputSpot->x() - theShift_.x(), inputSpot->y() - theShift_.y(), inputSpot->z() - theShift_.z());
0161 abeam.setSigmaZ(inputSpot->sigmaZ());
0162 abeam.setdxdz(inputSpot->dxdz());
0163 abeam.setdydz(inputSpot->dydz());
0164 abeam.setBeamWidthX(inputSpot->beamWidthX());
0165 abeam.setBeamWidthY(inputSpot->beamWidthY());
0166 abeam.setBeamWidthXError(inputSpot->beamWidthXError());
0167 abeam.setBeamWidthYError(inputSpot->beamWidthYError());
0168
0169 for (unsigned int i = 0; i < 7; i++) {
0170 for (unsigned int j = 0; j < 7; j++) {
0171 abeam.setCovariance(i, j, inputSpot->covariance(i, j));
0172 }
0173 }
0174
0175 abeam.setType(inputSpot->beamType());
0176 abeam.setEmittanceX(inputSpot->emittanceX());
0177 abeam.setEmittanceY(inputSpot->emittanceY());
0178 abeam.setBetaStar(inputSpot->betaStar());
0179
0180
0181 abeam.setLastAnalyzedLumi(inputSpot->lastAnalyzedLumi());
0182 abeam.setLastAnalyzedRun(inputSpot->lastAnalyzedRun());
0183 abeam.setLastAnalyzedFill(inputSpot->lastAnalyzedFill());
0184 abeam.setStartTimeStamp(inputSpot->startTimeStamp());
0185 abeam.setEndTimeStamp(inputSpot->endTimeStamp());
0186 abeam.setNumTracks(inputSpot->numTracks());
0187 abeam.setNumPVs(inputSpot->numPVs());
0188 abeam.setUsedEvents(inputSpot->usedEvents());
0189 abeam.setMaxPVs(inputSpot->maxPVs());
0190 abeam.setMeanPV(inputSpot->meanPV());
0191 abeam.setMeanErrorPV(inputSpot->meanErrorPV());
0192 abeam.setRmsPV(inputSpot->rmsPV());
0193 abeam.setRmsErrorPV(inputSpot->rmsErrorPV());
0194 abeam.setStartTime(inputSpot->startTime());
0195 abeam.setEndTime(inputSpot->endTime());
0196 abeam.setLumiRange(inputSpot->lumiRange());
0197 abeam.setCreationTime(inputSpot->creationTime());
0198
0199 edm::LogPrint("BeamSpotOnlineShifter") << " Writing results to DB...";
0200 edm::LogPrint("BeamSpotOnlineShifter") << abeam;
0201
0202 edm::Service<cond::service::PoolDBOutputService> poolDbService;
0203 if (poolDbService.isAvailable()) {
0204 edm::LogPrint("BeamSpotOnlineShifter") << "poolDBService available";
0205 if (poolDbService->isNewTagRequest(fLabel_)) {
0206 edm::LogPrint("BeamSpotOnlineShifter") << "new tag requested";
0207 if (fuseNewSince_) {
0208 edm::LogPrint("BeamSpotOnlineShifter") << "Using a new Since: " << fnewSince_;
0209 poolDbService->createOneIOV<BeamSpotOnlineObjects>(abeam, fnewSince_, fLabel_);
0210 } else
0211 poolDbService->createOneIOV<BeamSpotOnlineObjects>(abeam, poolDbService->beginOfTime(), fLabel_);
0212 } else {
0213 edm::LogPrint("BeamSpotOnlineShifter") << "no new tag requested";
0214 if (fuseNewSince_) {
0215 cond::Time_t thisSince = BeamSpotOnlineShifter::pack(iEvent.getLuminosityBlock().run(),
0216 iEvent.getLuminosityBlock().luminosityBlock());
0217 edm::LogPrint("BeamSpotOnlineShifter") << "Using a new Since: " << thisSince;
0218 poolDbService->appendOneIOV<BeamSpotOnlineObjects>(abeam, thisSince, fLabel_);
0219 } else
0220 poolDbService->appendOneIOV<BeamSpotOnlineObjects>(abeam, poolDbService->currentTime(), fLabel_);
0221 }
0222 }
0223 edm::LogPrint("BeamSpotOnlineShifter") << "[BeamSpotOnlineShifter] analyze done \n";
0224 }
0225
0226
0227 const GlobalPoint BeamSpotOnlineShifter::deltaAlignments(const Alignments* target,
0228 const Alignments* reference,
0229 const TrackerTopology& tTopo,
0230 const bool isFullPixel) {
0231 const std::map<AlignmentPI::coordinate, float> theZero = {
0232 {AlignmentPI::t_x, 0.0}, {AlignmentPI::t_y, 0.0}, {AlignmentPI::t_z, 0.0}};
0233
0234 AlignmentPI::TkAlBarycenters ref_barycenters;
0235 ref_barycenters.computeBarycenters(reference->m_align, tTopo, theZero);
0236 const auto& ref = this->getPixelBarycenter(ref_barycenters, isFullPixel);
0237
0238 AlignmentPI::TkAlBarycenters tar_barycenters;
0239 tar_barycenters.computeBarycenters(target->m_align, tTopo, theZero);
0240 const auto& tar = this->getPixelBarycenter(tar_barycenters, isFullPixel);
0241
0242 return GlobalPoint(tar.x() - ref.x(), tar.y() - ref.y(), tar.z() - ref.z());
0243 }
0244
0245
0246 const GlobalPoint BeamSpotOnlineShifter::getPixelBarycenter(AlignmentPI::TkAlBarycenters barycenters,
0247 const bool isFullPixel) {
0248 const auto& BPix = barycenters.getPartitionAvg(AlignmentPI::PARTITION::BPIX);
0249 const double BPixMods = barycenters.getNModules(AlignmentPI::PARTITION::BPIX);
0250
0251 const auto& FPixM = barycenters.getPartitionAvg(AlignmentPI::PARTITION::FPIXm);
0252 const double FPixMMods = barycenters.getNModules(AlignmentPI::PARTITION::FPIXm);
0253
0254 const auto& FPixP = barycenters.getPartitionAvg(AlignmentPI::PARTITION::FPIXp);
0255 const double FPixPMods = barycenters.getNModules(AlignmentPI::PARTITION::FPIXp);
0256
0257 const double BPixFrac = BPixMods / (BPixMods + FPixMMods + FPixPMods);
0258 const double FPixMFrac = FPixMMods / (BPixMods + FPixMMods + FPixPMods);
0259 const double FPixPFrac = FPixPMods / (BPixMods + FPixMMods + FPixPMods);
0260
0261 if (isFullPixel) {
0262 return GlobalPoint(BPixFrac * BPix.x() + FPixMFrac * FPixM.x() + FPixPFrac * FPixP.x(),
0263 BPixFrac * BPix.y() + FPixMFrac * FPixM.y() + FPixPFrac * FPixP.y(),
0264 BPixFrac * BPix.z() + FPixMFrac * FPixM.z() + FPixPFrac * FPixP.z());
0265 } else {
0266 return GlobalPoint(BPix.x(), BPix.y(), BPix.z());
0267 }
0268 }
0269
0270
0271 void BeamSpotOnlineShifter::beginRun(const edm::Run& iRun, const edm::EventSetup& iSetup) {
0272 const auto& reference = iSetup.getHandle(refAliTokenBR_);
0273 const auto& target = iSetup.getHandle(tarAliTokenBR_);
0274
0275 const TrackerTopology& tTopo = iSetup.getData(trackerTopoTokenBR_);
0276
0277 if (reference.isValid() and target.isValid()) {
0278 theShift_ = this->deltaAlignments(&(*reference), &(*target), tTopo, fullPixel_);
0279 } else {
0280 theShift_ = GlobalPoint(xShift_, yShift_, zShift_);
0281 }
0282 edm::LogPrint("BeamSpotOnlineShifter") << "[BeamSpotOnlineShifter] applied shift: " << theShift_ << std::endl;
0283 }
0284
0285
0286 void BeamSpotOnlineShifter::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0287 using namespace edm;
0288 if (fIsHLT_) {
0289 if (bsHLTWatcher_.check(iSetup)) {
0290 writeToDB<BeamSpotOnlineHLTObjectsRcd>(iEvent, iSetup, hltToken_);
0291 }
0292 } else {
0293 if (bsLegayWatcher_.check(iSetup)) {
0294 writeToDB<BeamSpotOnlineLegacyObjectsRcd>(iEvent, iSetup, legacyToken_);
0295 }
0296 }
0297 }
0298
0299
0300 void BeamSpotOnlineShifter::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0301 edm::ParameterSetDescription desc;
0302 desc.add<bool>("isHLT", true);
0303 desc.add<bool>("useFullPixel", false)->setComment("use the full pixel detector to compute the barycenter");
0304 desc.add<double>("xShift", 0.0)->setComment("in cm");
0305 desc.add<double>("yShift", 0.0)->setComment("in cm");
0306 desc.add<double>("zShift", 0.0)->setComment("in cm");
0307 desc.addOptionalUntracked<uint32_t>("IOVStartRun", 1);
0308 desc.addOptionalUntracked<uint32_t>("IOVStartLumi", 1);
0309 descriptions.addWithDefaultLabel(desc);
0310 }
0311
0312
0313 DEFINE_FWK_MODULE(BeamSpotOnlineShifter);