Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-09-07 04:35:47

0001 // -*- C++ -*-
0002 //
0003 // Package:    CondTools/SiStrip
0004 // Class:      SiStripDB2Tree
0005 //
0006 /**\class SiStripDB2Tree SiStripDB2Tree.cc CondTools/SiStrip/plugins/SiStripDB2Tree.cc
0007 
0008  Description:
0009      Converts several SiStrip Payloads (Noise, Peds, APVGain, etc.) into a TTree
0010 */
0011 //
0012 // Original Author:  Mauro Verzetti
0013 // Modified by:      Marco Musich
0014 //
0015 
0016 // system include files
0017 #include <map>
0018 #include <memory>
0019 #include <string>
0020 #include <unordered_map>
0021 #include <vector>
0022 
0023 // user include files
0024 #include "CalibFormats/SiStripObjects/interface/SiStripQuality.h"
0025 #include "CalibTracker/Records/interface/SiStripQualityRcd.h"
0026 #include "CalibTracker/SiStripCommon/interface/SiStripDetInfoFileReader.h"
0027 #include "CommonTools/UtilAlgos/interface/TFileService.h"
0028 #include "CondFormats/DataRecord/interface/SiStripApvGainRcd.h"
0029 #include "CondFormats/DataRecord/interface/SiStripCondDataRecords.h"
0030 #include "CondFormats/DataRecord/interface/SiStripNoisesRcd.h"
0031 #include "CondFormats/DataRecord/interface/SiStripPedestalsRcd.h"
0032 #include "CondFormats/SiStripObjects/interface/SiStripApvGain.h"
0033 #include "CondFormats/SiStripObjects/interface/SiStripLatency.h"
0034 #include "CondFormats/SiStripObjects/interface/SiStripNoises.h"
0035 #include "CondFormats/SiStripObjects/interface/SiStripPedestals.h"
0036 #include "DataFormats/Common/interface/DetSetNew.h"
0037 #include "DataFormats/Common/interface/DetSetVectorNew.h"
0038 #include "DataFormats/SiStripCommon/interface/ConstantsForHardwareSystems.h" /* for STRIPS_PER_APV*/
0039 #include "DataFormats/SiStripDetId/interface/SiStripDetId.h"
0040 #include "DataFormats/TrackerCommon/interface/TrackerTopology.h"
0041 #include "FWCore/Framework/interface/Event.h"
0042 #include "FWCore/Framework/interface/EventSetup.h"
0043 #include "FWCore/Framework/interface/Frameworkfwd.h"
0044 #include "FWCore/Framework/interface/MakerMacros.h"
0045 #include "FWCore/Framework/interface/Run.h"
0046 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0047 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0048 #include "FWCore/ServiceRegistry/interface/Service.h"
0049 #include "Geometry/Records/interface/TrackerTopologyRcd.h"
0050 
0051 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0052 #define LOGERROR(x) edm::LogError(x)
0053 #define LOGINFO(x) edm::LogInfo(x)
0054 #define LOGDEBUG(x) LogDebug(x)
0055 
0056 // ROOT includes
0057 #include "TNamed.h"
0058 #include "TObjString.h"
0059 #include "TText.h"
0060 #include "TTree.h"
0061 
0062 //**********************************************//
0063 // Auxilliary class
0064 //**********************************************//
0065 class RecordInfo : public TNamed {
0066 public:
0067   RecordInfo(const char* record, const char* tag) : TNamed(record, tag) {}
0068 
0069   void printInfo() const { LOGINFO("RecordInfo") << GetName() << " " << GetTitle(); }
0070 
0071   const char* getRecord() { return this->GetName(); }
0072   const char* getIOVSince() { return this->GetTitle(); }
0073 };
0074 
0075 class SiStripDB2Tree : public edm::one::EDAnalyzer<edm::one::SharedResources, edm::one::WatchRuns> {
0076 public:
0077   explicit SiStripDB2Tree(const edm::ParameterSet&);
0078   ~SiStripDB2Tree() override = default;
0079 
0080   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0081 
0082 private:
0083   void beginRun(edm::Run const&, edm::EventSetup const&) override;
0084   void endRun(edm::Run const&, edm::EventSetup const&) override {}
0085   void analyze(const edm::Event&, const edm::EventSetup&) override;
0086   void setTopoInfo(uint32_t detId, const TrackerTopology* tTopo);
0087   template <class Rcd>
0088   std::pair<const char*, std::string> getRecordInfo(const edm::EventSetup& iSetup) const;
0089 
0090   // ----------member data ---------------------------
0091 
0092   // ES tokens
0093   const edm::ESGetToken<SiStripPedestals, SiStripPedestalsRcd> pedToken_;
0094   const edm::ESGetToken<SiStripNoises, SiStripNoisesRcd> noiseToken_;
0095   const edm::ESGetToken<SiStripApvGain, SiStripApvGainRcd> g1Token_;
0096   const edm::ESGetToken<SiStripApvGain, SiStripApvGain2Rcd> g2Token_;
0097   const edm::ESGetToken<SiStripQuality, SiStripQualityRcd> qualToken_;
0098   const edm::ESGetToken<SiStripApvGain, SiStripApvGainSimRcd> gsimToken_;
0099   const edm::ESGetToken<SiStripLatency, SiStripLatencyRcd> latToken_;
0100   const edm::ESGetToken<TrackerTopology, TrackerTopologyRcd> topoToken_;
0101 
0102   const bool isMC_;
0103 
0104   TTree* tree_;
0105   std::string processGT_;
0106 
0107   //branches
0108   uint32_t detId_, ring_, istrip_, det_type_;
0109   Int_t layer_, side_, subdetId_;
0110   float noise_, gsim_, g1_, g2_, lenght_, pedestal_;
0111   bool isTIB_, isTOB_, isTEC_, isTID_, isBad_;
0112   TText* text_;
0113 
0114   // detInfo
0115   SiStripDetInfo detInfo_;
0116 };
0117 
0118 //
0119 // constructors and destructor
0120 //
0121 SiStripDB2Tree::SiStripDB2Tree(const edm::ParameterSet& iConfig)
0122     : pedToken_(esConsumes()),
0123       noiseToken_(esConsumes()),
0124       g1Token_(esConsumes()),
0125       g2Token_(esConsumes()),
0126       qualToken_(esConsumes(edm::ESInputTag("", iConfig.getParameter<std::string>("StripQualityLabel")))),
0127       gsimToken_(esConsumes()),
0128       latToken_(esConsumes()),
0129       topoToken_(esConsumes()),
0130       isMC_(iConfig.getUntrackedParameter<bool>("isMC", false)),
0131       detId_(0),
0132       ring_(0),
0133       istrip_(0),
0134       det_type_(0),
0135       layer_(0),
0136       side_(0),
0137       subdetId_(0),
0138       noise_(0),
0139       gsim_(0),
0140       g1_(0),
0141       g2_(0),
0142       lenght_(0),
0143       isTIB_(false),
0144       isTOB_(false),
0145       isTEC_(false),
0146       isTID_(false) {
0147   usesResource(TFileService::kSharedResource);
0148 
0149   edm::Service<TFileService> fs;
0150   //now do what ever initialization is needed
0151   text_ = fs->make<TText>(0., 0., "");
0152   text_->SetName("RunMode");
0153   tree_ = fs->make<TTree>("StripDBTree", "Tree with DB SiStrip info");
0154 
0155   tree_->Branch("detId", &detId_, "detId/i");
0156   tree_->Branch("detType", &det_type_, "detType/i");
0157   tree_->Branch("noise", &noise_, "noise/F");
0158   tree_->Branch("pedestal", &pedestal_, "pedestal/F");
0159   tree_->Branch("istrip", &istrip_, "istrip/i");
0160   tree_->Branch("gsim", &gsim_, "gsim/F");
0161   tree_->Branch("g1", &g1_, "g1/F");
0162   tree_->Branch("g2", &g2_, "g2/F");
0163   tree_->Branch("layer", &layer_, "layer/I");
0164   tree_->Branch("side", &side_, "side/I");
0165   tree_->Branch("subdetId", &subdetId_, "subdetId/I");
0166   tree_->Branch("ring", &ring_, "ring/i");
0167   tree_->Branch("length", &lenght_, "length/F");
0168   tree_->Branch("isBad", &isBad_, "isBad/O");
0169   tree_->Branch("isTIB", &isTIB_, "isTIB/O");
0170   tree_->Branch("isTOB", &isTOB_, "isTOB/O");
0171   tree_->Branch("isTEC", &isTEC_, "isTEC/O");
0172   tree_->Branch("isTID", &isTID_, "isTID/O");
0173 
0174   detInfo_ = SiStripDetInfoFileReader::read(edm::FileInPath(SiStripDetInfoFileReader::kDefaultFile).fullPath());
0175 }
0176 
0177 //
0178 // member functions
0179 //
0180 
0181 void SiStripDB2Tree::setTopoInfo(uint32_t detId, const TrackerTopology* tTopo) {
0182   subdetId_ = DetId(detId).subdetId();
0183   switch (subdetId_) {
0184     case SiStripSubdetector::TIB:  //TIB
0185       isTIB_ = true;
0186       isTOB_ = false;
0187       isTEC_ = false;
0188       isTID_ = false;
0189       layer_ = tTopo->tibLayer(detId);
0190       side_ = 0;
0191       ring_ = 0;
0192       break;
0193     case SiStripSubdetector::TID:  //TID
0194       isTIB_ = false;
0195       isTOB_ = false;
0196       isTEC_ = false;
0197       isTID_ = true;
0198       side_ = tTopo->tidSide(detId);
0199       layer_ = tTopo->tidWheel(detId);
0200       ring_ = 0;
0201       break;
0202     case SiStripSubdetector::TOB:  //TOB
0203       isTIB_ = false;
0204       isTOB_ = true;
0205       isTEC_ = false;
0206       isTID_ = false;
0207       layer_ = tTopo->tobLayer(detId);
0208       side_ = 0;
0209       ring_ = 0;
0210       break;
0211     case SiStripSubdetector::TEC:  //TEC
0212       isTIB_ = false;
0213       isTOB_ = false;
0214       isTEC_ = true;
0215       isTID_ = false;
0216       side_ = tTopo->tecSide(detId);
0217       layer_ = tTopo->tecWheel(detId);
0218       ring_ = 0;
0219       break;
0220   }
0221   return;
0222 }
0223 
0224 // ------------ auxilliary method for record info  ------------
0225 template <class Rcd>
0226 std::pair<const char*, std::string> SiStripDB2Tree::getRecordInfo(const edm::EventSetup& iSetup) const {
0227   const Rcd& record = iSetup.get<Rcd>();
0228   const edm::ValidityInterval& validity = record.validityInterval();
0229   const edm::IOVSyncValue first = validity.first();
0230   const edm::IOVSyncValue last = validity.last();
0231   if (first != edm::IOVSyncValue::beginOfTime() || last != edm::IOVSyncValue::endOfTime()) {
0232     LOGINFO("SiStripDB2Tree") << "@SUB=SiStripDB2Tree::getRecordInfo"
0233                               << "\nTrying to apply " << record.key().name() << " with multiple IOVs in tag.\n"
0234                               << "Validity range is " << first.eventID().run() << " - " << last.eventID().run() << "\n";
0235   } else {
0236     LOGINFO("SiStripDB2Tree") << "@SUB=SiStripDB2Tree::getRecordInfo"
0237                               << "\nTrying to apply " << record.key().name() << "Validity range is "
0238                               << first.eventID().run() << " - " << last.eventID().run() << "\n";
0239   }
0240 
0241   tree_->GetUserInfo()->Add(new RecordInfo(record.key().name(), std::to_string(first.eventID().run()).c_str()));
0242 
0243   return std::make_pair(record.key().name(), std::to_string(first.eventID().run()));
0244 }
0245 
0246 // ------------ method called for each run  ------------
0247 void SiStripDB2Tree::beginRun(const edm::Run& iRun, edm::EventSetup const& iSetup) {
0248   char c_run[30];
0249   sprintf(c_run, "%i", iRun.run());
0250   tree_->GetUserInfo()->Add(new TObjString(c_run));
0251 
0252   auto pedHook = this->getRecordInfo<SiStripPedestalsRcd>(iSetup);
0253   auto noiseHook = this->getRecordInfo<SiStripNoisesRcd>(iSetup);
0254   auto g1Hook = this->getRecordInfo<SiStripApvGainRcd>(iSetup);
0255   auto g2Hook = this->getRecordInfo<SiStripApvGain2Rcd>(iSetup);
0256   auto qualityHook = this->getRecordInfo<SiStripQualityRcd>(iSetup);
0257   if (isMC_) {
0258     auto g1SimHook = this->getRecordInfo<SiStripApvGainSimRcd>(iSetup);
0259   }
0260 }
0261 
0262 // ------------ method called for each event  ------------
0263 void SiStripDB2Tree::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0264   // fill header information
0265   LogDebug("SiStrip") << edm::getProcessParameterSetContainingModule(moduleDescription()).dump();
0266 
0267   const edm::ParameterSet& globalTagPSet =
0268       edm::getProcessParameterSetContainingModule(moduleDescription()).getParameterSet("PoolDBESSource@GlobalTag");
0269 
0270   processGT_ = globalTagPSet.getParameter<std::string>("globaltag");
0271 
0272   RecordInfo* GTheader = new RecordInfo("GlobalTag", processGT_.c_str());
0273   tree_->GetUserInfo()->Add(GTheader);
0274   GTheader->printInfo();
0275 
0276   // handles
0277   const SiStripPedestals* pedestalObj = &iSetup.getData(pedToken_);
0278   const SiStripNoises* noiseObj = &iSetup.getData(noiseToken_);
0279   const SiStripApvGain* g1Obj = &iSetup.getData(g1Token_);
0280   const SiStripApvGain* g2Obj = &iSetup.getData(g2Token_);
0281   const SiStripQuality* siStripQualityObj = &iSetup.getData(qualToken_);
0282   const SiStripApvGain* gsimObj = nullptr;
0283   if (isMC_) {
0284     gsimObj = &iSetup.getData(gsimToken_);
0285   } else {
0286     LOGINFO("SiStripDB2Tree") << "We have determined this Data";
0287   }
0288 
0289   bool first = true;
0290   const SiStripLatency* latencyObj = &iSetup.getData(latToken_);
0291 
0292   std::vector<uint32_t> activeDetIds;
0293   noiseObj->getDetIds(activeDetIds);
0294 
0295   const TrackerTopology* tTopo_ = &iSetup.getData(topoToken_);
0296 
0297   for (uint32_t detid : activeDetIds) {
0298     setTopoInfo(detid, tTopo_);
0299 
0300     SiStripNoises::Range noiseRange = noiseObj->getRange(detid);
0301     SiStripApvGain::Range gsimRange;
0302     if (isMC_ && gsimObj != nullptr) {
0303       gsimObj->getRange(detid);
0304     }
0305     SiStripApvGain::Range g1Range = g1Obj->getRange(detid);
0306     SiStripApvGain::Range g2Range = g2Obj->getRange(detid);
0307     SiStripPedestals::Range pedestalsRange = pedestalObj->getRange(detid);
0308 
0309     unsigned int nStrip = detInfo_.getNumberOfApvsAndStripLength(detid).first * sistrip::STRIPS_PER_APV;
0310     lenght_ = detInfo_.getNumberOfApvsAndStripLength(detid).second;
0311     detId_ = detid;
0312     det_type_ = static_cast<unsigned int>(SiStripDetId(detid).moduleGeometry());
0313     for (istrip_ = 0; istrip_ < nStrip; ++istrip_) {
0314       if (first) {
0315         first = false;
0316         std::string run_op = ((latencyObj->latency(detid, 1) & READMODEMASK) == READMODEMASK) ? "PEAK" : "DECO";
0317         text_->SetText(0., 0., run_op.c_str());
0318         LOGINFO("SiStripDB2Tree") << "SiStripOperationModeRcd "
0319                                   << ". . . " << run_op;
0320       }
0321       gsim_ = isMC_ ? gsimObj->getStripGain(istrip_, gsimRange) : 1.;
0322       g1_ = g1Obj->getStripGain(istrip_, g1Range) ? g1Obj->getStripGain(istrip_, g1Range) : 1.;
0323       g2_ = g2Obj->getStripGain(istrip_, g2Range) ? g2Obj->getStripGain(istrip_, g2Range) : 1.;
0324       noise_ = noiseObj->getNoise(istrip_, noiseRange);
0325       pedestal_ = pedestalObj->getPed(istrip_, pedestalsRange);
0326       isBad_ = siStripQualityObj->IsStripBad(siStripQualityObj->getRange(detid), istrip_);
0327       tree_->Fill();
0328     }
0329   }
0330 }
0331 
0332 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0333 void SiStripDB2Tree::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0334   edm::ParameterSetDescription desc;
0335 
0336   desc.setComment("Creates TTree with SiStrip Database tag content.");
0337   desc.add<std::string>("StripQualityLabel", "MergedBadComponent");
0338   desc.addUntracked<bool>("isMC", false);
0339 
0340   descriptions.add("SiStripDB2Tree", desc);
0341 }
0342 
0343 //define this as a plug-in
0344 #include "FWCore/PluginManager/interface/ModuleDef.h"
0345 DEFINE_FWK_MODULE(SiStripDB2Tree);