CMS 3D CMS Logo

printStripTrackerMap.cc
Go to the documentation of this file.
2 #include <cstdint> // For uint32_t
3 #include <cstdlib> // For std::exit
4 #include <fstream>
5 #include <iostream>
6 #include <numeric> // std::accumulate
7 #include <sstream>
8 #include <string>
9 #include <vector>
10 
11 #include "TCanvas.h"
12 #include "TStyle.h"
13 
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 
21 int main(int argc, char* argv[]) {
23  std::vector<std::pair<uint32_t, float>> detidValues;
24 
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 
31  // Parse command line arguments
32  for (int i = 1; i < argc; ++i) {
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) {
39  gStyle->SetPalette(kRainbow);
40  gStyle->SetNumberContours(256);
41  inputFile = argv[++i];
42  } else {
43  gStyle->SetPalette(1);
44  // Treat as DetId list if no --input-file is provided
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  }
53  }
54  }
55 
56  // If --input-file is provided, read from file
57  if (!inputFile.empty()) {
58  std::ifstream file(inputFile);
59  if (!file) {
60  std::cerr << "Error: Unable to open input file " << inputFile << std::endl;
61  return 1;
62  }
63 
65  while (std::getline(file, line)) {
66  std::istringstream iss(line);
67  uint32_t detid;
68  float value = 1.0; // Default value
69 
70  iss >> detid;
71  if (iss >> value) { // If a second column exists, read it as value
72  detidValues.emplace_back(detid, value);
73  } else {
74  detidValues.emplace_back(detid, 1.0);
75  }
76  }
77  }
78 
79  // Create the map and fill it
80  SiStripTkMaps theMap("COLZ0 AL");
81  theMap.bookMap("Strip Tracker Map of Marked modules", "input values");
82 
83  for (const auto& [detid, value] : detidValues) {
84  theMap.fill(detid, value);
85  }
86 
87  // Check if all values are the same using a lambda function
88  bool allSame = std::all_of(detidValues.begin(), detidValues.end(), [&](const std::pair<uint32_t, float>& p) {
89  return p.second == detidValues[0].second;
90  });
91 
92  TCanvas c = TCanvas("c", "c");
93  theMap.drawMap(c, "");
94 
95  // adjust the z-axis scale
96  if (allSame)
97  theMap.setZAxisRange(0., detidValues[0].second);
98 
99  c.SaveAs("SiStripsTkMaps.png");
100 
101  std::cout << "Filled tracker map with " << detidValues.size() << " detids." << std::endl;
102 
103  return 0;
104 }
void showHelp(const std::string &scriptName)
int main(int argc, char *argv[])
void bookMap(const std::string mapTitle, const std::string zAxisTitle)
A arg
Definition: Factorize.h:31
U second(std::pair< T, U > const &p)
Definition: value.py:1
void fill(long rawid, double val)
void setZAxisRange(double xmin, double xmax)
Definition: SiStripTkMaps.h:69
void drawMap(TCanvas &canvas, std::string option="")