ATTPCROOT  0.3.0-alpha
A ROOT-based framework for analyzing data from active target detectors
AtMap.cxx
Go to the documentation of this file.
1 #include "AtMap.h"
2 
3 #include <FairLogger.h>
4 
5 #include <Math/Point2D.h>
6 #include <Rtypes.h>
7 #include <TCanvas.h>
8 #include <TDOMParser.h>
9 #include <TH2Poly.h>
10 #include <TObject.h> // for TObject
11 #include <TStyle.h>
12 #include <TXMLDocument.h>
13 #include <TXMLNode.h>
14 
15 #include <boost/multi_array/base.hpp>
16 #include <boost/multi_array/extent_gen.hpp>
17 
18 #include <cstdlib>
19 #include <cstring>
20 #include <iostream>
21 #include <iterator>
22 #include <memory>
23 #include <utility>
24 
25 constexpr auto cRED = "\033[1;31m";
26 constexpr auto cYELLOW = "\033[1;33m";
27 constexpr auto cNORMAL = "\033[0m";
28 constexpr auto cGREEN = "\033[1;32m";
29 
31 std::ostream &operator<<(std::ostream &os, const AtMap::InhibitType &t)
32 {
33  switch (t) {
34  case InhibitType::kNone: os << "kNone"; break;
35  case InhibitType::kTotal: os << "kTotal"; break;
36  case InhibitType::kLowGain: os << "kLowGain"; break;
37  case InhibitType::kXTalk: os << "kXTalk"; break;
38  case InhibitType::kBadPad: os << "kBadPad"; break;
39  }
40  return os;
41 }
42 
43 AtMap::AtMap() : AtPadCoord(boost::extents[10240][3][2]), fPadPlane(nullptr) {}
44 
46 {
47  return GetNearestFPN(GetPadRef(padNum));
48 }
49 
51 {
52  auto fpn = ref;
53  if (ref.ch < 17)
54  fpn.ch = 11;
55  else if (ref.ch < 34)
56  fpn.ch = 22;
57  else if (ref.ch < 51)
58  fpn.ch = 45;
59  else
60  fpn.ch = 56;
61 
62  return fpn;
63 }
64 
65 bool AtMap::IsFPNchannel(const AtPadReference &ref) const
66 {
67  return ref.ch == 11 || ref.ch == 22 || ref.ch == 45 || ref.ch == 56;
68 }
69 
71 {
72  if (fPadPlane == nullptr)
74 
75  return dynamic_cast<TH2Poly *>(fPadPlane->Clone());
76 }
77 
79 {
80  if (fPadPlane == nullptr)
82  auto binNum = fPadPlane->FindBin(point.X(), point.Y());
83  if (binNum < 0)
84  return -1;
85  else
86  return BinToPad(binNum);
87 }
88 
89 Int_t AtMap::GetPadNum(const AtPadReference &PadRef) const
90 {
91 
92  // Option 1: Int key - vector<int> value
93  // std::map<int, std::vector<int>>::const_iterator ite = fPadMap.find(1);
94  // std::string value = it->second;
95  // Option 2: vector<int> key - int value
96 
97  auto its = fPadMap.find(PadRef);
98 
99  // std::cout<<int(fPadMap.find(test) == fPadMap.end())<<endl;
100  if (its == fPadMap.end()) {
101  if (kDebug)
102  std::cerr << " AtTpcMap::GetPadNum - Pad key not found - CoboID : " << PadRef.cobo
103  << " AsadID : " << PadRef.asad << " AgetID : " << PadRef.aget << " ChannelID : " << PadRef.ch
104  << std::endl;
105  return -1;
106  }
107 
108  return (*its).second;
109 }
110 
111 Bool_t AtMap::ParseInhibitMap(TString inimap, AtMap::InhibitType type)
112 {
113  std::ifstream fIni(inimap.Data());
114 
115  Int_t pad = 0;
116 
117  LOG(info) << cYELLOW << __func__ << " - Parsing map for inhibited pads of type " << type << cNORMAL;
118  if (fIni.fail()) {
119  LOG(error) << cRED << " = Warning : No Inhibit Pad Map found! Please, check the path. Current :" << inimap.Data()
120  << cNORMAL;
121  return false;
122  }
123 
124  if (fIniPads.size() != 0)
125  LOG(info) << "Will overriding any existing inhibited pads with new type";
126 
127  while (!fIni.eof()) {
128  fIni >> pad;
129  InhibitPad(pad, type);
130  }
131 
132  LOG(info) << cYELLOW << fIniPads.size() << " pads in inhibition list." << cNORMAL;
133  return true;
134 }
135 
137 {
138  auto pad = fIniPads.find(padRef);
139  if (pad == fIniPads.end() || pad->second < type)
140  fIniPads[padRef] = type;
141 }
142 
144 {
145  auto pad = fIniPads.find(padRef);
146  if (pad == fIniPads.end())
147  return InhibitType::kNone;
148  else
149  return pad->second;
150 }
151 
152 int AtMap::GetPadSize(int padNum)
153 {
154  if (fPadSizeMap.find(padNum) == fPadSizeMap.end())
155  return -1000;
156  return fPadSizeMap[padNum];
157 }
158 void AtMap::ParseAtTPCMap(TXMLNode *node)
159 {
160 
161  Int_t fCoboID = -1000;
162  Int_t fAsadID = -1000;
163  Int_t fAgetID = -1000;
164  Int_t fChannelID = -1000;
165  Int_t fPadID = -1000;
166  Int_t fSizeID = -1000;
167 
168  for (; node; node = node->GetNextNode()) {
169  if (node->GetNodeType() == TXMLNode::kXMLElementNode) { // Element Node
170  if (strcmp(node->GetNodeName(), "CoboID") == 0)
171  fCoboID = atoi(node->GetText());
172  if (strcmp(node->GetNodeName(), "AsadID") == 0)
173  fAsadID = atoi(node->GetText());
174  if (strcmp(node->GetNodeName(), "AgetID") == 0)
175  fAgetID = atoi(node->GetText());
176  if (strcmp(node->GetNodeName(), "ChannelID") == 0)
177  fChannelID = atoi(node->GetText());
178  if (strcmp(node->GetNodeName(), "PadID") == 0)
179  fPadID = atoi(node->GetText());
180  if (strcmp(node->GetNodeName(), "SizeID") == 0)
181  fSizeID = atoi(node->GetText());
182  }
183  }
184  AtPadReference ref = {fCoboID, fAsadID, fAgetID, fChannelID};
185 
186  fPadMap.insert(std::pair<AtPadReference, int>(ref, fPadID));
187  fPadMapInverse.insert(std::pair<int, AtPadReference>(fPadID, ref));
188  fPadSizeMap.insert(std::pair<int, int>(fPadID, fSizeID));
189 }
190 
191 void AtMap::ParseMapList(TXMLNode *node)
192 {
193 
194  for (; node; node = node->GetNextNode()) {
195  if (node->GetNodeType() == TXMLNode::kXMLElementNode) { // Element node
196  if (strcmp(node->GetNodeName(), "e17504_fission") == 0 || strcmp(node->GetNodeName(), "Lookup20150611") == 0 ||
197  strcmp(node->GetNodeName(), "e18505") == 0 || strcmp(node->GetNodeName(), "LookupProto20150331") == 0 ||
198  strcmp(node->GetNodeName(), "LookupProto10Be") == 0 || "ANL2023.xml" ||
199  strcmp(node->GetNodeName(), "LookupProto20181201v2") == 0 ||
200  strcmp(node->GetNodeName(), "LookupProtoX17") == 0 ||
201  strcmp(node->GetNodeName(), "e12014_pad_mapping") == 0 ||
202  strcmp(node->GetNodeName(), "e12014_pad_map_size") == 0 ||
203  strcmp(node->GetNodeName(), "LookupGADGET08232021") == 0 ||
204  strcmp(node->GetNodeName(), "Lookup20141208") == 0 ||
205  strcmp(node->GetNodeName(), "LookupSpecMATnoScint") == 0 ||
206  strcmp(node->GetNodeName(), "LookupSpecMATnoScintHisto") == 0 ||
207  strcmp(node->GetNodeName(), "LookupSpecMATnoScint3seg") == 0 ||
208  strcmp(node->GetNodeName(), "LookupProtoND") == 0) { // TODO Implement this as function parameter
209 
210  ParseAtTPCMap(node->GetChildren());
211  } else
212  std::cout << " AtTpcMap::ParseMapList - Node not found! Check node name" << std::endl;
213  // std::cout <<node->GetNodeName()<<std::endl;
214  }
215  }
216  kIsParsed = true;
217 }
218 
219 Bool_t AtMap::ParseXMLMap(Char_t const *xmlfile)
220 {
221 
222  auto domParser = std::make_unique<TDOMParser>();
223  domParser->SetValidate(false);
224  Int_t parsecode = domParser->ParseFile(xmlfile);
225  if (parsecode < 0) {
226  std::cerr << domParser->GetParseCodeMessage(parsecode) << std::endl;
227  return false;
228  }
229  TXMLNode *node = domParser->GetXMLDocument()->GetRootNode();
230  ParseMapList(node->GetChildren());
231  // itrEnd = pmap.end();
232 
233  LOG(INFO) << "Pad map has an average load of " << fPadMap.load_factor() << " and a max load of "
234  << fPadMap.max_load_factor() << " with buckets " << fPadMap.bucket_count() << " for " << fPadMap.size()
235  << " pads.";
236 
237  return true;
238 }
239 
241 {
242  if (!kIsParsed) {
243 
244  std::cout << " AtTpcMap::DumpAtTPCMap Error : Pad plane has not been generated or parsed - Exiting... "
245  << std::endl;
246 
247  return false;
248  }
249 
250  std::ostream_iterator<int> ii(std::cout, ", ");
251 
252  for (auto &it : this->fPadMap) {
253  std::cout << " [ " << it.second << ", ";
254  std::cout << it.first.cobo << "," << it.first.asad << "," << it.first.aget << "," << it.first.ch;
255  std::cout << "]" << std::endl;
256  ;
257  }
258 
259  return true;
260 }
261 
262 bool AtMap::AddAuxPad(const AtPadReference &ref, std::string auxName)
263 {
264  auto emplacePair = fAuxPadMap.emplace(ref, auxName);
265  std::cout << cGREEN << " Auxiliary channel added " << fAuxPadMap[ref] << " - Hash "
266  << std::hash<AtPadReference>()(ref) << cNORMAL << "\n";
267 
268  return emplacePair.second;
269 }
270 bool AtMap::IsAuxPad(const AtPadReference &ref) const
271 {
272  return fAuxPadMap.find(ref) != fAuxPadMap.end();
273 }
274 std::string AtMap::GetAuxName(const AtPadReference &ref) const
275 {
276  if (IsAuxPad(ref))
277  return fAuxPadMap.find(ref)->second;
278  else
279  return "";
280 }
282 {
283  if (fPadMapInverse.find(padNum) == fPadMapInverse.end())
284  return {};
285  return fPadMapInverse.at(padNum);
286 }
287 
289 {
290  // NOLINTNEXTLINE (memory belongs t root)
291  fPadPlaneCanvas = new TCanvas("padPlaneCanvas", "Pad Plane", 1000, 1000);
292  gStyle->SetPalette(1);
293  fPadPlane->Draw("col");
294 }
295 
AtMap::InhibitType::kBadPad
@ kBadPad
AtMap
Definition: AtMap.h:33
AtPadReference::aget
Int_t aget
Definition: AtPadReference.h:23
AtMap::IsAuxPad
bool IsAuxPad(const AtPadReference &ref) const
Definition: AtMap.cxx:270
AtPadReference::cobo
Int_t cobo
Definition: AtPadReference.h:21
AtMap::GetPadPlane
TH2Poly * GetPadPlane()
Definition: AtMap.cxx:70
AtMap::InhibitPad
void InhibitPad(Int_t padNum, AtMap::InhibitType type)
Definition: AtMap.h:96
AtMap::AtMap
AtMap()
Definition: AtMap.cxx:43
AtPadReference::asad
Int_t asad
Definition: AtPadReference.h:22
ClassImp
ClassImp(AtFindVertex)
AtMap::ParseXMLMap
Bool_t ParseXMLMap(Char_t const *xmlfile)
Definition: AtMap.cxx:219
AtMap::fPadPlaneCanvas
TCanvas * fPadPlaneCanvas
Definition: AtMap.h:46
XYPoint
ROOT::Math::XYPoint XYPoint
Definition: AtPatternCircle2D.cxx:16
AtMap::DumpAtTPCMap
Bool_t DumpAtTPCMap()
Definition: AtMap.cxx:240
AtMap::GetPadNum
Int_t GetPadNum(const AtPadReference &PadRef) const
Definition: AtMap.cxx:89
node
Definition: fastcluster_dm.cxx:213
operator<<
std::ostream & operator<<(std::ostream &os, const AtMap::InhibitType &t)
Definition: AtMap.cxx:31
AtMap::ParseInhibitMap
Bool_t ParseInhibitMap(TString inimap, AtMap::InhibitType type)
Definition: AtMap.cxx:111
AtMap::ParseAtTPCMap
void ParseAtTPCMap(TXMLNode *node)
Definition: AtMap.cxx:158
AtMap::fAuxPadMap
std::unordered_map< AtPadReference, std::string > fAuxPadMap
Definition: AtMap.h:52
AtMap::kDebug
Bool_t kDebug
Definition: AtMap.h:44
AtMap::InhibitType::kLowGain
@ kLowGain
AtMap::kIsParsed
Bool_t kIsParsed
Definition: AtMap.h:43
AtMap::InhibitType
InhibitType
Definition: AtMap.h:109
AtMap::fPadMap
std::unordered_map< AtPadReference, int > fPadMap
Definition: AtMap.h:50
AtMap::drawPadPlane
void drawPadPlane()
Definition: AtMap.cxx:288
cNORMAL
constexpr auto cNORMAL
Definition: AtMap.cxx:27
AtMap::GetNearestFPN
AtPadReference GetNearestFPN(int padNum) const
Definition: AtMap.cxx:45
AtMap::fPadSizeMap
std::map< int, int > fPadSizeMap
Definition: AtMap.h:53
AtMap::InhibitType::kXTalk
@ kXTalk
AtMap::InhibitType::kNone
@ kNone
AtMap::InhibitType::kTotal
@ kTotal
cRED
constexpr auto cRED
Definition: AtMap.cxx:25
cGREEN
constexpr auto cGREEN
Definition: AtMap.cxx:28
AtMap.h
cYELLOW
constexpr auto cYELLOW
Definition: AtMap.cxx:26
AtMap::BinToPad
virtual Int_t BinToPad(Int_t binval)=0
AtMap::fIniPads
std::unordered_map< AtPadReference, AtMap::InhibitType > fIniPads
Definition: AtMap.h:45
AtMap::GeneratePadPlane
virtual void GeneratePadPlane()=0
AtPadReference::ch
Int_t ch
Definition: AtPadReference.h:24
AtMap::IsFPNchannel
bool IsFPNchannel(const AtPadReference &ref) const
Definition: AtMap.cxx:65
AtPadReference
Definition: AtPadReference.h:20
AtMap::fPadMapInverse
std::map< int, AtPadReference > fPadMapInverse
Definition: AtMap.h:51
AtMap::AddAuxPad
bool AddAuxPad(const AtPadReference &ref, std::string auxName)
Definition: AtMap.cxx:262
AtMap::GetAuxName
std::string GetAuxName(const AtPadReference &ref) const
Definition: AtMap.cxx:274
AtMap::ParseMapList
void ParseMapList(TXMLNode *node)
Definition: AtMap.cxx:191
AtMap::GetPadRef
AtPadReference GetPadRef(int padNum) const
Definition: AtMap.cxx:281
AtMap::GetPadSize
Int_t GetPadSize(int padNum)
Definition: AtMap.cxx:152
AtMap::fPadPlane
TH2Poly * fPadPlane
Definition: AtMap.h:47
std::hash< AtPadReference >
Definition: AtPadReference.h:32
AtMap::IsInhibited
AtMap::InhibitType IsInhibited(Int_t PadNum)
Definition: AtMap.h:98