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:      BeamSpotOnlineRecordsWriter
0005 //
0006 /**\class BeamSpotOnlineRecordsWriter BeamSpotOnlineRecordsWriter.cc CondTools/BeamSpot/plugins/BeamSpotOnlineRecordsWriter.cc
0007 
0008  Description: EDAnalyzer to create a BeamSpotOnlineHLTObjectsRcd or BeamSpotOnlineLegacyObjectsRcd payload from a txt file and dump it in a db file
0009 
0010  Implementation:
0011      [Notes on implementation]
0012 */
0013 //
0014 // Original Author:  Francesco Brivio
0015 //         Created:  Tue, 11 Feb 2020 11:10:12 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 "FWCore/Framework/interface/Frameworkfwd.h"
0028 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0029 #include "FWCore/Framework/interface/Event.h"
0030 #include "FWCore/Framework/interface/MakerMacros.h"
0031 
0032 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0033 #include "FWCore/Utilities/interface/InputTag.h"
0034 #include "FWCore/ServiceRegistry/interface/Service.h"
0035 
0036 #include "CondFormats/DataRecord/interface/BeamSpotOnlineLegacyObjectsRcd.h"
0037 #include "CondFormats/DataRecord/interface/BeamSpotOnlineHLTObjectsRcd.h"
0038 #include "CondFormats/BeamSpotObjects/interface/BeamSpotOnlineObjects.h"
0039 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
0040 
0041 //
0042 // class declaration
0043 //
0044 
0045 class BeamSpotOnlineRecordsWriter : public edm::one::EDAnalyzer<> {
0046 public:
0047   explicit BeamSpotOnlineRecordsWriter(const edm::ParameterSet&);
0048   ~BeamSpotOnlineRecordsWriter() override;
0049 
0050   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0051   cond::Time_t pack(uint32_t, uint32_t);
0052 
0053 private:
0054   void analyze(const edm::Event&, const edm::EventSetup&) override;
0055   void endJob() override;
0056 
0057   // ----------member data ---------------------------
0058   bool fIsHLT;
0059   std::ifstream fasciiFile;
0060   std::string fasciiFileName;
0061   uint32_t fIOVStartRun;
0062   uint32_t fIOVStartLumi;
0063   cond::Time_t fnewSince;
0064   bool fuseNewSince;
0065 };
0066 
0067 //
0068 // constructors and destructor
0069 //
0070 BeamSpotOnlineRecordsWriter::BeamSpotOnlineRecordsWriter(const edm::ParameterSet& iConfig) {
0071   //now do what ever initialization is needed
0072   fIsHLT = iConfig.getParameter<bool>("isHLT");
0073   fasciiFileName = iConfig.getUntrackedParameter<std::string>("InputFileName");
0074   fasciiFile.open(fasciiFileName.c_str());
0075   if (iConfig.exists("IOVStartRun") && iConfig.exists("IOVStartLumi")) {
0076     fIOVStartRun = iConfig.getUntrackedParameter<uint32_t>("IOVStartRun");
0077     fIOVStartLumi = iConfig.getUntrackedParameter<uint32_t>("IOVStartLumi");
0078     fnewSince = BeamSpotOnlineRecordsWriter::pack(fIOVStartRun, fIOVStartLumi);
0079     fuseNewSince = true;
0080     edm::LogPrint("BeamSpotOnlineRecordsWriter") << "useNewSince = True";
0081   } else {
0082     fuseNewSince = false;
0083     edm::LogPrint("BeamSpotOnlineRecordsWriter") << "useNewSince = False";
0084   }
0085 }
0086 
0087 BeamSpotOnlineRecordsWriter::~BeamSpotOnlineRecordsWriter() = default;
0088 
0089 //
0090 // member functions
0091 //
0092 
0093 // ------------ Create a since object (cond::Time_t) by packing Run and LS (both uint32_t)  ------------
0094 cond::Time_t BeamSpotOnlineRecordsWriter::pack(uint32_t fIOVStartRun, uint32_t fIOVStartLumi) {
0095   return ((uint64_t)fIOVStartRun << 32 | fIOVStartLumi);
0096 }
0097 
0098 // ------------ method called for each event  ------------
0099 void BeamSpotOnlineRecordsWriter::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {}
0100 
0101 // ------------ method called once each job just after ending the event loop  ------------
0102 void BeamSpotOnlineRecordsWriter::endJob() {
0103   const std::string fLabel = (fIsHLT) ? "BeamSpotOnlineHLTObjectsRcd" : "BeamSpotOnlineLegacyObjectsRcd";
0104   edm::LogInfo("BeamSpotOnlineRecordsWriter")
0105       << "Reading " << fLabel << " data from text file: " << fasciiFileName << std::endl;
0106 
0107   // extract from file
0108   double x, y, z, sigmaZ, dxdz, dydz, beamWidthX, beamWidthY, emittanceX, emittanceY, betastar;
0109   double cov[7][7];
0110   int type, lastAnalyzedLumi, firstAnalyzedLumi, lastAnalyzedRun, lastAnalyzedFill;
0111   std::string tag;
0112   std::time_t lumiRangeBeginTime, lumiRangeEndTime;
0113 
0114   fasciiFile >> tag >> lastAnalyzedRun;
0115   fasciiFile >> tag >> tag >> tag >> tag >> lumiRangeBeginTime;  // BeginTimeOfFit parsing (not used in payload)
0116   fasciiFile >> tag >> tag >> tag >> tag >> lumiRangeEndTime;    // EndTimeOfFit parsing (not used in payload)
0117   fasciiFile >> tag >> firstAnalyzedLumi;
0118   fasciiFile >> tag >> lastAnalyzedLumi;
0119   fasciiFile >> tag >> type;
0120   fasciiFile >> tag >> x;
0121   fasciiFile >> tag >> y;
0122   fasciiFile >> tag >> z;
0123   fasciiFile >> tag >> sigmaZ;
0124   fasciiFile >> tag >> dxdz;
0125   fasciiFile >> tag >> dydz;
0126   fasciiFile >> tag >> beamWidthX;
0127   fasciiFile >> tag >> beamWidthY;
0128   fasciiFile >> tag >> cov[0][0] >> cov[0][1] >> cov[0][2] >> cov[0][3] >> cov[0][4] >> cov[0][5] >> cov[0][6];
0129   fasciiFile >> tag >> cov[1][0] >> cov[1][1] >> cov[1][2] >> cov[1][3] >> cov[1][4] >> cov[1][5] >> cov[1][6];
0130   fasciiFile >> tag >> cov[2][0] >> cov[2][1] >> cov[2][2] >> cov[2][3] >> cov[2][4] >> cov[2][5] >> cov[2][6];
0131   fasciiFile >> tag >> cov[3][0] >> cov[3][1] >> cov[3][2] >> cov[3][3] >> cov[3][4] >> cov[3][5] >> cov[3][6];
0132   fasciiFile >> tag >> cov[4][0] >> cov[4][1] >> cov[4][2] >> cov[4][3] >> cov[4][4] >> cov[4][5] >> cov[4][6];
0133   fasciiFile >> tag >> cov[5][0] >> cov[5][1] >> cov[5][2] >> cov[5][3] >> cov[5][4] >> cov[5][5] >> cov[5][6];
0134   fasciiFile >> tag >> cov[6][0] >> cov[6][1] >> cov[6][2] >> cov[6][3] >> cov[6][4] >> cov[6][5] >> cov[6][6];
0135   fasciiFile >> tag >> emittanceX;
0136   fasciiFile >> tag >> emittanceY;
0137   fasciiFile >> tag >> betastar;
0138 
0139   lastAnalyzedFill = -999;
0140 
0141   // Verify that the parsing was correct by checking the BS positions
0142   if (std::fabs(x) > 1000. || std::fabs(x) < 1.e-20 || std::fabs(y) > 1000. || std::fabs(y) < 1.e-20 ||
0143       std::fabs(z) > 1000. || std::fabs(z) < 1.e-20) {
0144     throw edm::Exception(edm::errors::Unknown)
0145         << " !!! Error in parsing input file, parsed BS (x,y,z): (" << x << "," << y << "," << z << ") !!!";
0146   }
0147 
0148   edm::LogPrint("BeamSpotOnlineRecordsWriter") << "---- Parsed these parameters from input txt file ----";
0149   edm::LogPrint("BeamSpotOnlineRecordsWriter") << " lastAnalyzedRun   : " << lastAnalyzedRun;
0150   edm::LogPrint("BeamSpotOnlineRecordsWriter") << " lastAnalyzedFill  : " << lastAnalyzedFill;
0151   edm::LogPrint("BeamSpotOnlineRecordsWriter") << " firstAnalyzedLumi : " << firstAnalyzedLumi;
0152   edm::LogPrint("BeamSpotOnlineRecordsWriter") << " lastAnalyzedLumi  : " << lastAnalyzedLumi;
0153   edm::LogPrint("BeamSpotOnlineRecordsWriter") << " lumiRangeBeginTime: " << lumiRangeBeginTime;
0154   edm::LogPrint("BeamSpotOnlineRecordsWriter") << " lumiRangeEndTime  : " << lumiRangeEndTime;
0155   edm::LogPrint("BeamSpotOnlineRecordsWriter") << " type              : " << type;
0156   edm::LogPrint("BeamSpotOnlineRecordsWriter") << " x                 : " << x;
0157   edm::LogPrint("BeamSpotOnlineRecordsWriter") << " y                 : " << y;
0158   edm::LogPrint("BeamSpotOnlineRecordsWriter") << " z                 : " << z;
0159   edm::LogPrint("BeamSpotOnlineRecordsWriter") << " sigmaZ            : " << sigmaZ;
0160   edm::LogPrint("BeamSpotOnlineRecordsWriter") << " dxdz              : " << dxdz;
0161   edm::LogPrint("BeamSpotOnlineRecordsWriter") << " dydz              : " << dydz;
0162   edm::LogPrint("BeamSpotOnlineRecordsWriter") << " beamWidthX        : " << beamWidthX;
0163   edm::LogPrint("BeamSpotOnlineRecordsWriter") << " beamWidthY        : " << beamWidthY;
0164   edm::LogPrint("BeamSpotOnlineRecordsWriter")
0165       << " Cov(0,j)          : " << cov[0][0] << " " << cov[0][1] << " " << cov[0][2] << " " << cov[0][3] << " "
0166       << cov[0][4] << " " << cov[0][5] << " " << cov[0][6];
0167   edm::LogPrint("BeamSpotOnlineRecordsWriter")
0168       << " Cov(1,j)          : " << cov[1][0] << " " << cov[1][1] << " " << cov[1][2] << " " << cov[1][3] << " "
0169       << cov[1][4] << " " << cov[1][5] << " " << cov[1][6];
0170   edm::LogPrint("BeamSpotOnlineRecordsWriter")
0171       << " Cov(2,j)          : " << cov[2][0] << " " << cov[2][1] << " " << cov[2][2] << " " << cov[2][3] << " "
0172       << cov[2][4] << " " << cov[2][5] << " " << cov[2][6];
0173   edm::LogPrint("BeamSpotOnlineRecordsWriter")
0174       << " Cov(3,j)          : " << cov[3][0] << " " << cov[3][1] << " " << cov[3][2] << " " << cov[3][3] << " "
0175       << cov[3][4] << " " << cov[3][5] << " " << cov[3][6];
0176   edm::LogPrint("BeamSpotOnlineRecordsWriter")
0177       << " Cov(4,j)          : " << cov[4][0] << " " << cov[4][1] << " " << cov[4][2] << " " << cov[4][3] << " "
0178       << cov[4][4] << " " << cov[4][5] << " " << cov[4][6];
0179   edm::LogPrint("BeamSpotOnlineRecordsWriter")
0180       << " Cov(5,j)          : " << cov[5][0] << " " << cov[5][1] << " " << cov[5][2] << " " << cov[5][3] << " "
0181       << cov[5][4] << " " << cov[5][5] << " " << cov[5][6];
0182   edm::LogPrint("BeamSpotOnlineRecordsWriter")
0183       << " Cov(6,j)          : " << cov[6][0] << " " << cov[6][1] << " " << cov[6][2] << " " << cov[6][3] << " "
0184       << cov[6][4] << " " << cov[6][5] << " " << cov[6][6];
0185   edm::LogPrint("BeamSpotOnlineRecordsWriter") << " emittanceX        : " << emittanceX;
0186   edm::LogPrint("BeamSpotOnlineRecordsWriter") << " emittanceY        : " << emittanceY;
0187   edm::LogPrint("BeamSpotOnlineRecordsWriter") << " betastar          : " << betastar;
0188   edm::LogPrint("BeamSpotOnlineRecordsWriter") << "-----------------------------------------------------";
0189 
0190   BeamSpotOnlineObjects abeam;
0191 
0192   abeam.setLastAnalyzedLumi(lastAnalyzedLumi);
0193   abeam.setLastAnalyzedRun(lastAnalyzedRun);
0194   abeam.setLastAnalyzedFill(lastAnalyzedFill);
0195   abeam.setStartTimeStamp(lumiRangeBeginTime);
0196   abeam.setEndTimeStamp(lumiRangeEndTime);
0197   abeam.setType(type);
0198   abeam.setPosition(x, y, z);
0199   abeam.setSigmaZ(sigmaZ);
0200   abeam.setdxdz(dxdz);
0201   abeam.setdydz(dydz);
0202   abeam.setBeamWidthX(beamWidthX);
0203   abeam.setBeamWidthY(beamWidthY);
0204   abeam.setEmittanceX(emittanceX);
0205   abeam.setEmittanceY(emittanceY);
0206   abeam.setBetaStar(betastar);
0207 
0208   for (int i = 0; i < 7; ++i) {
0209     for (int j = 0; j < 7; ++j) {
0210       abeam.setCovariance(i, j, cov[i][j]);
0211     }
0212   }
0213 
0214   // Set the creation time of the payload to the current time
0215   auto creationTime =
0216       std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
0217   abeam.setCreationTime(creationTime);
0218 
0219   edm::LogPrint("BeamSpotOnlineRecordsWriter") << " Writing results to DB...";
0220 
0221   edm::Service<cond::service::PoolDBOutputService> poolDbService;
0222   if (poolDbService.isAvailable()) {
0223     edm::LogPrint("BeamSpotOnlineRecordsWriter") << "poolDBService available";
0224     if (poolDbService->isNewTagRequest(fLabel)) {
0225       edm::LogPrint("BeamSpotOnlineRecordsWriter") << "new tag requested";
0226       if (fuseNewSince) {
0227         edm::LogPrint("BeamSpotOnlineRecordsWriter") << "Using a new Since: " << fnewSince;
0228         poolDbService->createOneIOV<BeamSpotOnlineObjects>(abeam, fnewSince, fLabel);
0229       } else
0230         poolDbService->createOneIOV<BeamSpotOnlineObjects>(abeam, poolDbService->beginOfTime(), fLabel);
0231     } else {
0232       edm::LogPrint("BeamSpotOnlineRecordsWriter") << "no new tag requested";
0233       if (fuseNewSince) {
0234         edm::LogPrint("BeamSpotOnlineRecordsWriter") << "Using a new Since: " << fnewSince;
0235         poolDbService->appendOneIOV<BeamSpotOnlineObjects>(abeam, fnewSince, fLabel);
0236       } else
0237         poolDbService->appendOneIOV<BeamSpotOnlineObjects>(abeam, poolDbService->currentTime(), fLabel);
0238     }
0239   }
0240   edm::LogPrint("BeamSpotOnlineRecordsWriter") << "[BeamSpotOnlineRecordsWriter] endJob done \n";
0241 }
0242 
0243 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0244 void BeamSpotOnlineRecordsWriter::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0245   edm::ParameterSetDescription desc;
0246   desc.add<bool>("isHLT", true);
0247   desc.addUntracked<std::string>("InputFileName", "");
0248   desc.addOptionalUntracked<uint32_t>("IOVStartRun", 1);
0249   desc.addOptionalUntracked<uint32_t>("IOVStartLumi", 1);
0250   descriptions.addWithDefaultLabel(desc);
0251 }
0252 
0253 //define this as a plug-in
0254 DEFINE_FWK_MODULE(BeamSpotOnlineRecordsWriter);