Skip to content

Commit

Permalink
Merge pull request #45795 from mmusich/mm_printPixelTrackerMap
Browse files Browse the repository at this point in the history
Add plotting utilities to generate Tracker maps given user input
  • Loading branch information
cmsbuild authored Sep 6, 2024
2 parents d79a877 + 68c2b44 commit 43fe74b
Show file tree
Hide file tree
Showing 6 changed files with 367 additions and 0 deletions.
11 changes: 11 additions & 0 deletions DQM/TrackerRemapper/bin/BuildFile.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<flags CXXFLAGS="-g -O3 -lASImage -lMultiProc" />
<use name="DQM/TrackerRemapper"/>
<use name="fmt"/>
<use name="rootcore"/>
<use name="rootgraphics"/>
<use name="rootmath"/>
<lib name="stdc++fs" />
<bin name="printPixelTrackerMap" file="printPixelTrackerMap.cc" />
<bin name="printPixelROCsMap" file="printPixelROCsMap.cc" />
<bin name="printPixelLayersDisksMap" file="printPixelLayersDisksMap.cc" />
<bin name="printStripTrackerMap" file="printStripTrackerMap.cc" />
69 changes: 69 additions & 0 deletions DQM/TrackerRemapper/bin/printPixelLayersDisksMap.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "DQM/TrackerRemapper/interface/Phase1PixelMaps.h"
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <numeric> // std::accumulate
#include <sstream>
#include <string>
#include <vector>

#include "TCanvas.h"
#include "TStyle.h"

int main(int argc, char* argv[]) {
std::string inputFile;
std::vector<std::pair<uint32_t, float>> detidValues;

// Parse command line arguments
for (int i = 1; i < argc; ++i) {
if (std::string(argv[i]) == "--input-file" && i + 1 < argc) {
gStyle->SetPalette(kRainbow);
gStyle->SetNumberContours(256);
inputFile = argv[++i];
} else {
gStyle->SetPalette(1);
// Treat as DetId list if no --input-file is provided
uint32_t detid = std::stoul(argv[i]);
detidValues.emplace_back(detid, 1.0); // Default value is 1.0
}
}

// If --input-file is provided, read from file
if (!inputFile.empty()) {
std::ifstream file(inputFile);
if (!file) {
std::cerr << "Error: Unable to open input file " << inputFile << std::endl;
return 1;
}

std::string line;
while (std::getline(file, line)) {
std::istringstream iss(line);
uint32_t detid;
float value = 1.0; // Default value

iss >> detid;
if (iss >> value) { // If a second column exists, read it as value
detidValues.emplace_back(detid, value);
} else {
detidValues.emplace_back(detid, 1.0);
}
}
}

// Create the map and fill it
Phase1PixelMaps theMap("COLZ0A L"); // needed to not show the axis
TCanvas c = TCanvas("c", "c", 1200, 800);
theMap.book("mytest", "Marked modules", "input values");
for (const auto& [detid, value] : detidValues) {
theMap.fill("mytest", detid, value);
}

theMap.beautifyAllHistograms();
theMap.drawSummaryMaps("mytest", c);
c.SaveAs("Phase1PixelMaps_Summary.png");

std::cout << "Filled tracker map with " << detidValues.size() << " detids." << std::endl;

return 0;
}
107 changes: 107 additions & 0 deletions DQM/TrackerRemapper/bin/printPixelROCsMap.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#include "DQM/TrackerRemapper/interface/Phase1PixelROCMaps.h"
#include <bitset>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <numeric> // std::accumulate
#include <sstream>
#include <string>
#include <vector>

#include "TCanvas.h"
#include "TStyle.h"

int main(int argc, char* argv[]) {
std::string inputFile;
std::string inputROCsFile;
std::vector<std::pair<uint32_t, float>> detidValues;
std::vector<std::pair<uint32_t, std::bitset<16>>> detidRocs;

// Parse command line arguments
for (int i = 1; i < argc; ++i) {
if (std::string(argv[i]) == "--input-file" && i + 1 < argc) {
gStyle->SetPalette(kRainbow);
gStyle->SetNumberContours(256);
inputFile = argv[++i];
} else if (std::string(argv[i]) == "--input-ROCs" && i + 1 < argc) {
gStyle->SetPalette(kRainBow);
gStyle->SetNumberContours(256);
inputROCsFile = argv[++i];
} else {
gStyle->SetPalette(1);
// Treat as DetId list if no --input-file is provided
uint32_t detid = std::stoul(argv[i]);
detidValues.emplace_back(detid, 1.0); // Default value is 1.0
}
}

// If --input-file is provided, read from file
if (!inputFile.empty()) {
std::ifstream file(inputFile);
if (!file) {
std::cerr << "Error: Unable to open input file " << inputFile << std::endl;
return 1;
}

std::string line;
while (std::getline(file, line)) {
std::istringstream iss(line);
uint32_t detid;
float value = 1.0; // Default value

iss >> detid;
if (iss >> value) { // If a second column exists, read it as value
detidValues.emplace_back(detid, value);
} else {
detidValues.emplace_back(detid, 1.0);
}
}
}

// If --input-ROCs is provided, read from file
if (!inputROCsFile.empty()) {
std::ifstream file(inputROCsFile);
if (!file) {
std::cerr << "Error: Unable to open input ROCs file " << inputROCsFile << std::endl;
return 1;
}

std::string line;
while (std::getline(file, line)) {
std::istringstream iss(line);
uint32_t detid;
std::string rocBits;
iss >> detid >> rocBits;

if (rocBits.length() == 16) {
std::bitset<16> rocs(rocBits);
detidRocs.emplace_back(detid, rocs);
} else {
std::cerr << "Error: Invalid ROC bits string for detid " << detid << std::endl;
return 1;
}
}
}

// Create the map and fill it
Phase1PixelROCMaps theMap("");

// Fill with detidValues if --input-file or command line DetIds are used
for (const auto& [detid, value] : detidValues) {
theMap.fillWholeModule(detid, value);
}

// Fill with detidRocs if --input-ROCs is used
for (const auto& [detid, rocs] : detidRocs) {
theMap.fillSelectedRocs(detid, rocs, 1.0); // Default value 1.0
}

// Draw and save the map
TCanvas canvas("Summary", "Summary", 1200, 1600);
theMap.drawMaps(canvas, "Marked Pixel ROCs");
canvas.SaveAs("Phase1PixelROCMap.png");

std::cout << "Filled Phase1 Pixel ROC map with " << detidValues.size() + detidRocs.size() << " detids." << std::endl;

return 0;
}
69 changes: 69 additions & 0 deletions DQM/TrackerRemapper/bin/printPixelTrackerMap.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "DQM/TrackerRemapper/interface/Phase1PixelSummaryMap.h"
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <numeric> // std::accumulate
#include <sstream>
#include <string>
#include <vector>

#include "TCanvas.h"
#include "TStyle.h"

int main(int argc, char* argv[]) {
std::string inputFile;
std::vector<std::pair<uint32_t, float>> detidValues;

// Parse command line arguments
for (int i = 1; i < argc; ++i) {
if (std::string(argv[i]) == "--input-file" && i + 1 < argc) {
gStyle->SetPalette(kRainbow);
gStyle->SetNumberContours(256);
inputFile = argv[++i];
} else {
gStyle->SetPalette(1);
// Treat as DetId list if no --input-file is provided
uint32_t detid = std::stoul(argv[i]);
detidValues.emplace_back(detid, 1.0); // Default value is 1.0
}
}

// If --input-file is provided, read from file
if (!inputFile.empty()) {
std::ifstream file(inputFile);
if (!file) {
std::cerr << "Error: Unable to open input file " << inputFile << std::endl;
return 1;
}

std::string line;
while (std::getline(file, line)) {
std::istringstream iss(line);
uint32_t detid;
float value = 1.0; // Default value

iss >> detid;
if (iss >> value) { // If a second column exists, read it as value
detidValues.emplace_back(detid, value);
} else {
detidValues.emplace_back(detid, 1.0);
}
}
}

// Create the map and fill it
Phase1PixelSummaryMap theMap("colz", "Marked Pixel Modules", "input values");
theMap.createTrackerBaseMap();

for (const auto& [detid, value] : detidValues) {
theMap.fillTrackerMap(detid, value);
}

TCanvas c = TCanvas("c", "c", 3000, 2000);
theMap.printTrackerMap(c);
c.SaveAs("Phase1PixelSummaryMap.png");

std::cout << "Filled tracker map with " << detidValues.size() << " detids." << std::endl;

return 0;
}
79 changes: 79 additions & 0 deletions DQM/TrackerRemapper/bin/printStripTrackerMap.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include "DQM/TrackerRemapper/interface/SiStripTkMaps.h"
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <numeric> // std::accumulate
#include <sstream>
#include <string>
#include <vector>

#include "TCanvas.h"
#include "TStyle.h"

int main(int argc, char* argv[]) {
std::string inputFile;
std::vector<std::pair<uint32_t, float>> detidValues;

// Parse command line arguments
for (int i = 1; i < argc; ++i) {
if (std::string(argv[i]) == "--input-file" && i + 1 < argc) {
gStyle->SetPalette(kRainbow);
gStyle->SetNumberContours(256);
inputFile = argv[++i];
} else {
gStyle->SetPalette(1);
// Treat as DetId list if no --input-file is provided
uint32_t detid = std::stoul(argv[i]);
detidValues.emplace_back(detid, 1.0); // Default value is 1.0
}
}

// If --input-file is provided, read from file
if (!inputFile.empty()) {
std::ifstream file(inputFile);
if (!file) {
std::cerr << "Error: Unable to open input file " << inputFile << std::endl;
return 1;
}

std::string line;
while (std::getline(file, line)) {
std::istringstream iss(line);
uint32_t detid;
float value = 1.0; // Default value

iss >> detid;
if (iss >> value) { // If a second column exists, read it as value
detidValues.emplace_back(detid, value);
} else {
detidValues.emplace_back(detid, 1.0);
}
}
}

// Create the map and fill it
SiStripTkMaps theMap("COLZ0 AL");
theMap.bookMap("Strip Tracker Map of Marked modules", "input values");

for (const auto& [detid, value] : detidValues) {
theMap.fill(detid, value);
}

// Check if all values are the same using a lambda function
bool allSame = std::all_of(detidValues.begin(), detidValues.end(), [&](const std::pair<uint32_t, float>& p) {
return p.second == detidValues[0].second;
});

TCanvas c = TCanvas("c", "c");
theMap.drawMap(c, "");

// adjust the z-axis scale
if (allSame)
theMap.setZAxisRange(0., detidValues[0].second);

c.SaveAs("SiStripsTkMaps.png");

std::cout << "Filled tracker map with " << detidValues.size() << " detids." << std::endl;

return 0;
}
32 changes: 32 additions & 0 deletions DQM/TrackerRemapper/src/SiStripTkMaps.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,15 @@ void SiStripTkMaps::bookMap(const std::string mapTitle, const std::string zAxisT
m_trackerMap->GetZaxis()->SetTitle(m_zAxisTitle.c_str());
m_trackerMap->GetZaxis()->CenterTitle();

// Add all bins
for (const auto& pair : m_bins) {
m_trackerMap->AddBin(pair.second->Clone());
}

// Initialize all bins with zero values
for (const auto& pair : m_bins) {
m_trackerMap->Fill(pair.first, 0.0);
}
}

//============================================================================
Expand All @@ -79,6 +85,24 @@ void SiStripTkMaps::drawMap(TCanvas& canvas, std::string option) {
static constexpr int wH_ = 3000;
static constexpr int hH_ = 700;

// Define a special value for "empty" bins
static constexpr double emptyBinValue = -9999;

// Ensure all bins are drawn, including empty ones
for (int i = 1; i <= m_trackerMap->GetNumberOfBins(); ++i) {
if (m_trackerMap->GetBinContent(i) == 0) {
m_trackerMap->SetBinContent(i, emptyBinValue);
}
}

// Adjust the color palette
double minValue = *std::min_element(m_values.begin(), m_values.end());
double maxValue = *std::max_element(m_values.begin(), m_values.end());

// Setting a palette that skips the color for the emptyBinValue
m_trackerMap->SetMinimum(minValue); // Set min to the smallest valid value
m_trackerMap->SetMaximum(maxValue); // Set max to the largest valid value

canvas.cd();
adjustCanvasMargins(canvas.cd(), tmargin_, bmargin_, lmargin_, rmargin_);
canvas.Update();
Expand All @@ -90,6 +114,14 @@ void SiStripTkMaps::drawMap(TCanvas& canvas, std::string option) {
m_trackerMap->Draw();
}

// Set the "empty" bins color to white
for (int i = 1; i <= m_trackerMap->GetNumberOfBins(); ++i) {
if (m_trackerMap->GetBinContent(i) == emptyBinValue) {
m_trackerMap->SetBinContent(i, emptyBinValue);
m_trackerMap->SetMarkerColor(kWhite);
}
}

canvas.SetFrameLineColor(0);
gPad->Update();
TPaletteAxis* palette = (TPaletteAxis*)m_trackerMap->GetListOfFunctions()->FindObject("palette");
Expand Down

0 comments on commit 43fe74b

Please sign in to comment.