Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-12-14 11:44:42

0001 
0002 /*
0003  *  See header file for a description of this class.
0004  *
0005  *  \author L. Uplegger F. Yumiceva - Fermilab
0006  */
0007 
0008 #include "Calibration/TkAlCaRecoProducers/interface/AlcaBeamSpotHarvester.h"
0009 #include "DataFormats/Common/interface/Handle.h"
0010 #include "FWCore/Framework/interface/ConsumesCollector.h"
0011 #include "FWCore/Framework/interface/Event.h"
0012 #include "FWCore/Framework/interface/EventSetup.h"
0013 #include "FWCore/Framework/interface/FileBlock.h"
0014 #include "FWCore/Framework/interface/LuminosityBlock.h"
0015 #include "FWCore/Framework/interface/MakerMacros.h"
0016 #include "FWCore/Framework/interface/Run.h"
0017 #include "FWCore/MessageLogger/interface/JobReport.h"
0018 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0019 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0020 
0021 #include "DataFormats/BeamSpot/interface/BeamSpot.h"
0022 
0023 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
0024 #include "CondFormats/BeamSpotObjects/interface/BeamSpotObjects.h"
0025 #include "FWCore/ServiceRegistry/interface/Service.h"
0026 //#include "CondCore/Utilities/bin/cmscond_export_iov.cpp"
0027 //#include "CondCore/Utilities/interface/Utilities.h"
0028 // #include "FWCore/MessageLogger/interface/JobReport.h"
0029 
0030 #include <cstring>
0031 #include <iostream>
0032 
0033 using namespace edm;
0034 using namespace reco;
0035 // using namespace std;
0036 
0037 //--------------------------------------------------------------------------------------------------
0038 AlcaBeamSpotHarvester::AlcaBeamSpotHarvester(const edm::ParameterSet &iConfig)
0039     : beamSpotOutputBase_(iConfig.getParameter<ParameterSet>("AlcaBeamSpotHarvesterParameters")
0040                               .getUntrackedParameter<std::string>("BeamSpotOutputBase")),
0041       outputrecordName_(iConfig.getParameter<ParameterSet>("AlcaBeamSpotHarvesterParameters")
0042                             .getUntrackedParameter<std::string>("outputRecordName", "BeamSpotObjectsRcd")),
0043       sigmaZValue_(iConfig.getParameter<ParameterSet>("AlcaBeamSpotHarvesterParameters")
0044                        .getUntrackedParameter<double>("SigmaZValue")),
0045       sigmaZCut_(iConfig.getParameter<ParameterSet>("AlcaBeamSpotHarvesterParameters")
0046                      .getUntrackedParameter<double>("SigmaZCut")),
0047       dumpTxt_(
0048           iConfig.getParameter<ParameterSet>("AlcaBeamSpotHarvesterParameters").getUntrackedParameter<bool>("DumpTxt")),
0049       outTxtFileName_(iConfig.getParameter<ParameterSet>("AlcaBeamSpotHarvesterParameters")
0050                           .getUntrackedParameter<std::string>("TxtFileName")),
0051       theAlcaBeamSpotManager_(iConfig, consumesCollector()) {}
0052 
0053 //--------------------------------------------------------------------------------------------------
0054 void AlcaBeamSpotHarvester::analyze(const edm::Event &iEvent, const edm::EventSetup &) {
0055   //  edm::LogInfo("AlcaBeamSpotHarvester")
0056   //      << "Lumi: " << iEvent.luminosityBlock()
0057   //      << " Time: " << iEvent.time().unixTime()
0058   //      << std::endl;
0059 }
0060 
0061 //--------------------------------------------------------------------------------------------------
0062 void AlcaBeamSpotHarvester::beginRun(const edm::Run &, const edm::EventSetup &) { theAlcaBeamSpotManager_.reset(); }
0063 
0064 //--------------------------------------------------------------------------------------------------
0065 void AlcaBeamSpotHarvester::endRun(const edm::Run &iRun, const edm::EventSetup &) {
0066   theAlcaBeamSpotManager_.createWeightedPayloads();
0067   std::map<edm::LuminosityBlockNumber_t, std::pair<edm::Timestamp, reco::BeamSpot>> beamSpotMap =
0068       theAlcaBeamSpotManager_.getPayloads();
0069   Service<cond::service::PoolDBOutputService> poolDbService;
0070   //  cond::ExportIOVUtilities utilities;
0071 
0072   std::string outTxt = Form("%s_Run%d.txt", outTxtFileName_.c_str(), iRun.id().run());
0073   std::ofstream outFile;
0074   outFile.open(outTxt.c_str(), std::ios::app);
0075 
0076   if (poolDbService.isAvailable()) {
0077     for (AlcaBeamSpotManager::bsMap_iterator it = beamSpotMap.begin(); it != beamSpotMap.end(); it++) {
0078       BeamSpotObjects aBeamSpot;
0079       aBeamSpot.setType(it->second.second.type());
0080       aBeamSpot.setPosition(it->second.second.x0(), it->second.second.y0(), it->second.second.z0());
0081       if (sigmaZValue_ == -1) {
0082         aBeamSpot.setSigmaZ(it->second.second.sigmaZ());
0083       } else {
0084         aBeamSpot.setSigmaZ(sigmaZValue_);
0085       }
0086       aBeamSpot.setdxdz(it->second.second.dxdz());
0087       aBeamSpot.setdydz(it->second.second.dydz());
0088       aBeamSpot.setBeamWidthX(it->second.second.BeamWidthX());
0089       aBeamSpot.setBeamWidthY(it->second.second.BeamWidthY());
0090       aBeamSpot.setEmittanceX(it->second.second.emittanceX());
0091       aBeamSpot.setEmittanceY(it->second.second.emittanceY());
0092       aBeamSpot.setBetaStar(it->second.second.betaStar());
0093 
0094       for (int i = 0; i < 7; ++i) {
0095         for (int j = 0; j < 7; ++j) {
0096           aBeamSpot.setCovariance(i, j, it->second.second.covariance(i, j));
0097         }
0098       }
0099 
0100       if (sigmaZValue_ > 0) {
0101         aBeamSpot.setCovariance(3, 3, 0.000025);
0102       }
0103 
0104       cond::Time_t thisIOV = 1;
0105 
0106       beamspot::BeamSpotContainer currentBS;
0107 
0108       // run based
0109       if (beamSpotOutputBase_ == "runbased") {
0110         thisIOV = (cond::Time_t)iRun.id().run();
0111       }
0112       // lumi based
0113       else if (beamSpotOutputBase_ == "lumibased") {
0114         edm::LuminosityBlockID lu(iRun.id().run(), it->first);
0115         thisIOV = (cond::Time_t)(lu.value());
0116 
0117         currentBS.beamspot = it->second.second;
0118         currentBS.run = iRun.id().run();
0119         currentBS.beginLumiOfFit = it->first;
0120         currentBS.endLumiOfFit = it->first;  // endLumi = initLumi
0121 
0122         std::time_t lumi_t_begin = it->second.first.unixTime();
0123         std::time_t lumi_t_end = it->second.first.unixTime();  // begin time == end time
0124         strftime(
0125             currentBS.beginTimeOfFit, sizeof currentBS.beginTimeOfFit, "%Y.%m.%d %H:%M:%S GMT", gmtime(&lumi_t_begin));
0126         strftime(currentBS.endTimeOfFit, sizeof currentBS.endTimeOfFit, "%Y.%m.%d %H:%M:%S GMT", gmtime(&lumi_t_end));
0127 
0128         currentBS.reftime[0] = lumi_t_begin;
0129         currentBS.reftime[1] = lumi_t_end;
0130       }
0131       if (poolDbService->isNewTagRequest(outputrecordName_)) {
0132         edm::LogInfo("AlcaBeamSpotHarvester") << "new tag requested" << std::endl;
0133         poolDbService->writeOneIOV<BeamSpotObjects>(aBeamSpot, thisIOV, outputrecordName_);
0134         if (dumpTxt_ && beamSpotOutputBase_ == "lumibased") {
0135           beamspot::dumpBeamSpotTxt(outFile, currentBS);
0136 
0137           edm::Service<edm::JobReport> jr;
0138           if (jr.isAvailable()) {
0139             std::map<std::string, std::string> jrInfo;
0140             jrInfo["Source"] = std::string("AlcaHarvesting");
0141             jrInfo["FileClass"] = std::string("ALCATXT");
0142             jr->reportAnalysisFile(outTxt, jrInfo);
0143           }
0144         }
0145       } else {
0146         edm::LogInfo("AlcaBeamSpotHarvester") << "no new tag requested, appending IOV" << std::endl;
0147         poolDbService->writeOneIOV<BeamSpotObjects>(aBeamSpot, thisIOV, outputrecordName_);
0148         if (dumpTxt_ && beamSpotOutputBase_ == "lumibased") {
0149           beamspot::dumpBeamSpotTxt(outFile, currentBS);
0150         }
0151       }
0152 
0153       /*
0154             int         argc = 15;
0155             const char* argv[] = {"endRun"
0156                                  ,"-d","sqlite_file:combined.db"
0157                                  ,"-s","sqlite_file:testbs2.db"
0158                                  ,"-l","sqlite_file:log.db"
0159                                  ,"-i","TestLSBasedBS"
0160                                  ,"-t","TestLSBasedBS"
0161                                  ,"-b","1"
0162                                  ,"-e","10"
0163                                  };
0164 
0165             edm::LogInfo("AlcaBeamSpotHarvester")
0166               << "Running utilities!"
0167               << utilities.run(argc,(char**)argv);
0168             edm::LogInfo("AlcaBeamSpotHarvester")
0169               << "Run utilities!"
0170               << std::endl;
0171       */
0172     }
0173   }
0174 
0175   outFile.close();
0176 }
0177 
0178 //--------------------------------------------------------------------------------------------------
0179 void AlcaBeamSpotHarvester::beginLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &) {}
0180 
0181 //--------------------------------------------------------------------------------------------------
0182 void AlcaBeamSpotHarvester::endLuminosityBlock(const edm::LuminosityBlock &iLumi, const edm::EventSetup &) {
0183   theAlcaBeamSpotManager_.readLumi(iLumi);
0184 }
0185 
0186 DEFINE_FWK_MODULE(AlcaBeamSpotHarvester);