ATTPCROOT  0.3.0-alpha
A ROOT-based framework for analyzing data from active target detectors
AtTpcProtoMap.cxx
Go to the documentation of this file.
1 /*********************************************************************
2  * ATTPC Mapping Class AtTpcProtoMap.cxx *
3  * Author: Y. Ayyad *
4  * Log: 31-03-2015 11:56 JST *
5  * *
6  *********************************************************************/
7 
8 #include "AtTpcProtoMap.h"
9 
10 #include <Math/Point2D.h>
11 #include <Rtypes.h>
12 #include <TCollection.h>
13 #include <TDirectory.h>
14 #include <TFile.h>
15 #include <TH2Poly.h>
16 #include <TKey.h>
17 #include <TMultiGraph.h>
18 #include <TObject.h>
19 
20 #include <cstdlib>
21 #include <fstream>
22 #include <iostream>
23 #include <memory>
24 #include <utility>
25 
26 constexpr auto cRED = "\033[1;31m";
27 constexpr auto cYELLOW = "\033[1;33m";
28 constexpr auto cNORMAL = "\033[0m";
29 constexpr auto cGREEN = "\033[1;32m";
31 
33 
35 
36 Bool_t AtTpcProtoMap::SetGeoFile(TString geofile)
37 {
38 
39  TString dir = getenv("VMCWORKDIR");
40  TString geodir = dir + "/geometry/" + geofile;
41  f = new TFile(geodir.Data()); // NOLINT
42 
43  if (f->IsZombie()) {
44  std::cout << cRED << " AtTPC Proto Map : No geometry file found! Check VMCWORKDIR variable. Exiting... "
45  << cNORMAL << std::endl; // TODO Not working!!!
46  delete f;
47  return kFALSE;
48  }
49  std::cout << cGREEN << " AtTPC Proto Map : Prototype geometry found in : " << geodir.Data() << cNORMAL << std::endl;
50  kIsFileSet = kTRUE;
51  return kTRUE;
52 }
53 
55 {
56 
57  if (f->IsZombie()) {
58  std::cout
59  << " AtTPC Proto Map : No geometry file found! Please set the geometry file first via SetGeoFile method "
60  << std::endl;
61  return;
62  }
63  if (fPadPlane) {
64  std::cout << "Skipping generation of pad plane, it is already parsed!" << std::endl;
65  return;
66  }
67  fPadPlane = new TH2Poly(); // NOLINT
68  std::cout << " AtTPC Proto Map : Generating the map geometry of the AtTPC Prototype " << std::endl;
69  TMultiGraph *mg = nullptr;
70  TKey *key = nullptr;
71  TIter nextkey(gDirectory->GetListOfKeys());
72  while ((key = dynamic_cast<TKey *>(nextkey()))) {
73  auto *obj = dynamic_cast<TMultiGraph *>(key->ReadObj());
74  if (obj != nullptr)
75  fPadPlane->AddBin(obj);
76  auto padPlane = dynamic_cast<TH2Poly *>(key->ReadObj());
77  if (padPlane != nullptr) {
78  if (fPadPlane != nullptr)
79  delete fPadPlane;
80  fPadPlane = dynamic_cast<TH2Poly *>(padPlane->Clone());
81  break;
82  }
83  /*std::cout << "Using key: " << key << " " << obj << std::endl;
84  if (obj->InheritsFrom("TMultiGraph")) {
85  mg = (TMultiGraph *)obj;
86  bin = fPadPlane->AddBin(mg);
87  // std::cout<<bin<<std::endl;
88  }
89  */
90  }
91 
92  kIsGenerated = kTRUE;
93 }
94 
95 TH2Poly *AtTpcProtoMap::GetAtTpcPlane(TString TH2Poly_name)
96 {
97  // This is a stand alone method
98  if (f->IsZombie()) {
99  std::cout
100  << " AtTPC Proto Map : No geometry file found! Please set the geometry file first via SetGeoFile method "
101  << std::endl;
102  return nullptr;
103  }
104  fPadPlane = dynamic_cast<TH2Poly *>(f->Get(TH2Poly_name.Data()));
105  return fPadPlane;
106 }
107 
109 {
110 
111  if (!kIsProtoMapSet) {
112  std::cout << " AtTPC Proto Map : No map file for prototype found! Please set the geometry file first via the "
113  "SetProtoMap method "
114  << std::endl;
115  return {-9999, -9999};
116  }
117 
118  if (f->IsZombie()) {
119  std::cout
120  << " AtTPC Proto Map : No geometry file found! Please set the geometry file first via the SetGeoFile method "
121  << std::endl;
122  return {-9999, -9999};
123  }
124 
125  if (PadRef != -1) { // Boost multi_array crashes with a negative index
126  auto its = ProtoGeoMap.find(PadRef);
127 
128  Int_t kIs = Int_t(ProtoGeoMap.find(PadRef) == ProtoGeoMap.end());
129  if (kIs) {
130  if (kDebug)
131  std::cerr << " AtTpcProtoMap::CalcPadCenter - Pad not found - CoboID : " << PadRef << std::endl;
132  return {-9999, -9999};
133  }
134 
135  auto padCenter = (*its).second;
136  return {padCenter[0], padCenter[1]};
137 
138  } else {
139 
140  if (kDebug)
141  std::cout << " AtTpcProtoMap::CalcPadCenter Error : Pad not found" << std::endl;
142  return {-9999, -9999};
143  }
144 }
145 
146 Bool_t AtTpcProtoMap::SetProtoMap(TString file)
147 {
148 
149  /* Sets a map for the prototype containing the Pad Number*/
150  TString PadName;
151  Int_t PadNum = -1;
152  Float_t pad_xcoord = -1;
153  Float_t pad_ycoord = -1;
154  Int_t bin_num = -1;
155  std::vector<Float_t> PadCoord;
156  PadCoord.reserve(2);
157  auto InProtoMap = std::make_unique<std::ifstream>(file.Data());
158 
159  if (InProtoMap->fail()) {
160  std::cout << " = AtTpcProtoMap::SetProtoMap : No Prototype Map file found! Please, check the path. Current :"
161  << file.Data() << std::endl;
162  return -1;
163  } else
164  kIsProtoMapSet = kTRUE;
165 
166  while (!InProtoMap->eof()) {
167 
168  *InProtoMap >> PadNum >> PadName >> pad_xcoord >> pad_ycoord >> bin_num;
169  PadCoord.push_back(pad_xcoord);
170  PadCoord.push_back(pad_ycoord);
171  if (kDebug)
172  if (!PadName.IsNull())
173  std::cout << " PadNum : " << PadNum << " - PadName : " << PadName.Data()
174  << " - Pad X coord : " << pad_xcoord << " - Pad Y coord : " << pad_ycoord
175  << " - Bin number : " << bin_num << std::endl;
176  ProtoGeoMap.insert(std::pair<Int_t, std::vector<Float_t>>(PadNum, PadCoord));
177  ProtoBinMap.insert(std::pair<Int_t, Int_t>(bin_num, PadNum));
178  PadCoord.clear();
179  }
180 
181  return kTRUE;
182 }
183 
184 Int_t AtTpcProtoMap::BinToPad(Int_t binval)
185 {
186 
187  if (!kIsProtoMapSet) {
188  std::cout << " = AtTpcProtoMap::BinToPad : No Prototype Map file found! Please, use the SetProtoMap method first."
189  << std::endl;
190  return -1;
191  }
192 
193  auto its = ProtoBinMap.find(binval);
194  Int_t padval = (*its).second;
195  Int_t kIs = int(ProtoBinMap.find(binval) == ProtoBinMap.end());
196  if (kIs) {
197  if (kDebug)
198  std::cerr << " = AtTpcProtoMap::BinToPad - Bin not found : " << binval << std::endl;
199  return -1;
200  } else if (binval > 2014 || binval < 0) {
201 
202  std::cout
203  << " = AtTpcProtoMap::BinToPad - Warning: Bin value out of expected boundaries for prototype bin mapping : "
204  << binval << std::endl;
205  }
206 
207  return padval;
208 }
209 
AtMap
Definition: AtMap.h:33
cRED
constexpr auto cRED
Definition: AtTpcProtoMap.cxx:26
AtTpcProtoMap::SetProtoMap
Bool_t SetProtoMap(TString file)
Definition: AtTpcProtoMap.cxx:146
AtTpcProtoMap::SetGeoFile
Bool_t SetGeoFile(TString geofile)
Definition: AtTpcProtoMap.cxx:36
AtTpcProtoMap::f
TFile * f
Definition: AtTpcProtoMap.h:28
AtTpcProtoMap::ProtoBinMap
std::map< Int_t, Int_t > ProtoBinMap
Definition: AtTpcProtoMap.h:36
AtTpcProtoMap::GetAtTpcPlane
TH2Poly * GetAtTpcPlane(TString TH2Poly_name)
Definition: AtTpcProtoMap.cxx:95
AtTpcProtoMap::BinToPad
virtual Int_t BinToPad(Int_t binval) override
Definition: AtTpcProtoMap.cxx:184
cNORMAL
constexpr auto cNORMAL
Definition: AtTpcProtoMap.cxx:28
XYPoint
ROOT::Math::XYPoint XYPoint
Definition: AtPatternCircle2D.cxx:16
cYELLOW
constexpr auto cYELLOW
Definition: AtTpcProtoMap.cxx:27
AtTpcProtoMap
Definition: AtTpcProtoMap.h:26
cGREEN
constexpr auto cGREEN
Definition: AtTpcProtoMap.cxx:29
AtMap::kDebug
Bool_t kDebug
Definition: AtMap.h:44
AtTpcProtoMap::ProtoGeoMap
std::map< Int_t, std::vector< Float_t > > ProtoGeoMap
Definition: AtTpcProtoMap.h:35
AtTpcProtoMap::kIsGenerated
Bool_t kIsGenerated
Definition: AtTpcProtoMap.h:32
XYPoint
ROOT::Math::XYPoint XYPoint
Definition: AtTpcProtoMap.cxx:30
AtTpcProtoMap::Dump
virtual void Dump() override
Definition: AtTpcProtoMap.cxx:210
ClassImp
ClassImp(AtTpcProtoMap)
AtTpcProtoMap::GeneratePadPlane
virtual void GeneratePadPlane() override
Definition: AtTpcProtoMap.cxx:54
AtTpcProtoMap::kIsProtoMapSet
Bool_t kIsProtoMapSet
Definition: AtTpcProtoMap.h:33
AtTpcProtoMap.h
AtTpcProtoMap::kIsFileSet
Bool_t kIsFileSet
Definition: AtTpcProtoMap.h:31
AtTpcProtoMap::CalcPadCenter
virtual ROOT::Math::XYPoint CalcPadCenter(Int_t PadRef) override
Definition: AtTpcProtoMap.cxx:108
AtTpcProtoMap::AtTpcProtoMap
AtTpcProtoMap()
Definition: AtTpcProtoMap.cxx:34
AtMap::fPadPlane
TH2Poly * fPadPlane
Definition: AtMap.h:47