Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-12-14 11:45:28

0001 /**_________________________________________________________________
0002    class:   BeamSpotWrite2DB.h
0003    package: RecoVertex/BeamSpotProducer
0004 
0005    author: Francisco Yumiceva, Fermilab (yumiceva@fnal.gov)
0006 ________________________________________________________________**/
0007 
0008 // C++ standard
0009 #include <string>
0010 #include <fstream>
0011 
0012 // CMS
0013 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
0014 #include "CondFormats/BeamSpotObjects/interface/BeamSpotObjects.h"
0015 #include "DataFormats/BeamSpot/interface/BeamSpot.h"
0016 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0017 #include "FWCore/Framework/interface/Event.h"
0018 #include "FWCore/Framework/interface/Frameworkfwd.h"
0019 #include "FWCore/Framework/interface/MakerMacros.h"
0020 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0021 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0022 #include "FWCore/ServiceRegistry/interface/Service.h"
0023 #include "RecoVertex/BeamSpotProducer/interface/BSFitter.h"
0024 #include "RecoVertex/BeamSpotProducer/interface/BSTrkParameters.h"
0025 #include "RecoVertex/BeamSpotProducer/interface/BeamSpotWrite2DB.h"
0026 
0027 // ROOT
0028 #include "TFile.h"
0029 #include "TTree.h"
0030 
0031 class BeamSpotWrite2DB : public edm::one::EDAnalyzer<edm::one::SharedResources> {
0032 public:
0033   explicit BeamSpotWrite2DB(const edm::ParameterSet&);
0034   ~BeamSpotWrite2DB() override;
0035   static void fillDescriptions(edm::ConfigurationDescriptions&);
0036 
0037 private:
0038   void analyze(const edm::Event&, const edm::EventSetup&) override;
0039   void endJob() override;
0040 
0041   std::ifstream fasciiFile;
0042   std::string fasciiFileName;
0043 };
0044 
0045 BeamSpotWrite2DB::BeamSpotWrite2DB(const edm::ParameterSet& iConfig) {
0046   usesResource("PoolDBOutputService");
0047   fasciiFileName = iConfig.getUntrackedParameter<std::string>("OutputFileName");
0048   if (!fasciiFileName.empty()) {
0049     fasciiFile.open(fasciiFileName.c_str());
0050   } else {
0051     throw cms::Exception("Inconsistent Data") << " expected input file name is null\n";
0052   }
0053 }
0054 
0055 BeamSpotWrite2DB::~BeamSpotWrite2DB() = default;
0056 
0057 void BeamSpotWrite2DB::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {}
0058 
0059 void BeamSpotWrite2DB::endJob() {
0060   edm::LogPrint("BeamSpotWrite2DB") << " Read beam spot data from text file: " << fasciiFileName;
0061   edm::LogPrint("BeamSpotWrite2DB") << " please see plugins/BeamSpotWrite2DB.cc for format of text file.";
0062 
0063   edm::LogInfo("BeamSpotWrite2DB")
0064       << " Content of the file is expected to have this format with the first column as a keyword:";
0065   edm::LogInfo("BeamSpotWrite2DB") << " x\n y\n z\n sigmaZ\n dxdz\n dydz\n beamWidthX\n beamWidthY";
0066   for (int i = 0; i < 7; i++) {
0067     for (int j = 0; j < 7; j++) {
0068       edm::LogInfo("BeamSpotWrite2DB") << " cov[" << i << "][" << j << "] cov[" << i << "][" << j << "] cov[" << i
0069                                        << "][" << j << "] cov[" << i << "][" << j << "] cov[" << i << "][" << j
0070                                        << "] cov[" << j << "][" << j << "] cov[" << i << "][" << j << "]";
0071     }
0072   }
0073 
0074   // extract from file
0075   double x, y, z, sigmaZ, dxdz, dydz, beamWidthX, beamWidthY, emittanceX, emittanceY, betastar;
0076   std::string tag;
0077   double cov[7][7];
0078   int type;
0079 
0080   fasciiFile >> tag >> type;
0081   fasciiFile >> tag >> x;
0082   fasciiFile >> tag >> y;
0083   fasciiFile >> tag >> z;
0084   fasciiFile >> tag >> sigmaZ;
0085   fasciiFile >> tag >> dxdz;
0086   fasciiFile >> tag >> dydz;
0087   fasciiFile >> tag >> beamWidthX;
0088   fasciiFile >> tag >> beamWidthY;
0089   fasciiFile >> tag >> cov[0][0] >> cov[0][1] >> cov[0][2] >> cov[0][3] >> cov[0][4] >> cov[0][5] >> cov[0][6];
0090   fasciiFile >> tag >> cov[1][0] >> cov[1][1] >> cov[1][2] >> cov[1][3] >> cov[1][4] >> cov[1][5] >> cov[1][6];
0091   fasciiFile >> tag >> cov[2][0] >> cov[2][1] >> cov[2][2] >> cov[2][3] >> cov[2][4] >> cov[2][5] >> cov[2][6];
0092   fasciiFile >> tag >> cov[3][0] >> cov[3][1] >> cov[3][2] >> cov[3][3] >> cov[3][4] >> cov[3][5] >> cov[3][6];
0093   fasciiFile >> tag >> cov[4][0] >> cov[4][1] >> cov[4][2] >> cov[4][3] >> cov[4][4] >> cov[4][5] >> cov[4][6];
0094   fasciiFile >> tag >> cov[5][0] >> cov[5][1] >> cov[5][2] >> cov[5][3] >> cov[5][4] >> cov[5][5] >> cov[5][6];
0095   fasciiFile >> tag >> cov[6][0] >> cov[6][1] >> cov[6][2] >> cov[6][3] >> cov[6][4] >> cov[6][5] >> cov[6][6];
0096   fasciiFile >> tag >> emittanceX;
0097   fasciiFile >> tag >> emittanceY;
0098   fasciiFile >> tag >> betastar;
0099 
0100   BeamSpotObjects abeam;
0101 
0102   abeam.setType(type);
0103   abeam.setPosition(x, y, z);
0104   abeam.setSigmaZ(sigmaZ);
0105   abeam.setdxdz(dxdz);
0106   abeam.setdydz(dydz);
0107   abeam.setBeamWidthX(beamWidthX);
0108   abeam.setBeamWidthY(beamWidthY);
0109   abeam.setEmittanceX(emittanceX);
0110   abeam.setEmittanceY(emittanceY);
0111   abeam.setBetaStar(betastar);
0112 
0113   for (int i = 0; i < 7; ++i) {
0114     for (int j = 0; j < 7; ++j) {
0115       abeam.setCovariance(i, j, cov[i][j]);
0116     }
0117   }
0118 
0119   edm::LogPrint("BeamSpotWrite2DB") << " write results to DB...";
0120 
0121   edm::Service<cond::service::PoolDBOutputService> poolDbService;
0122   if (poolDbService.isAvailable()) {
0123     edm::LogPrint("BeamSpotWrite2DB") << "poolDBService available";
0124     if (poolDbService->isNewTagRequest("BeamSpotObjectsRcd")) {
0125       edm::LogPrint("BeamSpotWrite2DB") << "new tag requested";
0126       poolDbService->createOneIOV<BeamSpotObjects>(abeam, poolDbService->beginOfTime(), "BeamSpotObjectsRcd");
0127     } else {
0128       edm::LogPrint("BeamSpotWrite2DB") << "no new tag requested";
0129       poolDbService->appendOneIOV<BeamSpotObjects>(abeam, poolDbService->currentTime(), "BeamSpotObjectsRcd");
0130     }
0131   }
0132   edm::LogPrint("BeamSpotWrite2DB") << "[BeamSpotWrite2DB] endJob done \n";
0133 }
0134 
0135 void BeamSpotWrite2DB::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0136   edm::ParameterSetDescription desc;
0137   desc.setComment(
0138       "Writes out a DB file containing a BeamSpotObjects payload, according to parameters defined in ASCII file");
0139   desc.addUntracked<std::string>("OutputFileName", {});
0140   descriptions.addWithDefaultLabel(desc);
0141 }
0142 
0143 //define this as a plug-in
0144 DEFINE_FWK_MODULE(BeamSpotWrite2DB);