Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:03:28

0001 // system include files
0002 #include <iostream>
0003 #include <cstdio>
0004 #include <sys/time.h>
0005 
0006 // user include files
0007 #include "CalibFormats/SiStripObjects/interface/SiStripGain.h"
0008 #include "CalibTracker/Records/interface/SiStripGainRcd.h"
0009 #include "CommonTools/UtilAlgos/interface/TFileService.h"
0010 #include "CondFormats/DataRecord/interface/SiStripApvGainRcd.h"
0011 #include "CondFormats/SiStripObjects/interface/SiStripApvGain.h"
0012 #include "FWCore/Framework/interface/Event.h"
0013 #include "FWCore/Framework/interface/EventSetup.h"
0014 #include "FWCore/Framework/interface/Frameworkfwd.h"
0015 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0016 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0017 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0018 #include "FWCore/ServiceRegistry/interface/Service.h"
0019 
0020 // root objects
0021 #include "TROOT.h"
0022 #include "TSystem.h"
0023 #include "TFile.h"
0024 #include "TDirectory.h"
0025 #include "TTree.h"
0026 
0027 class SiStripApvGainReader : public edm::one::EDAnalyzer<edm::one::SharedResources> {
0028 public:
0029   explicit SiStripApvGainReader(const edm::ParameterSet&);
0030   ~SiStripApvGainReader() override = default;
0031 
0032   void analyze(const edm::Event&, const edm::EventSetup&) override;
0033 
0034 private:
0035   // initializers list
0036   const bool printdebug_;
0037   const std::string formatedOutput_;
0038   const uint32_t gainType_;
0039   const edm::ESGetToken<SiStripGain, SiStripGainRcd> gainToken_;
0040   edm::Service<TFileService> fs_;
0041   TTree* tree_ = nullptr;
0042   int id_ = 0, detId_ = 0, apvId_ = 0;
0043   double gain_ = 0;
0044 };
0045 
0046 using namespace cms;
0047 
0048 SiStripApvGainReader::SiStripApvGainReader(const edm::ParameterSet& iConfig)
0049     : printdebug_(iConfig.getUntrackedParameter<bool>("printDebug", true)),
0050       formatedOutput_(iConfig.getUntrackedParameter<std::string>("outputFile", "")),
0051       gainType_(iConfig.getUntrackedParameter<uint32_t>("gainType", 1)),
0052       gainToken_(esConsumes()) {
0053   usesResource(TFileService::kSharedResource);
0054 
0055   if (fs_.isAvailable()) {
0056     tree_ = fs_->make<TTree>("Gains", "Gains");
0057 
0058     tree_->Branch("Index", &id_, "Index/I");
0059     tree_->Branch("DetId", &detId_, "DetId/I");
0060     tree_->Branch("APVId", &apvId_, "APVId/I");
0061     tree_->Branch("Gain", &gain_, "Gain/D");
0062   }
0063 }
0064 
0065 void SiStripApvGainReader::analyze(const edm::Event& e, const edm::EventSetup& iSetup) {
0066   const auto& stripApvGain = iSetup.getData(gainToken_);
0067   edm::LogInfo("SiStripApvGainReader") << "[SiStripApvGainReader::analyze] End Reading SiStripApvGain" << std::endl;
0068   std::vector<uint32_t> detid;
0069   stripApvGain.getDetIds(detid);
0070   edm::LogInfo("Number of detids ") << detid.size() << std::endl;
0071 
0072   FILE* pFile = nullptr;
0073   if (!formatedOutput_.empty())
0074     pFile = fopen(formatedOutput_.c_str(), "w");
0075   for (size_t id = 0; id < detid.size(); id++) {
0076     SiStripApvGain::Range range = stripApvGain.getRange(detid[id], gainType_);
0077     if (printdebug_) {
0078       int apv = 0;
0079       for (int it = 0; it < range.second - range.first; it++) {
0080         edm::LogInfo("SiStripApvGainReader")
0081             << "detid " << detid[id] << " \t " << apv++ << " \t " << stripApvGain.getApvGain(it, range) << std::endl;
0082         id_++;
0083 
0084         if (tree_) {
0085           detId_ = detid[id];
0086           apvId_ = apv;
0087           gain_ = stripApvGain.getApvGain(it, range);
0088           tree_->Fill();
0089         }
0090       }
0091     }
0092 
0093     if (pFile) {
0094       fprintf(pFile, "%i ", detid[id]);
0095       for (int it = 0; it < range.second - range.first; it++) {
0096         fprintf(pFile, "%f ", stripApvGain.getApvGain(it, range));
0097       }
0098       fprintf(pFile, "\n");
0099     }
0100   }
0101 
0102   if (pFile)
0103     fclose(pFile);
0104 }
0105 
0106 #include "FWCore/PluginManager/interface/ModuleDef.h"
0107 #include "FWCore/Framework/interface/MakerMacros.h"
0108 
0109 DEFINE_FWK_MODULE(SiStripApvGainReader);