Skip to content

Commit 5da7d06

Browse files
authored
Merge pull request #46456 from mmusich/mm_tkmaps_add-help
Improve plotting utilities to generate Tracker maps given user input
2 parents fd1e760 + 8d5d139 commit 5da7d06

File tree

6 files changed

+224
-26
lines changed

6 files changed

+224
-26
lines changed

DQM/TrackerRemapper/bin/printPixelLayersDisksMap.cc

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "DQM/TrackerRemapper/interface/Phase1PixelMaps.h"
2-
#include <cstdlib>
2+
#include <cstdint> // For uint32_t
3+
#include <cstdlib> // For std::exit
34
#include <fstream>
45
#include <iostream>
56
#include <numeric> // std::accumulate
@@ -10,21 +11,46 @@
1011
#include "TCanvas.h"
1112
#include "TStyle.h"
1213

14+
void showHelp(const std::string& scriptName) {
15+
std::cout << "Usage: " << scriptName << " [options] <detid>\n"
16+
<< "Options:\n"
17+
<< " --input-file <filename> Specify the input file\n"
18+
<< " --h or --help Show this help message\n"
19+
<< " <detid> Provide DetId (list of DetIds)\n";
20+
}
21+
1322
int main(int argc, char* argv[]) {
1423
std::string inputFile;
1524
std::vector<std::pair<uint32_t, float>> detidValues;
1625

26+
// If no arguments are passed or --h/--help is passed, show the help message
27+
if (argc == 1) {
28+
showHelp(argv[0]);
29+
return 0;
30+
}
31+
1732
// Parse command line arguments
1833
for (int i = 1; i < argc; ++i) {
19-
if (std::string(argv[i]) == "--input-file" && i + 1 < argc) {
34+
std::string arg = argv[i];
35+
36+
if (arg == "--h" || arg == "--help") {
37+
showHelp(argv[0]);
38+
return 0; // Exit after displaying help
39+
} else if (arg == "--input-file" && i + 1 < argc) {
2040
gStyle->SetPalette(kRainbow);
2141
gStyle->SetNumberContours(256);
2242
inputFile = argv[++i];
2343
} else {
2444
gStyle->SetPalette(1);
2545
// Treat as DetId list if no --input-file is provided
26-
uint32_t detid = std::stoul(argv[i]);
27-
detidValues.emplace_back(detid, 1.0); // Default value is 1.0
46+
try {
47+
uint32_t detid = std::stoul(arg);
48+
detidValues.emplace_back(detid, 1.0); // Default value is 1.0
49+
} catch (const std::invalid_argument&) {
50+
std::cerr << "Invalid DetId: " << arg << "\n";
51+
showHelp(argv[0]);
52+
return 1;
53+
}
2854
}
2955
}
3056

DQM/TrackerRemapper/bin/printPixelROCsMap.cc

Lines changed: 95 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "DQM/TrackerRemapper/interface/Phase1PixelROCMaps.h"
22
#include <bitset>
3-
#include <cstdlib>
3+
#include <cstdint> // for uint32_t
4+
#include <cstdlib> // for std::exit
45
#include <fstream>
56
#include <iostream>
67
#include <numeric> // std::accumulate
@@ -11,27 +12,94 @@
1112
#include "TCanvas.h"
1213
#include "TStyle.h"
1314

15+
// Define an enum for region
16+
enum class Region {
17+
Barrel = 0, // Assume 0 for barrel
18+
Forward = 1, // 1 for forward
19+
Full = 2 // 2 for full
20+
};
21+
22+
void showHelp(const std::string& scriptName) {
23+
std::cout << "Usage: " << scriptName << " [options] <detid>\n"
24+
<< " --input-file <filename> Specify the input file\n"
25+
<< " --input-ROCs <filename> Specify the input ROCs file\n"
26+
<< " --region <barrel|forward|full> Specify the region (default: full)\n"
27+
<< " --h or --help Show this help message\n"
28+
<< " <detid> Provide DetId (list of DetIds)\n";
29+
}
30+
31+
// Helper function to convert region enum to string (for displaying)
32+
std::string regionToString(Region region) {
33+
switch (region) {
34+
case Region::Barrel:
35+
return "barrel";
36+
case Region::Forward:
37+
return "forward";
38+
case Region::Full:
39+
return "full";
40+
default:
41+
return "unknown";
42+
}
43+
}
44+
45+
// Helper function to parse region from string
46+
Region parseRegion(const std::string& regionStr) {
47+
if (regionStr == "barrel") {
48+
return Region::Barrel;
49+
} else if (regionStr == "forward") {
50+
return Region::Forward;
51+
} else if (regionStr == "full") {
52+
return Region::Full;
53+
} else {
54+
throw std::invalid_argument("Invalid region value");
55+
}
56+
}
57+
1458
int main(int argc, char* argv[]) {
59+
static constexpr std::array<int, 3> k_height = {{1200, 600, 1600}};
60+
1561
std::string inputFile;
1662
std::string inputROCsFile;
63+
Region region = Region::Full; // Default value: Full region
1764
std::vector<std::pair<uint32_t, float>> detidValues;
1865
std::vector<std::pair<uint32_t, std::bitset<16>>> detidRocs;
1966

67+
// If no arguments are passed or --h/--help is passed, show the help message
68+
if (argc == 1) {
69+
showHelp(argv[0]);
70+
return 0;
71+
}
72+
2073
// Parse command line arguments
2174
for (int i = 1; i < argc; ++i) {
22-
if (std::string(argv[i]) == "--input-file" && i + 1 < argc) {
23-
gStyle->SetPalette(kRainbow);
24-
gStyle->SetNumberContours(256);
75+
std::string arg = argv[i];
76+
77+
if (arg == "--h" || arg == "--help") {
78+
showHelp(argv[0]);
79+
return 0; // Exit after displaying help
80+
} else if (arg == "--input-file" && i + 1 < argc) {
2581
inputFile = argv[++i];
26-
} else if (std::string(argv[i]) == "--input-ROCs" && i + 1 < argc) {
27-
gStyle->SetPalette(kRainBow);
28-
gStyle->SetNumberContours(256);
82+
} else if (arg == "--input-ROCs" && i + 1 < argc) {
2983
inputROCsFile = argv[++i];
84+
} else if (arg == "--region" && i + 1 < argc) {
85+
std::string regionArg = argv[++i];
86+
try {
87+
region = parseRegion(regionArg); // Parse region from string
88+
} catch (const std::invalid_argument&) {
89+
std::cerr << "Invalid value for --region: " << regionArg << "\n";
90+
showHelp(argv[0]);
91+
return 1;
92+
}
3093
} else {
31-
gStyle->SetPalette(1);
32-
// Treat as DetId list if no --input-file is provided
33-
uint32_t detid = std::stoul(argv[i]);
34-
detidValues.emplace_back(detid, 1.0); // Default value is 1.0
94+
// Assume it's a DetId, convert to uint32_t
95+
try {
96+
uint32_t detid = std::stoul(arg);
97+
detidValues.emplace_back(detid, 1.0); // Default value is 1.0
98+
} catch (const std::invalid_argument&) {
99+
std::cerr << "Invalid argument: " << arg << "\n";
100+
showHelp(argv[0]);
101+
return 1;
102+
}
35103
}
36104
}
37105

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

167+
// Construct the full label string
168+
std::string title = "Marked Pixel ROCs - " + regionToString(region);
169+
99170
// Draw and save the map
100-
TCanvas canvas("Summary", "Summary", 1200, 1600);
101-
theMap.drawMaps(canvas, "Marked Pixel ROCs");
102-
canvas.SaveAs("Phase1PixelROCMap.png");
171+
TCanvas canvas("Summary", "Summary", 1200, k_height[static_cast<int>(region)]);
172+
if (region == Region::Full) {
173+
theMap.drawMaps(canvas, title.c_str());
174+
} else if (region == Region::Barrel) {
175+
theMap.drawBarrelMaps(canvas, title.c_str());
176+
} else if (region == Region::Forward) {
177+
theMap.drawForwardMaps(canvas, title.c_str());
178+
}
179+
180+
// Construct the filename string based on the region
181+
std::string fileName = "Phase1PixelROCMap_" + regionToString(region) + ".png";
182+
// Save the canvas with the constructed filename
183+
canvas.SaveAs(fileName.c_str());
103184

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

DQM/TrackerRemapper/bin/printPixelTrackerMap.cc

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "DQM/TrackerRemapper/interface/Phase1PixelSummaryMap.h"
2-
#include <cstdlib>
2+
#include <cstdint> // For uint32_t
3+
#include <cstdlib> // For std::exit
34
#include <fstream>
45
#include <iostream>
56
#include <numeric> // std::accumulate
@@ -10,21 +11,45 @@
1011
#include "TCanvas.h"
1112
#include "TStyle.h"
1213

14+
void showHelp(const std::string& scriptName) {
15+
std::cout << "Usage: " << scriptName << " [options] <detid>\n"
16+
<< " --input-file <filename> Specify the input file\n"
17+
<< " --h or --help Show this help message\n"
18+
<< " <detid> Provide DetId (list of DetIds)\n";
19+
}
20+
1321
int main(int argc, char* argv[]) {
1422
std::string inputFile;
1523
std::vector<std::pair<uint32_t, float>> detidValues;
1624

25+
// If no arguments are passed or --h/--help is passed, show the help message
26+
if (argc == 1) {
27+
showHelp(argv[0]);
28+
return 0;
29+
}
30+
1731
// Parse command line arguments
1832
for (int i = 1; i < argc; ++i) {
19-
if (std::string(argv[i]) == "--input-file" && i + 1 < argc) {
33+
std::string arg = argv[i];
34+
35+
if (arg == "--h" || arg == "--help") {
36+
showHelp(argv[0]);
37+
return 0; // Exit after displaying help
38+
} else if (arg == "--input-file" && i + 1 < argc) {
2039
gStyle->SetPalette(kRainbow);
2140
gStyle->SetNumberContours(256);
2241
inputFile = argv[++i];
2342
} else {
2443
gStyle->SetPalette(1);
2544
// Treat as DetId list if no --input-file is provided
26-
uint32_t detid = std::stoul(argv[i]);
27-
detidValues.emplace_back(detid, 1.0); // Default value is 1.0
45+
try {
46+
uint32_t detid = std::stoul(arg);
47+
detidValues.emplace_back(detid, 1.0); // Default value is 1.0
48+
} catch (const std::invalid_argument&) {
49+
std::cerr << "Invalid DetId: " << arg << "\n";
50+
showHelp(argv[0]);
51+
return 1;
52+
}
2853
}
2954
}
3055

DQM/TrackerRemapper/bin/printStripTrackerMap.cc

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "DQM/TrackerRemapper/interface/SiStripTkMaps.h"
2-
#include <cstdlib>
2+
#include <cstdint> // For uint32_t
3+
#include <cstdlib> // For std::exit
34
#include <fstream>
45
#include <iostream>
56
#include <numeric> // std::accumulate
@@ -10,21 +11,45 @@
1011
#include "TCanvas.h"
1112
#include "TStyle.h"
1213

14+
void showHelp(const std::string& scriptName) {
15+
std::cout << "Usage: " << scriptName << " [options] <detid>\n"
16+
<< " --input-file <filename> Specify the input file\n"
17+
<< " --h or --help Show this help message\n"
18+
<< " <detid> Provide DetId (list of DetIds)\n";
19+
}
20+
1321
int main(int argc, char* argv[]) {
1422
std::string inputFile;
1523
std::vector<std::pair<uint32_t, float>> detidValues;
1624

25+
// If no arguments are passed or --h/--help is passed, show the help message
26+
if (argc == 1) {
27+
showHelp(argv[0]);
28+
return 0;
29+
}
30+
1731
// Parse command line arguments
1832
for (int i = 1; i < argc; ++i) {
19-
if (std::string(argv[i]) == "--input-file" && i + 1 < argc) {
33+
std::string arg = argv[i];
34+
35+
if (arg == "--h" || arg == "--help") {
36+
showHelp(argv[0]);
37+
return 0; // Exit after displaying help
38+
} else if (arg == "--input-file" && i + 1 < argc) {
2039
gStyle->SetPalette(kRainbow);
2140
gStyle->SetNumberContours(256);
2241
inputFile = argv[++i];
2342
} else {
2443
gStyle->SetPalette(1);
2544
// Treat as DetId list if no --input-file is provided
26-
uint32_t detid = std::stoul(argv[i]);
27-
detidValues.emplace_back(detid, 1.0); // Default value is 1.0
45+
try {
46+
uint32_t detid = std::stoul(arg);
47+
detidValues.emplace_back(detid, 1.0); // Default value is 1.0
48+
} catch (const std::invalid_argument&) {
49+
std::cerr << "Invalid DetId: " << arg << "\n";
50+
showHelp(argv[0]);
51+
return 1;
52+
}
2853
}
2954
}
3055

DQM/TrackerRemapper/test/BuildFile.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@
77
<use name="root"/>
88
<use name="catch2"/>
99
</bin>
10+
11+
<test name="testPrintTkMaps" command="testPrintTkMaps.sh"/>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/bin/bash
2+
function die { echo $1: status $2; exit $2; }
3+
4+
echo -e "Testing help functions"
5+
printPixelLayersDisksMap --help || die 'failed running printPixelLayersDisksMap --help' $?
6+
printPixelROCsMap --help || die 'failed running printPixelROCsMap --help' $?
7+
printPixelTrackerMap --help || die 'failed running printPixelTrackerMap --help' $?
8+
printStripTrackerMap --help || die 'failed running printStripTrackerMap --help' $?
9+
echo -e "\n"
10+
testPixelFile=$CMSSW_RELEASE_BASE/src/SLHCUpgradeSimulations/Geometry/data/PhaseI/PixelSkimmedGeometry_phase1.txt
11+
# Store the first 50 elements of the first column in a variable
12+
testPixelDetids=$(head -n 50 "$testPixelFile" | cut -d ' ' -f 1 | paste -sd ' ' -)
13+
14+
echo "Using the following pixel DetIds:" $testPixelDetids
15+
echo -e "\n"
16+
echo -e "==== Testing printPixelLayersDisksMap"
17+
printPixelLayersDisksMap --input-file $testPixelFile || die 'failed printPixelLayersDisksMap --input-file' $?
18+
printPixelLayersDisksMap $testPixelDetids || die 'failed printPixelLayersDisksMap $testPixelDetids' $?
19+
echo -e "\n"
20+
echo -e "==== Testing printPixelROCsMap"
21+
printPixelROCsMap --input-file $testPixelFile || die 'failed printPixelROCsMap --input-file' $?
22+
printPixelROCsMap $testPixelDetids || die 'failed printPixelROCsMap $testPixelDetids' $?
23+
printPixelROCsMap $testPixelDetids --region barrel || die 'failed printPixelROCsMap $testPixelDetids --barrel' $?
24+
printPixelROCsMap $testPixelDetids --region forward || die 'failed printPixelROCsMap $testPixelDetids --forward' $?
25+
printPixelROCsMap $testPixelDetids --region full || die 'failed printPixelROCsMap $testPixelDetids --full' $?
26+
echo -e "\n"
27+
echo -e "==== Testing printPixelTrackerMap"
28+
printPixelTrackerMap --input-file $testPixelFile || die 'failed printPixelTrackerMap --input-file' $?
29+
printPixelTrackerMap $testPixelDetids || die 'failed printPixelTrackerMap $testPixelDetids' $?
30+
echo -e "\n"
31+
testStripFile=$CMSSW_RELEASE_BASE/src/CalibTracker/SiStripCommon/data/SiStripDetInfo.dat
32+
# Store the first 50 elements of the first column in a variable
33+
testStripDetids=$(head -n 50 "$testStripFile" | cut -d ' ' -f 1 | paste -sd ' ' -)
34+
35+
echo "Using the following strip DetIds:" $testStripDetids
36+
echo -e "\n"
37+
echo -e "==== Testing printStripTrackerMap"
38+
printStripTrackerMap --input-file $testStripFile || die 'failed printStripTrackerMap --input-file' $?
39+
printStripTrackerMap $testStripDetids || die 'failed printStripTrackerMap $testPixelDetids' $?

0 commit comments

Comments
 (0)