Region

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
#include "DQM/TrackerRemapper/interface/Phase1PixelROCMaps.h"
#include <bitset>
#include <cstdint>  // for uint32_t
#include <cstdlib>  // for std::exit
#include <fstream>
#include <iostream>
#include <numeric>  // std::accumulate
#include <sstream>
#include <string>
#include <vector>

#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) {
    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 (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 {
      // 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;
      }
    }
  }

  // If --input-file is provided, read from file
  if (!inputFile.empty()) {
    std::ifstream file(inputFile);
    if (!file) {
      std::cerr << "Error: Unable to open input file " << inputFile << std::endl;
      return 1;
    }

    std::string line;
    while (std::getline(file, line)) {
      std::istringstream iss(line);
      uint32_t detid;
      float value = 1.0;  // Default value

      iss >> detid;
      if (iss >> value) {  // If a second column exists, read it as value
        detidValues.emplace_back(detid, value);
      } else {
        detidValues.emplace_back(detid, 1.0);
      }
    }
  }

  // If --input-ROCs is provided, read from file
  if (!inputROCsFile.empty()) {
    std::ifstream file(inputROCsFile);
    if (!file) {
      std::cerr << "Error: Unable to open input ROCs file " << inputROCsFile << std::endl;
      return 1;
    }

    std::string line;
    while (std::getline(file, line)) {
      std::istringstream iss(line);
      uint32_t detid;
      std::string rocBits;
      iss >> detid >> rocBits;

      if (rocBits.length() == 16) {
        std::bitset<16> rocs(rocBits);
        detidRocs.emplace_back(detid, rocs);
      } else {
        std::cerr << "Error: Invalid ROC bits string for detid " << detid << std::endl;
        return 1;
      }
    }
  }

  // Create the map and fill it
  Phase1PixelROCMaps theMap("");

  // Fill with detidValues if --input-file or command line DetIds are used
  for (const auto& [detid, value] : detidValues) {
    theMap.fillWholeModule(detid, value);
  }

  // Fill with detidRocs if --input-ROCs is used
  for (const auto& [detid, rocs] : detidRocs) {
    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, k_height[static_cast<int>(region)]);
  if (region == Region::Full) {
    theMap.drawMaps(canvas, title);
  } else if (region == Region::Barrel) {
    theMap.drawBarrelMaps(canvas, title);
  } else if (region == Region::Forward) {
    theMap.drawForwardMaps(canvas, title);
  }

  // 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;

  return 0;
}