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