Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-01-22 07:33:59

0001 #include "DQM/TrackerRemapper/interface/Phase1PixelROCMaps.h"
0002 #include <bitset>
0003 #include <cstdint>  // for uint32_t
0004 #include <cstdlib>  // for std::exit
0005 #include <fstream>
0006 #include <iostream>
0007 #include <numeric>  // std::accumulate
0008 #include <sstream>
0009 #include <string>
0010 #include <vector>
0011 
0012 #include "TCanvas.h"
0013 #include "TStyle.h"
0014 
0015 // Define an enum for region
0016 enum class Region {
0017   Barrel = 0,   // Assume 0 for barrel
0018   Forward = 1,  // 1 for forward
0019   Full = 2      // 2 for full
0020 };
0021 
0022 void showHelp(const std::string& scriptName) {
0023   std::cout << "Usage: " << scriptName << " [options] <detid>\n"
0024             << "  --input-file <filename>        Specify the input file\n"
0025             << "  --input-ROCs <filename>        Specify the input ROCs file\n"
0026             << "  --region <barrel|forward|full> Specify the region (default: full)\n"
0027             << "  --h or --help                  Show this help message\n"
0028             << "  <detid>                        Provide DetId (list of DetIds)\n";
0029 }
0030 
0031 // Helper function to convert region enum to string (for displaying)
0032 std::string regionToString(Region region) {
0033   switch (region) {
0034     case Region::Barrel:
0035       return "barrel";
0036     case Region::Forward:
0037       return "forward";
0038     case Region::Full:
0039       return "full";
0040     default:
0041       return "unknown";
0042   }
0043 }
0044 
0045 // Helper function to parse region from string
0046 Region parseRegion(const std::string& regionStr) {
0047   if (regionStr == "barrel") {
0048     return Region::Barrel;
0049   } else if (regionStr == "forward") {
0050     return Region::Forward;
0051   } else if (regionStr == "full") {
0052     return Region::Full;
0053   } else {
0054     throw std::invalid_argument("Invalid region value");
0055   }
0056 }
0057 
0058 int main(int argc, char* argv[]) {
0059   static constexpr std::array<int, 3> k_height = {{1200, 600, 1600}};
0060 
0061   std::string inputFile;
0062   std::string inputROCsFile;
0063   Region region = Region::Full;  // Default value: Full region
0064   std::vector<std::pair<uint32_t, float>> detidValues;
0065   std::vector<std::pair<uint32_t, std::bitset<16>>> detidRocs;
0066 
0067   // If no arguments are passed or --h/--help is passed, show the help message
0068   if (argc == 1) {
0069     showHelp(argv[0]);
0070     return 0;
0071   }
0072 
0073   // Parse command line arguments
0074   for (int i = 1; i < argc; ++i) {
0075     std::string arg = argv[i];
0076 
0077     if (arg == "--h" || arg == "--help") {
0078       showHelp(argv[0]);
0079       return 0;  // Exit after displaying help
0080     } else if (arg == "--input-file" && i + 1 < argc) {
0081       inputFile = argv[++i];
0082     } else if (arg == "--input-ROCs" && i + 1 < argc) {
0083       inputROCsFile = argv[++i];
0084     } else if (arg == "--region" && i + 1 < argc) {
0085       std::string regionArg = argv[++i];
0086       try {
0087         region = parseRegion(regionArg);  // Parse region from string
0088       } catch (const std::invalid_argument&) {
0089         std::cerr << "Invalid value for --region: " << regionArg << "\n";
0090         showHelp(argv[0]);
0091         return 1;
0092       }
0093     } else {
0094       // Assume it's a DetId, convert to uint32_t
0095       try {
0096         uint32_t detid = std::stoul(arg);
0097         detidValues.emplace_back(detid, 1.0);  // Default value is 1.0
0098       } catch (const std::invalid_argument&) {
0099         std::cerr << "Invalid argument: " << arg << "\n";
0100         showHelp(argv[0]);
0101         return 1;
0102       }
0103     }
0104   }
0105 
0106   // If --input-file is provided, read from file
0107   if (!inputFile.empty()) {
0108     std::ifstream file(inputFile);
0109     if (!file) {
0110       std::cerr << "Error: Unable to open input file " << inputFile << std::endl;
0111       return 1;
0112     }
0113 
0114     std::string line;
0115     while (std::getline(file, line)) {
0116       std::istringstream iss(line);
0117       uint32_t detid;
0118       float value = 1.0;  // Default value
0119 
0120       iss >> detid;
0121       if (iss >> value) {  // If a second column exists, read it as value
0122         detidValues.emplace_back(detid, value);
0123       } else {
0124         detidValues.emplace_back(detid, 1.0);
0125       }
0126     }
0127   }
0128 
0129   // If --input-ROCs is provided, read from file
0130   if (!inputROCsFile.empty()) {
0131     std::ifstream file(inputROCsFile);
0132     if (!file) {
0133       std::cerr << "Error: Unable to open input ROCs file " << inputROCsFile << std::endl;
0134       return 1;
0135     }
0136 
0137     std::string line;
0138     while (std::getline(file, line)) {
0139       std::istringstream iss(line);
0140       uint32_t detid;
0141       std::string rocBits;
0142       iss >> detid >> rocBits;
0143 
0144       if (rocBits.length() == 16) {
0145         std::bitset<16> rocs(rocBits);
0146         detidRocs.emplace_back(detid, rocs);
0147       } else {
0148         std::cerr << "Error: Invalid ROC bits string for detid " << detid << std::endl;
0149         return 1;
0150       }
0151     }
0152   }
0153 
0154   // Create the map and fill it
0155   Phase1PixelROCMaps theMap("");
0156 
0157   // Fill with detidValues if --input-file or command line DetIds are used
0158   for (const auto& [detid, value] : detidValues) {
0159     theMap.fillWholeModule(detid, value);
0160   }
0161 
0162   // Fill with detidRocs if --input-ROCs is used
0163   for (const auto& [detid, rocs] : detidRocs) {
0164     theMap.fillSelectedRocs(detid, rocs, 1.0);  // Default value 1.0
0165   }
0166 
0167   // Construct the full label string
0168   std::string title = "Marked Pixel ROCs - " + regionToString(region);
0169 
0170   // Draw and save the map
0171   TCanvas canvas("Summary", "Summary", 1200, k_height[static_cast<int>(region)]);
0172   if (region == Region::Full) {
0173     theMap.drawMaps(canvas, title);
0174   } else if (region == Region::Barrel) {
0175     theMap.drawBarrelMaps(canvas, title);
0176   } else if (region == Region::Forward) {
0177     theMap.drawForwardMaps(canvas, title);
0178   }
0179 
0180   // Construct the filename string based on the region
0181   std::string fileName = "Phase1PixelROCMap_" + regionToString(region) + ".png";
0182   // Save the canvas with the constructed filename
0183   canvas.SaveAs(fileName.c_str());
0184 
0185   std::cout << "Filled Phase1 Pixel ROC map with " << detidValues.size() + detidRocs.size() << " detids." << std::endl;
0186 
0187   return 0;
0188 }