PandaRoot
PndLmdGeometryHelper.h
Go to the documentation of this file.
1 #ifndef LMD_LMDMC_PNDLMDGEOMETRYHELPER_H_
2 #define LMD_LMDMC_PNDLMDGEOMETRYHELPER_H_
3 
4 #include <exception>
5 #include <iostream>
6 #include <map>
7 #include <mutex>
8 #include <string>
9 #include <vector>
10 
11 #include <boost/property_tree/json_parser.hpp>
12 #include <boost/property_tree/ptree.hpp>
13 
14 #include <TGeoManager.h>
15 #include <TVector3.h>
16 
18  // we use small data types here as they can be packed into 8 bytes
19  unsigned char detector_half;
20  unsigned char plane;
21  unsigned char module;
22  unsigned char module_side;
23  unsigned char module_sensor_id;
24 
25  friend std::ostream &operator<<(std::ostream &stream, const PndLmdHitLocationInfo &hit_info)
26  {
27  stream << "detector half: " << (unsigned int)hit_info.detector_half << "\n";
28  stream << "detector plane: " << (unsigned int)hit_info.plane << "\n";
29  stream << "module on half plane: " << (unsigned int)hit_info.module << "\n";
30  stream << "side of module: " << (unsigned int)hit_info.module_side << "\n";
31  stream << "sensor id: " << (unsigned int)hit_info.module_sensor_id << "\n";
32 
33  return stream;
34  }
35 
37  {
38  return std::tie(detector_half, plane, module, module_side, module_sensor_id) < std::tie(o.detector_half, o.plane, o.module, o.module_side, o.module_sensor_id);
39  }
40 };
41 
43  std::string path1;
44  std::string path2;
45  std::string pathModule;
46  int overlapID;
47  int id1;
48  int id2;
49 
50  TGeoHMatrix mat1;
51  TGeoHMatrix mat2;
52 
53  // two dummy HitLocationInfo objects for the sensors
56 
57  friend std::ostream &operator<<(std::ostream &stream, const PndLmdOverlapInfo &overlap_info)
58  {
59  stream << "path 1: " << overlap_info.path1 << "\n";
60  stream << "path 2: " << overlap_info.path2 << "\n";
61  stream << "module path: " << overlap_info.pathModule << "\n";
62  stream << "id 1: " << overlap_info.id1 << "\n";
63  stream << "id 2: " << overlap_info.id2 << "\n";
64  stream << "overlapID: " << overlap_info.overlapID << "\n";
65  stream << "matrix 1: "
66  << "\n";
67  stream << "matrix 2: "
68  << "\n";
69  return stream;
70  }
71 };
72 
74  boost::property_tree::ptree geometry_properties;
75  std::vector<std::pair<std::string, bool>> navigation_paths;
76 
77  std::map<std::string, PndLmdHitLocationInfo> volume_path_to_hit_info_mapping;
78  std::map<int, PndLmdHitLocationInfo> sensor_id_to_hit_info_mapping;
79 
80  std::string lmd_root_path;
81 
82  TGeoManager *fGeoManager;
83 
84  std::mutex accessMutex;
85 
86  PndLmdGeometryHelper(const std::string &geo_params_config_file_url = "") : fGeoManager(gGeoManager)
87  {
88  std::string file_url(geo_params_config_file_url);
89  if (geo_params_config_file_url == "") {
90  file_url = std::getenv("VMCWORKDIR");
91  file_url += "/macro/detectors/lmd/lmd-geo-params.json"; //FIXME: Put to input/ or params/
92  }
93  // load parameters
94  try {
95  read_json(file_url, geometry_properties);
96  } catch (std::exception &e) {
97  std::cerr << "PndLmdGeometryHelper::PndLmdGeometryHelper(): ERROR! Parameter file not present!\n";
98  std::cerr << "Was looking for file: " << file_url << "\n";
99  exit(1);
100  }
101 
102  if (!fGeoManager) {
103  std::cerr << "ERROR! gGeoManager is unitialized! Please populate the gGeoManager or initialize a run!\n";
104  exit(2);
105  }
106 
107  auto pt_general = geometry_properties.get_child("general");
108  for (boost::property_tree::ptree::value_type &nav_path : pt_general.get_child("navigation_paths")) {
109  navigation_paths.push_back(std::make_pair(nav_path.second.get<std::string>("name"), nav_path.second.get<bool>("is_alignable")));
110  }
111 
112  TString actPath = fGeoManager->GetPath();
113  std::stringstream lmd_path;
114  fGeoManager->CdTop();
115  lmd_path << fGeoManager->GetPath() << "/" << navigation_paths[0].first << "_0";
116  lmd_root_path = lmd_path.str();
117  if (actPath != "" && actPath != " ")
118  fGeoManager->cd(actPath);
119  }
120 
121  const PndLmdHitLocationInfo &createMappingEntry(int sensor_id);
122  const PndLmdHitLocationInfo &createMappingEntry(const std::string &volume_path);
123 
124  PndLmdHitLocationInfo translateVolumePathToHitLocationInfo(const std::string &volume_path) const;
125 
126  public:
128  {
129  static PndLmdGeometryHelper instance;
130  return instance;
131  }
132 
133  virtual ~PndLmdGeometryHelper();
134 
135  PndLmdGeometryHelper(const PndLmdGeometryHelper &) = delete;
136  void operator=(const PndLmdGeometryHelper &) = delete;
137 
138  const PndLmdHitLocationInfo &getHitLocationInfo(const std::string &volume_path);
139  // this function with the global sensor id is much faster for lookups
140  // so use that if you need speed!
141 
142  const PndLmdHitLocationInfo &getHitLocationInfo(int sensor_id);
143 
144  std::vector<int> getAvailableOverlapIDs();
145  int getOverlapIdFromSensorIDs(int id1, int id2);
146  int getSensorOneFromOverlapID(int overlapID);
147  int getSensorTwoFromOverlapID(int overlapID);
148 
149  TVector3 transformPndGlobalToLmdLocal(const TVector3 &vec);
150  TVector3 transformPndGlobalToSensor(const TVector3 &vec, int sensorId);
151 
152  bool isOverlappingArea(const int id1, const int id2);
153 
154  const std::string getPath(unsigned char...);
155 
156  const TGeoHMatrix getMatrixPndGlobalToSensor(const int sensorId);
157  const TGeoHMatrix getMatrixSensorToPndGlobal(const int sensorId);
158 
159  const TGeoHMatrix getMatrixPndGlobalToLmdLocal();
160  const TGeoHMatrix getMatrixLmdLocalToPndGlobal();
161 
162  std::vector<std::string> getAllAlignPaths(bool sensors = true, bool modules = false, bool planes = false, bool halfs = false, bool detector = false);
163 
164  // can be restriced by half, plane and module
165  std::vector<PndLmdOverlapInfo> getOverlapInfos(int iHalf = -1, int iPlane = -1, int iModule = -1);
166 
167  std::vector<std::string> getAllAlignableVolumePaths() const;
168 };
169 
170 #endif /* LMD_LMDMC_PNDLMDGEOMETRYHELPER_H_ */
friend std::ostream & operator<<(std::ostream &stream, const PndLmdOverlapInfo &overlap_info)
static PndLmdGeometryHelper & getInstance()
bool operator<(const PndLmdHitLocationInfo &o)
friend std::ostream & operator<<(std::ostream &stream, const PndLmdHitLocationInfo &hit_info)
PndLmdHitLocationInfo hit1
PndLmdHitLocationInfo hit2