Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-12-31 23:14:00

0001 
0002 // -*- C++ -*-
0003 //
0004 // Package:    DQM/SiStripMonitorHardware
0005 // Class:      BuildTrackerMapPlugin
0006 //
0007 /**\class BuildTrackerMapPlugin BuildTrackerMap.cc DQM/SiStripMonitorHardware/plugins/BuildTrackerMap.cc
0008 
0009  Description: DQM source application to monitor common mode for SiStrip data
0010 */
0011 //
0012 //         Created:  2009/07/22
0013 //
0014 
0015 #include <sstream>
0016 #include <fstream>
0017 #include <iostream>
0018 #include <memory>
0019 #include <list>
0020 #include <algorithm>
0021 #include <cassert>
0022 
0023 #include "TCanvas.h"
0024 #include "TH1F.h"
0025 #include "TStyle.h"
0026 #include "TPaveStats.h"
0027 
0028 #include "FWCore/Framework/interface/Frameworkfwd.h"
0029 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0030 #include "FWCore/Framework/interface/Event.h"
0031 #include "FWCore/Framework/interface/EventSetup.h"
0032 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0033 #include "FWCore/ServiceRegistry/interface/Service.h"
0034 #include "FWCore/Utilities/interface/Exception.h"
0035 
0036 #include "CondFormats/DataRecord/interface/SiStripFedCablingRcd.h"
0037 #include "CondFormats/SiStripObjects/interface/SiStripFedCabling.h"
0038 #include "CondFormats/SiStripObjects/interface/FedChannelConnection.h"
0039 #include "DataFormats/TrackerCommon/interface/TrackerTopology.h"
0040 #include "Geometry/Records/interface/TrackerTopologyRcd.h"
0041 
0042 #include "CommonTools/TrackerMap/interface/TrackerMap.h"
0043 #include "CommonTools/TrackerMap/interface/TmModule.h"
0044 #include "CommonTools/TrackerMap/interface/TmApvPair.h"
0045 
0046 #include "DQM/SiStripCommon/interface/TkHistoMap.h"
0047 
0048 #include "DQMServices/Core/interface/DQMStore.h"
0049 
0050 //
0051 // Class declaration
0052 //
0053 
0054 class BuildTrackerMapPlugin : public edm::one::EDAnalyzer<> {
0055 public:
0056   typedef dqm::legacy::MonitorElement MonitorElement;
0057   typedef dqm::legacy::DQMStore DQMStore;
0058 
0059   explicit BuildTrackerMapPlugin(const edm::ParameterSet&);
0060   ~BuildTrackerMapPlugin() override {}
0061 
0062 private:
0063   void beginJob() override;
0064   void analyze(const edm::Event&, const edm::EventSetup&) override;
0065   void endJob() override;
0066 
0067   void read(bool aMechView,
0068             std::string aFile,
0069             const TkDetMap* tkDetMap,
0070             std::vector<std::unique_ptr<TkHistoMap>>& aTkMapVec,
0071             std::vector<bool>& aValidVec);
0072   void subtractMap(TkHistoMap* aResult, const TkHistoMap* aSubtr);
0073 
0074   //input file names
0075   std::string fileName_;
0076   //do mechanical view or not
0077   bool mechanicalView_;
0078   //folder name for histograms in DQMStore
0079   std::string folderName_;
0080   //print debug messages when problems are found: 1=error debug, 2=light debug, 3=full debug
0081   unsigned int printDebug_;
0082 
0083   bool doDiff_;
0084   std::string fileNameDiff_;
0085 
0086   std::vector<std::unique_ptr<TkHistoMap>> tkHistoMapVec_;
0087   std::vector<std::unique_ptr<TkHistoMap>> tkHistoMapVecDiff_;
0088 
0089   //name of the tkHistoMap to extract
0090   std::vector<std::string> tkHistoMapNameVec_;
0091   std::vector<double> minVal_;
0092   std::vector<double> maxVal_;
0093 
0094   std::vector<bool> isValidMap_;
0095   std::vector<bool> isValidMapDiff_;
0096 
0097   edm::ParameterSet pset_;
0098   std::vector<TrackerMap*> tkmap_;
0099 
0100   edm::ESGetToken<SiStripFedCabling, SiStripFedCablingRcd> fedCablingToken_;
0101   edm::ESGetToken<TrackerTopology, TrackerTopologyRcd> tTopoToken_;
0102   edm::ESGetToken<TkDetMap, TrackerTopologyRcd> tkDetMapToken_;
0103 };
0104 
0105 //
0106 // Constructors and destructor
0107 //
0108 
0109 BuildTrackerMapPlugin::BuildTrackerMapPlugin(const edm::ParameterSet& iConfig)
0110     : fileName_(iConfig.getUntrackedParameter<std::string>("InputFileName", "DQMStore.root")),
0111       mechanicalView_(iConfig.getUntrackedParameter<bool>("MechanicalView", true)),
0112       folderName_(iConfig.getUntrackedParameter<std::string>("HistogramFolderName", "DQMData/")),
0113       printDebug_(iConfig.getUntrackedParameter<unsigned int>("PrintDebugMessages", 1)),
0114       doDiff_(iConfig.getUntrackedParameter<bool>("DoDifference", false)),
0115       fileNameDiff_(iConfig.getUntrackedParameter<std::string>("InputFileNameForDiff", "DQMStore.root")),
0116       tkHistoMapNameVec_(iConfig.getUntrackedParameter<std::vector<std::string>>("TkHistoMapNameVec")),
0117       minVal_(iConfig.getUntrackedParameter<std::vector<double>>("MinValueVec")),
0118       maxVal_(iConfig.getUntrackedParameter<std::vector<double>>("MaxValueVec")),
0119       pset_(iConfig.getParameter<edm::ParameterSet>("TkmapParameters")),
0120       fedCablingToken_(esConsumes<>()),
0121       tTopoToken_(esConsumes<>()),
0122       tkDetMapToken_(esConsumes<>()) {
0123   //   for (unsigned int i(0); i<34; i++){
0124   //     if (i<4) histName_[i] << "TIB/layer_" << i+1 << "/" << tkDetMapName_ << "_TIB_L" << i+1;
0125   //     else if (i<7)  histName_[i] << "TID/side_1/wheel_" << i-3 << "/" << tkDetMapName_ << "_TIDM_D" << i-3;
0126   //     else if (i<10)  histName_[i] << "TID/side_2/wheel_" << i-6 << "/" << tkDetMapName_ << "_TIDP_D" << i-6;
0127   //     else if (i<16) histName_[i] << "TOB/layer_" << i-9 << "/" << tkDetMapName_ << "_TOB_L" << i-9;
0128   //     else if (i<25) histName_[i] << "TEC/side_1/wheel_" << i-15 << "/" << tkDetMapName_ << "_TECM_W" << i-15;
0129   //     else if (i<34) histName_[i] << "TEC/side_2/wheel_" << i-24 << "/" << tkDetMapName_ << "_TECP_W" << i-24;
0130 
0131   //     std::cout << "histName[" << i << "] =" << histName_[i] << std::endl;
0132 
0133   //   }
0134 }
0135 
0136 //
0137 // Member functions
0138 //
0139 
0140 /*Check that is possible to load in tkhistomaps histograms already stored in a DQM root file (if the folder and name are known)*/
0141 void BuildTrackerMapPlugin::read(bool aMechView,
0142                                  std::string aFile,
0143                                  const TkDetMap* tkDetMap,
0144                                  std::vector<std::unique_ptr<TkHistoMap>>& aTkMapVec,
0145                                  std::vector<bool>& aValidVec) {
0146   DQMStore* lDqmStore = edm::Service<DQMStore>().operator->();
0147   lDqmStore->open(aFile);
0148 
0149   unsigned int nHists = tkHistoMapNameVec_.size();
0150   aTkMapVec.reserve(nHists);
0151   aValidVec.reserve(nHists);
0152 
0153   std::string dirName = folderName_;
0154   if (dirName.empty()) {
0155     dirName += "Run ";
0156     dirName += aFile.substr(aFile.find_last_of('_') + 5, 6);
0157     dirName += "/SiStrip/Run summary";
0158     std::cout << " -- DirName = " << dirName << std::endl;
0159   }
0160 
0161   //lDqmStore->setCurrentFolder(dirName);
0162 
0163   unsigned int nFailTot = 0;
0164   unsigned int nTotTot = 0;
0165   for (unsigned int i(0); i < nHists; i++) {
0166     std::unique_ptr<TkHistoMap> tkHistoMap{new TkHistoMap(tkDetMap)};
0167 
0168     tkHistoMap->loadTkHistoMap(dirName, tkHistoMapNameVec_.at(i), aMechView);
0169 
0170     std::vector<MonitorElement*>& lMaps = tkHistoMap->getAllMaps();
0171 
0172     std::cout << " -- map " << i << ", nHistos = " << lMaps.size() << std::endl;
0173     unsigned int nFail = 0;
0174     unsigned int nTot = 0;
0175 
0176     for (unsigned int im(0); im < lMaps.size(); im++) {
0177       if (!lMaps[im]) {
0178         std::cout << " -- Failed to get element " << im << " for map " << i << std::endl;
0179         nFail++;
0180         nFailTot++;
0181       }
0182       nTot++;
0183       nTotTot++;
0184     }
0185 
0186     if (nFail == nTot)
0187       aValidVec[i] = false;
0188     aTkMapVec.emplace_back(std::move(tkHistoMap));
0189   }
0190 
0191   if (nFailTot < nTotTot)
0192     std::cout << " - " << nTotTot - nFailTot << "/" << nTotTot << " histomaps read with success for file ." << aFile
0193               << std::endl;
0194   else {
0195     std::cout << " - Failed to read any map for file " << aFile << ". Exiting line ... " << __LINE__ << std::endl;
0196     exit(1);
0197   }
0198 
0199   //   //get list of detid for which |deltaRMS(APV0-APV1)|>1
0200   //   unsigned int lHistoNumber = 35;
0201   //   TkDetMap lTkDetMap;
0202   //   std::ofstream list,listRms0,listRms1;
0203   //   list.open("./cmBadModuleList.dat",std::ios::out);
0204   //   listRms0.open("./cmBadModuleList_rms0.dat",std::ios::out);
0205   //   listRms1.open("./cmBadModuleList_rms1.dat",std::ios::out);
0206   //   if (!list || !listRms0 || !listRms1) {
0207   //     std::cout << "Warning, can't open output file to write bad module list !" << std::endl;
0208   //     exit(1);
0209   //   }
0210 
0211   //   TCanvas *lCan = new TCanvas("lCan","",1);
0212   //   TH1F *p_deltaMean = new TH1F("p_deltaMean",";CM_{mean}(APV0)-CM_{mean}(APV1)",500,-2,2);
0213   //   TH1F *p_deltaRMS = new TH1F("p_deltaRMS",";CM_{RMS}(APV0)-CM_{RMS}(APV1)",500,0,3);
0214   //   TH1F *p_MeanAPV0 = new TH1F("p_MeanAPV0",";CM_{mean}(APV0)",500,100,140);
0215   //   //TH1F *p_MeanAPV1 = new TH1F("p_MeanAPV1",";CM_{mean}(APV1)",500,100,140);
0216   //   TH1F *p_RMSAPV0 = new TH1F("p_RMSAPV0",";CM_{RMS}(APV0)",500,0,10);
0217   //   //TH1F *p_RMSAPV1 = new TH1F("p_RMSAPV1",";CM_{RMS}(APV1)",500,0,10);
0218 
0219   //   gStyle->SetOptStat(1111111);
0220 
0221   //   for(unsigned int layer=1;layer<lHistoNumber;++layer){
0222   //     std::vector<uint32_t> dets;
0223   //     lTkDetMap.getDetsForLayer(layer,dets);
0224   //     for(size_t i=0;i<dets.size();++i){
0225   //       if(dets[i]>0){
0226   //    //if(tkHistoMap[5]->getEntries(dets[i])>0 && tkHistoMap[5]->getValue(dets[i])) {
0227   //    if(nHists > 3){
0228   //      if (tkHistoMap[3]->getValue(dets[i]) > 1) {
0229   //        list << dets[i] << " " << tkHistoMap[3]->getValue(dets[i]) << std::endl;
0230   //      }
0231   //    p_deltaRMS->Fill(tkHistoMap[3]->getValue(dets[i]));
0232   //    }
0233   //    p_MeanAPV0->Fill(tkHistoMap[0]->getValue(dets[i]));
0234   //    //p_MeanAPV1->Fill(tkHistoMap[1]->getValue(dets[i]));
0235   //    p_RMSAPV0->Fill(tkHistoMap[1]->getValue(dets[i]));
0236   //    if (tkHistoMap[1]->getValue(dets[i]) > 2)
0237   //      listRms0 << dets[i] << " " << tkHistoMap[1]->getValue(dets[i]) << std::endl;
0238   //    //p_RMSAPV1->Fill(tkHistoMap[3]->getValue(dets[i]));
0239   //    //if (tkHistoMap[3]->getValue(dets[i]) > 2)
0240   //    //listRms1 << dets[i] << " " << tkHistoMap[3]->getValue(dets[i]) << std::endl;
0241 
0242   //    if(nHists > 2) p_deltaMean->Fill(tkHistoMap[2]->getValue(dets[i]));
0243   //       }
0244   //     }
0245   //   }
0246   //   list.close();
0247   //   listRms0.close();
0248   //   listRms1.close();
0249 
0250   //   lCan->cd();
0251   //   p_deltaRMS->Draw();
0252   //   //lCan->Print("./deltaRMStotal.png");
0253   //   lCan->Print("./deltaRMStotal.C");
0254 
0255   //   p_deltaMean->Draw();
0256   //   lCan->Update();
0257   //   lCan->Print("./deltaMeantotal.C");
0258 
0259   //   TPaveStats *statBox[2] = {0,0};
0260   //   statBox[0] = (TPaveStats*)p_MeanAPV0->FindObject("stats");
0261   //   //statBox[1] = (TPaveStats*)p_MeanAPV1->FindObject("stats");
0262 
0263   //   p_MeanAPV0->Draw();
0264   //   //p_MeanAPV1->SetLineColor(2);
0265   //   //p_MeanAPV1->Draw("same");
0266   //   if (statBox[0]) statBox[0]->Draw("same");
0267   //   if (statBox[1]) {
0268   //     statBox[1]->SetLineColor(2);
0269   //     statBox[1]->SetTextColor(2);
0270   //     statBox[1]->Draw("same");
0271   //   }
0272   //   lCan->Update();
0273   //   lCan->Print("./meanAPVstotal.C");
0274 
0275   //   statBox[0] = (TPaveStats*)p_RMSAPV0->FindObject("stats");
0276   //   //statBox[1] = (TPaveStats*)p_RMSAPV1->FindObject("stats");
0277 
0278   //   p_RMSAPV0->Draw();
0279   //   //p_RMSAPV1->SetLineColor(2);
0280   //   //p_RMSAPV1->Draw("same");
0281   //   if (statBox[0]) statBox[0]->Draw("same");
0282   //   if (statBox[1]) {
0283   //     statBox[1]->SetLineColor(2);
0284   //     statBox[1]->SetTextColor(2);
0285   //     statBox[1]->Draw("same");
0286   //   }
0287   //   lCan->Update();
0288   //   lCan->Print("./rmsAPVstotal.C");
0289 }
0290 
0291 // ------------ method called to for each event  ------------
0292 void BuildTrackerMapPlugin::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0293   static bool firstEvent = true;
0294 
0295   if (tkHistoMapVec_.empty() && (!tkHistoMapNameVec_.empty())) {
0296     const auto tkDetMap = &iSetup.getData(tkDetMapToken_);
0297     read(mechanicalView_, fileName_, tkDetMap, tkHistoMapVec_, isValidMap_);
0298     if (doDiff_)
0299       read(mechanicalView_, fileNameDiff_, tkDetMap, tkHistoMapVecDiff_, isValidMapDiff_);
0300   }
0301 
0302   if (firstEvent) {
0303     const auto fedcabling = &iSetup.getData(fedCablingToken_);
0304     const auto tTopo = &iSetup.getData(tTopoToken_);
0305     for (unsigned int i(0); i < tkHistoMapNameVec_.size(); i++) {
0306       tkmap_.push_back(new TrackerMap(pset_, fedcabling, tTopo));
0307     }
0308   }
0309 
0310   firstEvent = false;
0311 
0312   std::cout << "End of analyze method: tkmap_ size = " << tkmap_.size() << std::endl;
0313 
0314 }  //analyze method
0315 
0316 // ------------ method called once each job just before starting event loop  ------------
0317 void BuildTrackerMapPlugin::beginJob() {}
0318 
0319 // ------------ method called once each job just after ending the event loop  ------------
0320 void BuildTrackerMapPlugin::endJob() {
0321   //edm::ESHandle<SiStripFedCabling> pDD1;
0322   //iSetup.get<SiStripFedCablingRcd>().get(pDD1);
0323   std::cout << "Processing endjob with " << tkHistoMapNameVec_.size() << " elements." << std::endl;
0324 
0325   assert(minVal_.size() == tkHistoMapNameVec_.size());
0326   assert(maxVal_.size() == tkHistoMapNameVec_.size());
0327 
0328   for (unsigned int i(0); i < tkHistoMapNameVec_.size(); i++) {
0329     std::cout << "Processing element " << i << ": " << tkHistoMapNameVec_.at(i) << std::endl;
0330     std::cout << "Min, max = " << minVal_.at(i) << " " << maxVal_.at(i) << std::endl;
0331 
0332     TrackerMap* lTkMap = tkmap_.at(i);
0333 
0334     if (!lTkMap) {
0335       std::cout << "tkmap_ is NULL for element " << i << "... continuing ..." << std::endl;
0336       continue;
0337     }
0338 
0339     subtractMap(tkHistoMapVec_.at(i).get(), tkHistoMapVecDiff_.at(i).get());
0340 
0341     //(pset_,pDD1);
0342     lTkMap->setPalette(1);
0343     lTkMap->showPalette(true);
0344     if (!tkHistoMapVec_.at(i) || !isValidMap_.at(i)) {
0345       std::cout << "Warning, tkHistoMap is invalid for element " << i << "... continuing ..." << std::endl;
0346       continue;
0347     }
0348     tkHistoMapVec_.at(i)->dumpInTkMap(lTkMap);
0349 
0350     //to print all figures to create fancy view
0351     //lTkMap->printall(true,0,255,tkHistoMapNameVec_.at(i));
0352     lTkMap->save(true, minVal_.at(i), maxVal_.at(i), tkHistoMapNameVec_.at(i) + std::string(".png"));
0353     lTkMap->save_as_fedtrackermap(
0354         true, minVal_.at(i), maxVal_.at(i), tkHistoMapNameVec_.at(i) + std::string("_FED.png"));
0355   }
0356 }
0357 
0358 void BuildTrackerMapPlugin::subtractMap(TkHistoMap* aResult, const TkHistoMap* aSubtr) {
0359   std::vector<MonitorElement*>& lMaps = aResult->getAllMaps();
0360   const std::vector<MonitorElement*>& lMapsDiff = aSubtr->getAllMaps();
0361 
0362   assert(lMaps.size() == lMapsDiff.size());
0363 
0364   for (unsigned int im(0); im < lMaps.size(); im++) {
0365     if (!lMaps[im] || !lMapsDiff[im]) {
0366       std::cout << " -- Failed to get element " << im << " for maps." << std::endl;
0367     } else {
0368       (lMaps[im]->getTProfile2D())->Add(lMapsDiff[im]->getTProfile2D(), -1);
0369     }
0370   }
0371 }
0372 
0373 // Define as a plug-in
0374 //
0375 
0376 #include "FWCore/Framework/interface/MakerMacros.h"
0377 DEFINE_FWK_MODULE(BuildTrackerMapPlugin);