CMS 3D CMS Logo

printPixelROCsMap.cc
Go to the documentation of this file.
2 #include <bitset>
3 #include <cstdint> // for uint32_t
4 #include <cstdlib> // for std::exit
5 #include <fstream>
6 #include <iostream>
7 #include <numeric> // std::accumulate
8 #include <sstream>
9 #include <string>
10 #include <vector>
11 
12 #include "TCanvas.h"
13 #include "TStyle.h"
14 
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 
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)
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 
58 int main(int argc, char* argv[]) {
59  static constexpr std::array<int, 3> k_height = {{1200, 600, 1600}};
60 
62  std::string inputROCsFile;
63  Region region = Region::Full; // Default value: Full region
64  std::vector<std::pair<uint32_t, float>> detidValues;
65  std::vector<std::pair<uint32_t, std::bitset<16>>> detidRocs;
66 
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 
73  // Parse command line arguments
74  for (int i = 1; i < argc; ++i) {
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) {
81  inputFile = argv[++i];
82  } else if (arg == "--input-ROCs" && i + 1 < argc) {
83  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  }
93  } else {
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  }
103  }
104  }
105 
106  // If --input-file is provided, read from file
107  if (!inputFile.empty()) {
108  std::ifstream file(inputFile);
109  if (!file) {
110  std::cerr << "Error: Unable to open input file " << inputFile << std::endl;
111  return 1;
112  }
113 
115  while (std::getline(file, line)) {
116  std::istringstream iss(line);
117  uint32_t detid;
118  float value = 1.0; // Default value
119 
120  iss >> detid;
121  if (iss >> value) { // If a second column exists, read it as value
122  detidValues.emplace_back(detid, value);
123  } else {
124  detidValues.emplace_back(detid, 1.0);
125  }
126  }
127  }
128 
129  // If --input-ROCs is provided, read from file
130  if (!inputROCsFile.empty()) {
131  std::ifstream file(inputROCsFile);
132  if (!file) {
133  std::cerr << "Error: Unable to open input ROCs file " << inputROCsFile << std::endl;
134  return 1;
135  }
136 
138  while (std::getline(file, line)) {
139  std::istringstream iss(line);
140  uint32_t detid;
141  std::string rocBits;
142  iss >> detid >> rocBits;
143 
144  if (rocBits.length() == 16) {
145  std::bitset<16> rocs(rocBits);
146  detidRocs.emplace_back(detid, rocs);
147  } else {
148  std::cerr << "Error: Invalid ROC bits string for detid " << detid << std::endl;
149  return 1;
150  }
151  }
152  }
153 
154  // Create the map and fill it
155  Phase1PixelROCMaps theMap("");
156 
157  // Fill with detidValues if --input-file or command line DetIds are used
158  for (const auto& [detid, value] : detidValues) {
159  theMap.fillWholeModule(detid, value);
160  }
161 
162  // Fill with detidRocs if --input-ROCs is used
163  for (const auto& [detid, rocs] : detidRocs) {
164  theMap.fillSelectedRocs(detid, rocs, 1.0); // Default value 1.0
165  }
166 
167  // Construct the full label string
168  std::string title = "Marked Pixel ROCs - " + regionToString(region);
169 
170  // Draw and save the map
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());
184 
185  std::cout << "Filled Phase1 Pixel ROC map with " << detidValues.size() + detidRocs.size() << " detids." << std::endl;
186 
187  return 0;
188 }
void drawMaps(TCanvas &canvas, const std::string &text="")
void showHelp(const std::string &scriptName)
Region parseRegion(const std::string &regionStr)
void drawBarrelMaps(TCanvas &canvas, const std::string &text="")
A arg
Definition: Factorize.h:31
void fillWholeModule(const uint32_t &detid, double value)
void drawForwardMaps(TCanvas &canvas, const std::string &text="")
std::string regionToString(Region region)
Definition: value.py:1
int main(int argc, char *argv[])
void fillSelectedRocs(const uint32_t &detid, const std::bitset< 16 > &theROCs, double value)
def canvas(sub, attr)
Definition: svgfig.py:482