Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-10-25 09:43:47

0001 //
0002 // Original Author:  Pawel Jurgielewicz
0003 //         Created:  Tue, 21 Nov 2017 13:38:45 GMT
0004 //
0005 // Modified by:      Marco Musich
0006 //
0007 
0008 // system include files
0009 #include <array>
0010 #include <memory>
0011 #include <fstream>
0012 #include <utility>
0013 #include <iostream>
0014 #include <vector>
0015 #include <map>
0016 #include <string>
0017 
0018 // user include files
0019 #include "CalibTracker/SiStripCommon/interface/TkDetMap.h"
0020 #include "CommonTools/UtilAlgos/interface/TFileService.h"
0021 #include "DataFormats/Common/interface/DetSetVectorNew.h"
0022 #include "DataFormats/SiPixelDetId/interface/PXBDetId.h"
0023 #include "DataFormats/SiPixelDetId/interface/PXFDetId.h"
0024 #include "DataFormats/SiStripCluster/interface/SiStripCluster.h"
0025 #include "DataFormats/SiStripDigi/interface/SiStripDigi.h"
0026 #include "DataFormats/TrackReco/interface/Track.h"
0027 #include "DataFormats/TrackReco/interface/TrackFwd.h"
0028 #include "DataFormats/TrackerCommon/interface/PixelBarrelName.h"
0029 #include "DataFormats/TrackerCommon/interface/PixelEndcapName.h"
0030 #include "DataFormats/TrackerCommon/interface/TrackerTopology.h"
0031 #include "FWCore/Framework/interface/ESHandle.h"
0032 #include "FWCore/Framework/interface/Event.h"
0033 #include "FWCore/Framework/interface/Frameworkfwd.h"
0034 #include "FWCore/Framework/interface/MakerMacros.h"
0035 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0036 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0037 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0038 #include "FWCore/ParameterSet/interface/EmptyGroupDescription.h"
0039 #include "FWCore/ParameterSet/interface/allowedValues.h"
0040 #include "FWCore/ServiceRegistry/interface/Service.h"
0041 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
0042 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
0043 
0044 // root include files
0045 #include "TGraph.h"
0046 #include "TObjString.h"
0047 #include "TObjArray.h"
0048 #include "TH2Poly.h"
0049 #include "TProfile2D.h"
0050 #include "TColor.h"
0051 
0052 using namespace edm;
0053 
0054 class TrackerRemapper : public edm::one::EDAnalyzer<edm::one::SharedResources> {
0055 public:
0056   explicit TrackerRemapper(const edm::ParameterSet&);
0057   ~TrackerRemapper() override;
0058 
0059   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0060 
0061   enum PixelLayerEnum {
0062     INVALID = 0,
0063 
0064     PXB_L1,
0065     PXB_L2,
0066     PXB_L3,
0067     PXB_L4,
0068 
0069     PXF_R1,
0070     PXF_R2
0071   };
0072 
0073   enum AnalyzeData {
0074     RECHITS = 1,
0075     DIGIS,
0076     CLUSTERS,
0077   };
0078 
0079   enum OpMode { MODE_ANALYZE = 0, MODE_REMAP = 1 };
0080 
0081 private:
0082   void beginJob() override;
0083   void analyze(const edm::Event&, const edm::EventSetup&) override;
0084   void endJob() override;
0085 
0086   void readVertices(double& minx, double& maxx, double& miny, double& maxy);
0087 
0088   void prepareStripNames();
0089   void preparePixelNames();
0090 
0091   void bookBins();
0092 
0093   template <class T>
0094   void analyzeGeneric(const edm::Event& iEvent, const edm::EDGetTokenT<T>& src);
0095   void analyzeRechits(const edm::Event& iEvent, const edm::EDGetTokenT<reco::TrackCollection>& src);
0096 
0097   void fillStripRemap();
0098   void fillPixelRemap(const TrackerGeometry* theTrackerGeometry, const TrackerTopology* tt);
0099   void fillBarrelRemap(TFile* rootFileHandle, const TrackerGeometry* theTrackerGeometry, const TrackerTopology* tt);
0100   void fillEndcapRemap(TFile* rootFileHandle, const TrackerGeometry* theTrackerGeometry, const TrackerTopology* tt);
0101 
0102   const edm::ESGetToken<TrackerGeometry, TrackerDigiGeometryRecord> geomToken_;
0103   const edm::ESGetToken<TrackerTopology, TrackerTopologyRcd> topoToken_;
0104   const edm::ESGetToken<TkDetMap, TrackerTopologyRcd> tkDetMapToken_;
0105 
0106   edm::Service<TFileService> fs;
0107 
0108   int m_opMode;
0109   int m_analyzeMode;
0110 
0111   std::map<long, TGraph*> m_bins;
0112   std::vector<unsigned> m_detIdVector;
0113 
0114   const TkDetMap* m_tkdetmap;
0115 
0116   std::map<unsigned, std::string> m_stripHistnameMap;
0117   std::map<unsigned, std::string> m_pixelHistnameMap;
0118   std::map<unsigned, std::string> m_analyzeModeNameMap;
0119 
0120   std::string m_stripRemapFile;
0121   std::string m_pixelRemapFile;
0122 
0123   std::string m_stripBaseDir, m_stripDesiredHistogram;
0124   std::string m_pixelBaseDir, m_pixelDesiredHistogramBarrel, m_pixelDesiredHistogramDisk;
0125 
0126   std::string runString;
0127 
0128   TH2Poly* trackerMap{nullptr};
0129 
0130   edm::EDGetTokenT<reco::TrackCollection> rechitSrcToken;
0131   edm::EDGetTokenT<edmNew::DetSetVector<SiStripDigi>> digiSrcToken;
0132   edm::EDGetTokenT<edmNew::DetSetVector<SiStripCluster>> clusterSrcToken;
0133 };
0134 
0135 template <class T>
0136 //***************************************************************//
0137 void TrackerRemapper::analyzeGeneric(const edm::Event& iEvent, const edm::EDGetTokenT<T>& src)
0138 //***************************************************************//
0139 {
0140   edm::Handle<T> input;
0141   iEvent.getByToken(src, input);
0142 
0143   if (!input.isValid()) {
0144     edm::LogError("TrackerRemapper") << "<GENERIC> not found... Aborting...\n";
0145     return;
0146   }
0147 
0148   typename T::const_iterator it;
0149   for (it = input->begin(); it != input->end(); ++it) {
0150     auto id = DetId(it->detId());
0151     trackerMap->Fill(TString::Format("%ld", (long)id.rawId()), it->size());
0152   }
0153 }
0154 
0155 template <>
0156 //***************************************************************//
0157 void TrackerRemapper::analyzeGeneric(const edm::Event& iEvent, const edm::EDGetTokenT<reco::TrackCollection>& t)
0158 //***************************************************************//
0159 {
0160   analyzeRechits(iEvent, t);
0161 }
0162 
0163 //***************************************************************//
0164 TrackerRemapper::TrackerRemapper(const edm::ParameterSet& iConfig)
0165     : geomToken_(esConsumes()),
0166       topoToken_(esConsumes()),
0167       tkDetMapToken_(esConsumes()),
0168       m_opMode(iConfig.getParameter<int>("opMode")),
0169       m_analyzeMode(iConfig.getParameter<int>("analyzeMode")) {
0170   usesResource("TFileService");
0171 
0172   if (m_opMode == MODE_REMAP) {
0173     m_stripRemapFile = iConfig.getParameter<std::string>("stripRemapFile");
0174     m_stripDesiredHistogram = iConfig.getParameter<std::string>("stripHistogram");
0175     runString = iConfig.getParameter<std::string>("runString");
0176 
0177     m_pixelRemapFile = std::string("DQM_V0001_PixelPhase1_R000305516.root");
0178 
0179     m_stripBaseDir = std::string("DQMData/Run " + runString + "/SiStrip/Run summary/MechanicalView/");
0180     m_pixelBaseDir = std::string("DQMData/Run " + runString + "/PixelPhase1/Run summary/Phase1_MechanicalView/");
0181 
0182     m_pixelDesiredHistogramBarrel = std::string("adc_per_SignedModule_per_SignedLadder");
0183     m_pixelDesiredHistogramDisk = std::string("adc_per_PXDisk_per_SignedBladePanel");
0184 
0185     prepareStripNames();
0186     preparePixelNames();
0187   } else if (m_opMode == MODE_ANALYZE) {
0188     m_analyzeModeNameMap[RECHITS] = "# Rechits";
0189     m_analyzeModeNameMap[DIGIS] = "# Digis";
0190     m_analyzeModeNameMap[CLUSTERS] = "# Clusters";
0191 
0192     switch (m_analyzeMode) {
0193       case RECHITS:
0194         rechitSrcToken = consumes<reco::TrackCollection>(iConfig.getParameter<edm::InputTag>("src"));
0195         break;
0196       case DIGIS:
0197         digiSrcToken = consumes<edmNew::DetSetVector<SiStripDigi>>(iConfig.getParameter<edm::InputTag>("src"));
0198         break;
0199       case CLUSTERS:
0200         clusterSrcToken = consumes<edmNew::DetSetVector<SiStripCluster>>(iConfig.getParameter<edm::InputTag>("src"));
0201         break;
0202       default:
0203         edm::LogError("LogicError") << "Unrecognized analyze mode!" << std::endl;
0204     }
0205   } else {
0206     throw cms::Exception("TrackerRemapper") << "Unrecognized operations mode!" << std::endl;
0207   }
0208 
0209   // TColor::SetPalette(1);
0210 }
0211 
0212 //***************************************************************//
0213 void TrackerRemapper::prepareStripNames()
0214 //***************************************************************//
0215 {
0216   m_stripHistnameMap[TkLayerMap::TkLayerEnum::TIB_L1] =
0217       m_stripBaseDir + "TIB/layer_1/" + m_stripDesiredHistogram + "_TIB_L1;1";
0218   m_stripHistnameMap[TkLayerMap::TkLayerEnum::TIB_L2] =
0219       m_stripBaseDir + "TIB/layer_2/" + m_stripDesiredHistogram + "_TIB_L2;1";
0220   m_stripHistnameMap[TkLayerMap::TkLayerEnum::TIB_L3] =
0221       m_stripBaseDir + "TIB/layer_3/" + m_stripDesiredHistogram + "_TIB_L3;1";
0222   m_stripHistnameMap[TkLayerMap::TkLayerEnum::TIB_L4] =
0223       m_stripBaseDir + "TIB/layer_4/" + m_stripDesiredHistogram + "_TIB_L4;1";
0224 
0225   m_stripHistnameMap[TkLayerMap::TkLayerEnum::TIDM_D1] =
0226       m_stripBaseDir + "TID/MINUS/wheel_1/" + m_stripDesiredHistogram + "_TIDM_D1;1";
0227   m_stripHistnameMap[TkLayerMap::TkLayerEnum::TIDM_D2] =
0228       m_stripBaseDir + "TID/MINUS/wheel_2/" + m_stripDesiredHistogram + "_TIDM_D2;1";
0229   m_stripHistnameMap[TkLayerMap::TkLayerEnum::TIDM_D3] =
0230       m_stripBaseDir + "TID/MINUS/wheel_3/" + m_stripDesiredHistogram + "_TIDM_D3;1";
0231   m_stripHistnameMap[TkLayerMap::TkLayerEnum::TIDP_D1] =
0232       m_stripBaseDir + "TID/PLUS/wheel_1/" + m_stripDesiredHistogram + "_TIDP_D1;1";
0233   m_stripHistnameMap[TkLayerMap::TkLayerEnum::TIDP_D2] =
0234       m_stripBaseDir + "TID/PLUS/wheel_2/" + m_stripDesiredHistogram + "_TIDP_D2;1";
0235   m_stripHistnameMap[TkLayerMap::TkLayerEnum::TIDP_D3] =
0236       m_stripBaseDir + "TID/PLUS/wheel_3/" + m_stripDesiredHistogram + "_TIDP_D3;1";
0237 
0238   m_stripHistnameMap[TkLayerMap::TkLayerEnum::TOB_L1] =
0239       m_stripBaseDir + "TOB/layer_1/" + m_stripDesiredHistogram + "_TOB_L1;1";
0240   m_stripHistnameMap[TkLayerMap::TkLayerEnum::TOB_L2] =
0241       m_stripBaseDir + "TOB/layer_2/" + m_stripDesiredHistogram + "_TOB_L2;1";
0242   m_stripHistnameMap[TkLayerMap::TkLayerEnum::TOB_L3] =
0243       m_stripBaseDir + "TOB/layer_3/" + m_stripDesiredHistogram + "_TOB_L3;1";
0244   m_stripHistnameMap[TkLayerMap::TkLayerEnum::TOB_L4] =
0245       m_stripBaseDir + "TOB/layer_4/" + m_stripDesiredHistogram + "_TOB_L4;1";
0246   m_stripHistnameMap[TkLayerMap::TkLayerEnum::TOB_L5] =
0247       m_stripBaseDir + "TOB/layer_5/" + m_stripDesiredHistogram + "_TOB_L5;1";
0248   m_stripHistnameMap[TkLayerMap::TkLayerEnum::TOB_L6] =
0249       m_stripBaseDir + "TOB/layer_6/" + m_stripDesiredHistogram + "_TOB_L6;1";
0250 
0251   m_stripHistnameMap[TkLayerMap::TkLayerEnum::TECM_W1] =
0252       m_stripBaseDir + "TEC/MINUS/wheel_1/" + m_stripDesiredHistogram + "_TECM_W1;1";
0253   m_stripHistnameMap[TkLayerMap::TkLayerEnum::TECM_W2] =
0254       m_stripBaseDir + "TEC/MINUS/wheel_2/" + m_stripDesiredHistogram + "_TECM_W2;1";
0255   m_stripHistnameMap[TkLayerMap::TkLayerEnum::TECM_W3] =
0256       m_stripBaseDir + "TEC/MINUS/wheel_3/" + m_stripDesiredHistogram + "_TECM_W3;1";
0257   m_stripHistnameMap[TkLayerMap::TkLayerEnum::TECM_W4] =
0258       m_stripBaseDir + "TEC/MINUS/wheel_4/" + m_stripDesiredHistogram + "_TECM_W4;1";
0259   m_stripHistnameMap[TkLayerMap::TkLayerEnum::TECM_W5] =
0260       m_stripBaseDir + "TEC/MINUS/wheel_5/" + m_stripDesiredHistogram + "_TECM_W5;1";
0261   m_stripHistnameMap[TkLayerMap::TkLayerEnum::TECM_W6] =
0262       m_stripBaseDir + "TEC/MINUS/wheel_6/" + m_stripDesiredHistogram + "_TECM_W6;1";
0263   m_stripHistnameMap[TkLayerMap::TkLayerEnum::TECM_W7] =
0264       m_stripBaseDir + "TEC/MINUS/wheel_7/" + m_stripDesiredHistogram + "_TECM_W7;1";
0265   m_stripHistnameMap[TkLayerMap::TkLayerEnum::TECM_W8] =
0266       m_stripBaseDir + "TEC/MINUS/wheel_8/" + m_stripDesiredHistogram + "_TECM_W8;1";
0267   m_stripHistnameMap[TkLayerMap::TkLayerEnum::TECM_W9] =
0268       m_stripBaseDir + "TEC/MINUS/wheel_9/" + m_stripDesiredHistogram + "_TECM_W9;1";
0269 
0270   m_stripHistnameMap[TkLayerMap::TkLayerEnum::TECP_W1] =
0271       m_stripBaseDir + "TEC/PLUS/wheel_1/" + m_stripDesiredHistogram + "_TECP_W1;1";
0272   m_stripHistnameMap[TkLayerMap::TkLayerEnum::TECP_W2] =
0273       m_stripBaseDir + "TEC/PLUS/wheel_2/" + m_stripDesiredHistogram + "_TECP_W2;1";
0274   m_stripHistnameMap[TkLayerMap::TkLayerEnum::TECP_W3] =
0275       m_stripBaseDir + "TEC/PLUS/wheel_3/" + m_stripDesiredHistogram + "_TECP_W3;1";
0276   m_stripHistnameMap[TkLayerMap::TkLayerEnum::TECP_W4] =
0277       m_stripBaseDir + "TEC/PLUS/wheel_4/" + m_stripDesiredHistogram + "_TECP_W4;1";
0278   m_stripHistnameMap[TkLayerMap::TkLayerEnum::TECP_W5] =
0279       m_stripBaseDir + "TEC/PLUS/wheel_5/" + m_stripDesiredHistogram + "_TECP_W5;1";
0280   m_stripHistnameMap[TkLayerMap::TkLayerEnum::TECP_W6] =
0281       m_stripBaseDir + "TEC/PLUS/wheel_6/" + m_stripDesiredHistogram + "_TECP_W6;1";
0282   m_stripHistnameMap[TkLayerMap::TkLayerEnum::TECP_W7] =
0283       m_stripBaseDir + "TEC/PLUS/wheel_7/" + m_stripDesiredHistogram + "_TECP_W7;1";
0284   m_stripHistnameMap[TkLayerMap::TkLayerEnum::TECP_W8] =
0285       m_stripBaseDir + "TEC/PLUS/wheel_8/" + m_stripDesiredHistogram + "_TECP_W8;1";
0286   m_stripHistnameMap[TkLayerMap::TkLayerEnum::TECP_W9] =
0287       m_stripBaseDir + "TEC/PLUS/wheel_9/" + m_stripDesiredHistogram + "_TECP_W9;1";
0288 }
0289 
0290 //***************************************************************//
0291 void TrackerRemapper::preparePixelNames()
0292 //***************************************************************//
0293 {
0294   m_pixelHistnameMap[PixelLayerEnum::PXB_L1] =
0295       m_pixelBaseDir + "PXBarrel/" + m_pixelDesiredHistogramBarrel + "_PXLayer_1;1";
0296   m_pixelHistnameMap[PixelLayerEnum::PXB_L2] =
0297       m_pixelBaseDir + "PXBarrel/" + m_pixelDesiredHistogramBarrel + "_PXLayer_2;1";
0298   m_pixelHistnameMap[PixelLayerEnum::PXB_L3] =
0299       m_pixelBaseDir + "PXBarrel/" + m_pixelDesiredHistogramBarrel + "_PXLayer_3;1";
0300   m_pixelHistnameMap[PixelLayerEnum::PXB_L4] =
0301       m_pixelBaseDir + "PXBarrel/" + m_pixelDesiredHistogramBarrel + "_PXLayer_4;1";
0302 
0303   m_pixelHistnameMap[PixelLayerEnum::PXF_R1] =
0304       m_pixelBaseDir + "PXForward/" + m_pixelDesiredHistogramDisk + "_PXRing_1;1";
0305   m_pixelHistnameMap[PixelLayerEnum::PXF_R2] =
0306       m_pixelBaseDir + "PXForward/" + m_pixelDesiredHistogramDisk + "_PXRing_2;1";
0307 }
0308 
0309 TrackerRemapper::~TrackerRemapper() {}
0310 
0311 //***************************************************************//
0312 void TrackerRemapper::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
0313 //***************************************************************//
0314 {
0315   // get the ES products
0316 
0317   const TrackerGeometry* theTrackerGeometry = &iSetup.getData(geomToken_);
0318   const TrackerTopology* tt = &iSetup.getData(topoToken_);
0319   m_tkdetmap = &iSetup.getData(tkDetMapToken_);
0320 
0321   if (!trackerMap)
0322     bookBins();
0323 
0324   if (m_opMode == MODE_ANALYZE) {
0325     switch (m_analyzeMode) {
0326       case AnalyzeData::RECHITS:
0327         analyzeGeneric(iEvent, rechitSrcToken);
0328         break;
0329       case AnalyzeData::DIGIS:
0330         analyzeGeneric(iEvent, digiSrcToken);
0331         break;
0332       case AnalyzeData::CLUSTERS:
0333         analyzeGeneric(iEvent, clusterSrcToken);
0334         break;
0335       default:
0336         edm::LogError("LogicError") << "Unrecognized Analyze mode!" << std::endl;
0337         return;
0338     }
0339   } else if (m_opMode == MODE_REMAP) {
0340     fillStripRemap();
0341     fillPixelRemap(theTrackerGeometry, tt);
0342   }
0343 }
0344 
0345 //***************************************************************//
0346 void TrackerRemapper::analyzeRechits(const edm::Event& iEvent, const edm::EDGetTokenT<reco::TrackCollection>& src)
0347 //***************************************************************//
0348 {
0349   edm::Handle<reco::TrackCollection> tracks;
0350   iEvent.getByToken(src, tracks);
0351   if (!tracks.isValid()) {
0352     LogInfo("Analyzer") << "reco::TrackCollection not found... Aborting...\n";
0353     return;
0354   }
0355 
0356   for (auto const& track : *tracks) {
0357     auto recHitsBegin = track.recHitsBegin();
0358     for (unsigned i = 0; i < track.recHitsSize(); ++i) {
0359       auto recHit = *(recHitsBegin + i);
0360       if (!recHit->isValid())
0361         continue;
0362 
0363       DetId id = recHit->geographicalId();
0364       unsigned subdetId = id.subdetId();
0365 
0366       //reject Pixel
0367       if (subdetId == PixelSubdetector::PixelBarrel || subdetId == PixelSubdetector::PixelEndcap)
0368         continue;
0369 
0370       // NRECHITS
0371       trackerMap->Fill(TString::Format("%ld", (long)id.rawId()), 1);
0372     }
0373   }
0374 }
0375 
0376 //***************************************************************//
0377 void TrackerRemapper::bookBins()
0378 //***************************************************************//
0379 {
0380   // Read vertices from file
0381   double minx = 0xFFFFFF, maxx = -0xFFFFFF, miny = 0xFFFFFF, maxy = -0xFFFFFFF;
0382   readVertices(minx, maxx, miny, maxy);
0383 
0384   TObject* ghostObj = fs->make<TH2Poly>("ghost", "ghost", -1, 1, -1, 1);
0385 
0386   TDirectory* topDir = fs->getBareDirectory();
0387   topDir->cd();
0388 
0389   int margin = 50;
0390   std::string mapTitle;
0391   switch (m_opMode) {
0392     case MODE_ANALYZE:
0393       mapTitle = std::string(m_analyzeModeNameMap[m_analyzeMode] + " - " + runString);
0394       break;
0395     case MODE_REMAP:
0396       mapTitle = std::string(m_stripDesiredHistogram + " - " + runString);
0397       break;
0398   }
0399 
0400   trackerMap = new TH2Poly("Tracker Map", mapTitle.c_str(), minx - margin, maxx + margin, miny - margin, maxy + margin);
0401   trackerMap->SetFloat();
0402   trackerMap->SetOption("COLZ");
0403   trackerMap->SetStats(false);
0404 
0405   for (auto pair : m_bins) {
0406     trackerMap->AddBin(pair.second->Clone());
0407   }
0408 
0409   topDir->Add(trackerMap);
0410 
0411   ghostObj->Delete();  //not needed any more
0412 }
0413 
0414 //***************************************************************//
0415 void TrackerRemapper::readVertices(double& minx, double& maxx, double& miny, double& maxy)
0416 //***************************************************************//
0417 {
0418   std::ifstream in;
0419 
0420   // TPolyline vertices stored at https://github.com/cms-data/DQM-SiStripMonitorClient
0421   in.open(edm::FileInPath("DQM/SiStripMonitorClient/data/Geometry/tracker_map_bare").fullPath().c_str());
0422 
0423   if (!in.good()) {
0424     throw cms::Exception("TrackerRemapper") << "Error Reading File" << std::endl;
0425   }
0426   while (in.good()) {
0427     long detid = 0;
0428     double x[5], y[5];
0429 
0430     std::string line;
0431     std::getline(in, line);
0432 
0433     TString string(line);
0434     TObjArray* array = string.Tokenize(" ");
0435     int ix{0}, iy{0};
0436     bool isPixel{false};
0437     for (int i = 0; i < array->GetEntries(); ++i) {
0438       if (i == 0) {
0439         detid = static_cast<TObjString*>(array->At(i))->String().Atoll();
0440 
0441         // Drop Pixel Data
0442         DetId detId(detid);
0443         if (detId.subdetId() == PixelSubdetector::PixelBarrel || detId.subdetId() == PixelSubdetector::PixelEndcap) {
0444           isPixel = true;
0445           break;
0446         }
0447       } else {
0448         if (i % 2 == 0) {
0449           x[ix] = static_cast<TObjString*>(array->At(i))->String().Atof();
0450 
0451           if (x[ix] < minx)
0452             minx = x[ix];
0453           if (x[ix] > maxx)
0454             maxx = x[ix];
0455 
0456           ++ix;
0457         } else {
0458           y[iy] = static_cast<TObjString*>(array->At(i))->String().Atof();
0459 
0460           if (y[iy] < miny)
0461             miny = y[iy];
0462           if (y[iy] > maxy)
0463             maxy = y[iy];
0464 
0465           ++iy;
0466         }
0467       }
0468     }
0469 
0470     if (isPixel)
0471       continue;
0472 
0473     m_detIdVector.push_back(detid);
0474     m_bins[detid] = new TGraph(ix, x, y);
0475     m_bins[detid]->SetName(TString::Format("%ld", detid));
0476     m_bins[detid]->SetTitle(TString::Format("Module ID=%ld", detid));
0477   }
0478 }
0479 
0480 //***************************************************************//
0481 void TrackerRemapper::fillStripRemap()
0482 //***************************************************************//
0483 {
0484   int nchX;
0485   int nchY;
0486   double lowX, highX;
0487   double lowY, highY;
0488 
0489   TFile* rootFileHandle = new TFile(m_stripRemapFile.c_str());
0490 
0491   for (int layer = TkLayerMap::TkLayerEnum::TIB_L1; layer <= TkLayerMap::TkLayerEnum::TECP_W9; ++layer) {
0492     m_tkdetmap->getComponents(layer, nchX, lowX, highX, nchY, lowY, highY);
0493 
0494     const TProfile2D* histHandle = (TProfile2D*)rootFileHandle->Get(m_stripHistnameMap[layer].c_str());
0495 
0496     if (!histHandle) {
0497       edm::LogError("TrackerRemapper") << "Could not find histogram:\n\t" << m_stripHistnameMap[layer] << std::endl;
0498       return;
0499     }
0500 
0501     for (unsigned binx = 1; binx <= (unsigned)nchX; ++binx) {
0502       for (unsigned biny = 1; biny <= (unsigned)nchY; ++biny) {
0503         long rawid = m_tkdetmap->getDetFromBin(layer, binx, biny);
0504 
0505         if (rawid)  //bin represents real module -> go to file
0506         {
0507           double val = histHandle->GetBinContent(binx, biny);
0508 
0509           // edm::LogInfo("TrackerRemapper") << rawid << " " << val << "\n";
0510 
0511           trackerMap->Fill(TString::Format("%ld", rawid), val);
0512         }
0513       }
0514     }
0515   }
0516 
0517   rootFileHandle->Close();
0518 }
0519 
0520 //***************************************************************//
0521 void TrackerRemapper::fillPixelRemap(const TrackerGeometry* theTrackerGeometry, const TrackerTopology* tt)
0522 //***************************************************************//
0523 {
0524   TFile* rootFileHandle = new TFile(m_pixelRemapFile.c_str());
0525 
0526   if (!rootFileHandle) {
0527     edm::LogError("TrackerRemapper") << "Could not find file:\n\t" << m_pixelRemapFile << std::endl;
0528     return;
0529   }
0530   fillBarrelRemap(rootFileHandle, theTrackerGeometry, tt);
0531   fillEndcapRemap(rootFileHandle, theTrackerGeometry, tt);
0532 
0533   rootFileHandle->Close();
0534 }
0535 
0536 //***************************************************************//
0537 void TrackerRemapper::fillBarrelRemap(TFile* rootFileHandle,
0538                                       const TrackerGeometry* theTrackerGeometry,
0539                                       const TrackerTopology* tt)
0540 //***************************************************************//
0541 {
0542   TrackingGeometry::DetContainer pxb = theTrackerGeometry->detsPXB();
0543 
0544   for (auto& i : pxb) {
0545     const GeomDet* det = i;
0546 
0547     PXBDetId id = det->geographicalId();
0548     long rawid = id.rawId();
0549 
0550     int module = tt->pxbModule(id);
0551     int layer = tt->pxbLayer(id);
0552 
0553     int signedOnlineModule = module - 4;
0554     if (signedOnlineModule <= 0)
0555       --signedOnlineModule;
0556 
0557     PixelBarrelName pixelBarrelName = PixelBarrelName(id, tt, true);
0558     int onlineShell = pixelBarrelName.shell();
0559 
0560     int signedOnlineLadder = ((onlineShell & 1) ? -pixelBarrelName.ladderName() : pixelBarrelName.ladderName());
0561 
0562     const TProfile2D* histHandle = (TProfile2D*)rootFileHandle->Get(m_pixelHistnameMap[layer].c_str());
0563 
0564     unsigned nx = histHandle->GetNbinsX();
0565     unsigned ny = histHandle->GetNbinsY();
0566 
0567     unsigned binX = signedOnlineModule + ((nx + 1) >> 1);
0568     unsigned binY = (signedOnlineLadder) + ((ny + 1) >> 1);
0569 
0570     double val = histHandle->GetBinContent(binX, binY);
0571 
0572     trackerMap->Fill(TString::Format("%ld", rawid), val);
0573   }
0574 }
0575 
0576 //***************************************************************//
0577 void TrackerRemapper::fillEndcapRemap(TFile* rootFileHandle,
0578                                       const TrackerGeometry* theTrackerGeometry,
0579                                       const TrackerTopology* tt)
0580 //***************************************************************//
0581 {
0582   TrackingGeometry::DetContainer pxf = theTrackerGeometry->detsPXF();
0583 
0584   for (auto& i : pxf) {
0585     const GeomDet* det = i;
0586 
0587     PXFDetId id = det->geographicalId();
0588 
0589     int side = tt->side(id);
0590     int disk = tt->layer(id);
0591 
0592     long rawid = id.rawId();
0593 
0594     PixelEndcapName pixelEndcapName = PixelEndcapName(PXFDetId(rawid), tt, true);
0595 
0596     unsigned layer = pixelEndcapName.ringName() - 1 + PixelLayerEnum::PXF_R1;
0597     const TProfile2D* histHandle = (TProfile2D*)rootFileHandle->Get(m_pixelHistnameMap[layer].c_str());
0598 
0599     // ---- REMAP (Online -> Offline)
0600     unsigned nx = histHandle->GetNbinsX();
0601     unsigned ny = histHandle->GetNbinsY();
0602 
0603     int onlineBlade = pixelEndcapName.bladeName();
0604     bool isInnerOnlineBlade = !(pixelEndcapName.halfCylinder() & 1);  // inner -> blade > 0 (?)
0605 
0606     int signedOnlineBlade = (isInnerOnlineBlade) ? onlineBlade : -onlineBlade;
0607     int signedDisk = (side == 2) ? disk : -disk;
0608     int pannel = pixelEndcapName.pannelName() - 1;
0609 
0610     unsigned binX = signedDisk + ((nx + 1) >> 1);
0611     unsigned binY = (signedOnlineBlade * 2) + (ny >> 1);
0612 
0613     double val = histHandle->GetBinContent(binX, binY + pannel);
0614 
0615     trackerMap->Fill(TString::Format("%ld", rawid), val);
0616   }
0617 }
0618 
0619 void TrackerRemapper::beginJob() {}
0620 
0621 void TrackerRemapper::endJob() {}
0622 
0623 //***************************************************************//
0624 void TrackerRemapper::fillDescriptions(edm::ConfigurationDescriptions& descriptions)
0625 //***************************************************************//
0626 {
0627   edm::ParameterSetDescription desc;
0628   desc.setComment(
0629       "Creates TH2Poly Strip Tracker maps by either analyzing the event or remapping exising DQM historams");
0630   desc.add<edm::InputTag>("src", edm::InputTag("generalTracks"));
0631   desc.ifValue(edm::ParameterDescription<int>("opMode", 0, true),
0632                0 >> edm::EmptyGroupDescription() or
0633                    1 >> (edm::ParameterDescription<std::string>("stripRemapFile", "", true) and
0634                          edm::ParameterDescription<std::string>("stripHistogram", "", true) and
0635                          edm::ParameterDescription<std::string>("runString", "", true)))
0636       ->setComment("0 for Analyze, 1 for Remap");
0637 
0638   desc.ifValue(edm::ParameterDescription<int>("analyzeMode", 1, true), edm::allowedValues<int>(1, 2, 3))
0639       ->setComment("1=Rechits, 2=Digis, 3=Clusters");
0640 
0641   //desc.add<unsigned int>("analyzeMode", 1)->setComment("1=Rechits, 2=Digis, 3=Clusters");
0642   //desc.add<unsigned int>("opMode", 0)->setComment("0 for Analyze, 1 for Remap");
0643   //desc.addOptional<std::string>("stripRemapFile","" )->setComment("file name to analyze, will come from the config file");
0644   //desc.addOptional<std::string>("stripHistogram","TkHMap_NumberValidHits" )->setComment("histogram to use to remap");
0645   //desc.addOptional<std::string>("runString", "")->setComment("run number, will come form config file");
0646   descriptions.addWithDefaultLabel(desc);
0647 }
0648 
0649 //define this as a plug-in
0650 DEFINE_FWK_MODULE(TrackerRemapper);