Skip to content

Commit

Permalink
Merge pull request #46456 from mmusich/mm_tkmaps_add-help
Browse files Browse the repository at this point in the history
Improve plotting utilities to generate Tracker maps given user input
  • Loading branch information
cmsbuild authored Oct 24, 2024
2 parents fd1e760 + 8d5d139 commit 5da7d06
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 26 deletions.
34 changes: 30 additions & 4 deletions DQM/TrackerRemapper/bin/printPixelLayersDisksMap.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "DQM/TrackerRemapper/interface/Phase1PixelMaps.h"
#include <cstdlib>
#include <cstdint> // For uint32_t
#include <cstdlib> // For std::exit
#include <fstream>
#include <iostream>
#include <numeric> // std::accumulate
Expand All @@ -10,21 +11,46 @@
#include "TCanvas.h"
#include "TStyle.h"

void showHelp(const std::string& scriptName) {
std::cout << "Usage: " << scriptName << " [options] <detid>\n"
<< "Options:\n"
<< " --input-file <filename> Specify the input file\n"
<< " --h or --help Show this help message\n"
<< " <detid> Provide DetId (list of DetIds)\n";
}

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

// If no arguments are passed or --h/--help is passed, show the help message
if (argc == 1) {
showHelp(argv[0]);
return 0;
}

// Parse command line arguments
for (int i = 1; i < argc; ++i) {
if (std::string(argv[i]) == "--input-file" && i + 1 < argc) {
std::string arg = argv[i];

if (arg == "--h" || arg == "--help") {
showHelp(argv[0]);
return 0; // Exit after displaying help
} else if (arg == "--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
try {
uint32_t detid = std::stoul(arg);
detidValues.emplace_back(detid, 1.0); // Default value is 1.0
} catch (const std::invalid_argument&) {
std::cerr << "Invalid DetId: " << arg << "\n";
showHelp(argv[0]);
return 1;
}
}
}

Expand Down
109 changes: 95 additions & 14 deletions DQM/TrackerRemapper/bin/printPixelROCsMap.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "DQM/TrackerRemapper/interface/Phase1PixelROCMaps.h"
#include <bitset>
#include <cstdlib>
#include <cstdint> // for uint32_t
#include <cstdlib> // for std::exit
#include <fstream>
#include <iostream>
#include <numeric> // std::accumulate
Expand All @@ -11,27 +12,94 @@
#include "TCanvas.h"
#include "TStyle.h"

// Define an enum for region
enum class Region {
Barrel = 0, // Assume 0 for barrel
Forward = 1, // 1 for forward
Full = 2 // 2 for full
};

void showHelp(const std::string& scriptName) {
std::cout << "Usage: " << scriptName << " [options] <detid>\n"
<< " --input-file <filename> Specify the input file\n"
<< " --input-ROCs <filename> Specify the input ROCs file\n"
<< " --region <barrel|forward|full> Specify the region (default: full)\n"
<< " --h or --help Show this help message\n"
<< " <detid> Provide DetId (list of DetIds)\n";
}

// Helper function to convert region enum to string (for displaying)
std::string regionToString(Region region) {
switch (region) {
case Region::Barrel:
return "barrel";
case Region::Forward:
return "forward";
case Region::Full:
return "full";
default:
return "unknown";
}
}

// Helper function to parse region from string
Region parseRegion(const std::string& regionStr) {
if (regionStr == "barrel") {
return Region::Barrel;
} else if (regionStr == "forward") {
return Region::Forward;
} else if (regionStr == "full") {
return Region::Full;
} else {
throw std::invalid_argument("Invalid region value");
}
}

int main(int argc, char* argv[]) {
static constexpr std::array<int, 3> k_height = {{1200, 600, 1600}};

std::string inputFile;
std::string inputROCsFile;
Region region = Region::Full; // Default value: Full region
std::vector<std::pair<uint32_t, float>> detidValues;
std::vector<std::pair<uint32_t, std::bitset<16>>> detidRocs;

// If no arguments are passed or --h/--help is passed, show the help message
if (argc == 1) {
showHelp(argv[0]);
return 0;
}

// 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);
std::string arg = argv[i];

if (arg == "--h" || arg == "--help") {
showHelp(argv[0]);
return 0; // Exit after displaying help
} else if (arg == "--input-file" && i + 1 < argc) {
inputFile = argv[++i];
} else if (std::string(argv[i]) == "--input-ROCs" && i + 1 < argc) {
gStyle->SetPalette(kRainBow);
gStyle->SetNumberContours(256);
} else if (arg == "--input-ROCs" && i + 1 < argc) {
inputROCsFile = argv[++i];
} else if (arg == "--region" && i + 1 < argc) {
std::string regionArg = argv[++i];
try {
region = parseRegion(regionArg); // Parse region from string
} catch (const std::invalid_argument&) {
std::cerr << "Invalid value for --region: " << regionArg << "\n";
showHelp(argv[0]);
return 1;
}
} 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
// Assume it's a DetId, convert to uint32_t
try {
uint32_t detid = std::stoul(arg);
detidValues.emplace_back(detid, 1.0); // Default value is 1.0
} catch (const std::invalid_argument&) {
std::cerr << "Invalid argument: " << arg << "\n";
showHelp(argv[0]);
return 1;
}
}
}

Expand Down Expand Up @@ -96,10 +164,23 @@ int main(int argc, char* argv[]) {
theMap.fillSelectedRocs(detid, rocs, 1.0); // Default value 1.0
}

// Construct the full label string
std::string title = "Marked Pixel ROCs - " + regionToString(region);

// Draw and save the map
TCanvas canvas("Summary", "Summary", 1200, 1600);
theMap.drawMaps(canvas, "Marked Pixel ROCs");
canvas.SaveAs("Phase1PixelROCMap.png");
TCanvas canvas("Summary", "Summary", 1200, k_height[static_cast<int>(region)]);
if (region == Region::Full) {
theMap.drawMaps(canvas, title.c_str());
} else if (region == Region::Barrel) {
theMap.drawBarrelMaps(canvas, title.c_str());
} else if (region == Region::Forward) {
theMap.drawForwardMaps(canvas, title.c_str());
}

// Construct the filename string based on the region
std::string fileName = "Phase1PixelROCMap_" + regionToString(region) + ".png";
// Save the canvas with the constructed filename
canvas.SaveAs(fileName.c_str());

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

Expand Down
33 changes: 29 additions & 4 deletions DQM/TrackerRemapper/bin/printPixelTrackerMap.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "DQM/TrackerRemapper/interface/Phase1PixelSummaryMap.h"
#include <cstdlib>
#include <cstdint> // For uint32_t
#include <cstdlib> // For std::exit
#include <fstream>
#include <iostream>
#include <numeric> // std::accumulate
Expand All @@ -10,21 +11,45 @@
#include "TCanvas.h"
#include "TStyle.h"

void showHelp(const std::string& scriptName) {
std::cout << "Usage: " << scriptName << " [options] <detid>\n"
<< " --input-file <filename> Specify the input file\n"
<< " --h or --help Show this help message\n"
<< " <detid> Provide DetId (list of DetIds)\n";
}

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

// If no arguments are passed or --h/--help is passed, show the help message
if (argc == 1) {
showHelp(argv[0]);
return 0;
}

// Parse command line arguments
for (int i = 1; i < argc; ++i) {
if (std::string(argv[i]) == "--input-file" && i + 1 < argc) {
std::string arg = argv[i];

if (arg == "--h" || arg == "--help") {
showHelp(argv[0]);
return 0; // Exit after displaying help
} else if (arg == "--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
try {
uint32_t detid = std::stoul(arg);
detidValues.emplace_back(detid, 1.0); // Default value is 1.0
} catch (const std::invalid_argument&) {
std::cerr << "Invalid DetId: " << arg << "\n";
showHelp(argv[0]);
return 1;
}
}
}

Expand Down
33 changes: 29 additions & 4 deletions DQM/TrackerRemapper/bin/printStripTrackerMap.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "DQM/TrackerRemapper/interface/SiStripTkMaps.h"
#include <cstdlib>
#include <cstdint> // For uint32_t
#include <cstdlib> // For std::exit
#include <fstream>
#include <iostream>
#include <numeric> // std::accumulate
Expand All @@ -10,21 +11,45 @@
#include "TCanvas.h"
#include "TStyle.h"

void showHelp(const std::string& scriptName) {
std::cout << "Usage: " << scriptName << " [options] <detid>\n"
<< " --input-file <filename> Specify the input file\n"
<< " --h or --help Show this help message\n"
<< " <detid> Provide DetId (list of DetIds)\n";
}

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

// If no arguments are passed or --h/--help is passed, show the help message
if (argc == 1) {
showHelp(argv[0]);
return 0;
}

// Parse command line arguments
for (int i = 1; i < argc; ++i) {
if (std::string(argv[i]) == "--input-file" && i + 1 < argc) {
std::string arg = argv[i];

if (arg == "--h" || arg == "--help") {
showHelp(argv[0]);
return 0; // Exit after displaying help
} else if (arg == "--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
try {
uint32_t detid = std::stoul(arg);
detidValues.emplace_back(detid, 1.0); // Default value is 1.0
} catch (const std::invalid_argument&) {
std::cerr << "Invalid DetId: " << arg << "\n";
showHelp(argv[0]);
return 1;
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions DQM/TrackerRemapper/test/BuildFile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@
<use name="root"/>
<use name="catch2"/>
</bin>

<test name="testPrintTkMaps" command="testPrintTkMaps.sh"/>
39 changes: 39 additions & 0 deletions DQM/TrackerRemapper/test/testPrintTkMaps.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/bash
function die { echo $1: status $2; exit $2; }

echo -e "Testing help functions"
printPixelLayersDisksMap --help || die 'failed running printPixelLayersDisksMap --help' $?
printPixelROCsMap --help || die 'failed running printPixelROCsMap --help' $?
printPixelTrackerMap --help || die 'failed running printPixelTrackerMap --help' $?
printStripTrackerMap --help || die 'failed running printStripTrackerMap --help' $?
echo -e "\n"
testPixelFile=$CMSSW_RELEASE_BASE/src/SLHCUpgradeSimulations/Geometry/data/PhaseI/PixelSkimmedGeometry_phase1.txt
# Store the first 50 elements of the first column in a variable
testPixelDetids=$(head -n 50 "$testPixelFile" | cut -d ' ' -f 1 | paste -sd ' ' -)

echo "Using the following pixel DetIds:" $testPixelDetids
echo -e "\n"
echo -e "==== Testing printPixelLayersDisksMap"
printPixelLayersDisksMap --input-file $testPixelFile || die 'failed printPixelLayersDisksMap --input-file' $?
printPixelLayersDisksMap $testPixelDetids || die 'failed printPixelLayersDisksMap $testPixelDetids' $?
echo -e "\n"
echo -e "==== Testing printPixelROCsMap"
printPixelROCsMap --input-file $testPixelFile || die 'failed printPixelROCsMap --input-file' $?
printPixelROCsMap $testPixelDetids || die 'failed printPixelROCsMap $testPixelDetids' $?
printPixelROCsMap $testPixelDetids --region barrel || die 'failed printPixelROCsMap $testPixelDetids --barrel' $?
printPixelROCsMap $testPixelDetids --region forward || die 'failed printPixelROCsMap $testPixelDetids --forward' $?
printPixelROCsMap $testPixelDetids --region full || die 'failed printPixelROCsMap $testPixelDetids --full' $?
echo -e "\n"
echo -e "==== Testing printPixelTrackerMap"
printPixelTrackerMap --input-file $testPixelFile || die 'failed printPixelTrackerMap --input-file' $?
printPixelTrackerMap $testPixelDetids || die 'failed printPixelTrackerMap $testPixelDetids' $?
echo -e "\n"
testStripFile=$CMSSW_RELEASE_BASE/src/CalibTracker/SiStripCommon/data/SiStripDetInfo.dat
# Store the first 50 elements of the first column in a variable
testStripDetids=$(head -n 50 "$testStripFile" | cut -d ' ' -f 1 | paste -sd ' ' -)

echo "Using the following strip DetIds:" $testStripDetids
echo -e "\n"
echo -e "==== Testing printStripTrackerMap"
printStripTrackerMap --input-file $testStripFile || die 'failed printStripTrackerMap --input-file' $?
printStripTrackerMap $testStripDetids || die 'failed printStripTrackerMap $testPixelDetids' $?

0 comments on commit 5da7d06

Please sign in to comment.