LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
PixelMap.cxx
Go to the documentation of this file.
1 
8 #include <cassert>
9 #include <iostream>
10 #include <ostream>
11 
12 namespace lcvn {
13 
14  PixelMap::PixelMap(unsigned int nWire, unsigned int nTdc, const Boundary& bound)
15  : fPE(nWire * nTdc)
16  , fPEX(nWire * nTdc)
17  , fPEY(nWire * nTdc)
18  , fPEZ(nWire * nTdc)
19  , fPur(nWire * nTdc)
20  , fPurX(nWire * nTdc)
21  , fPurY(nWire * nTdc)
22  , fPurZ(nWire * nTdc)
23  , fLab(nWire * nTdc)
24  , fLabX(nWire * nTdc)
25  , fLabY(nWire * nTdc)
26  , fLabZ(nWire * nTdc)
27  , fNWire(nWire)
28  , fNTdc(nTdc)
29  , fBound(bound)
30  {
31  fTotHits = 0;
32  }
33 
34  void PixelMap::FillInputVector(float* input) const
35  {
36  unsigned int i = 0;
37 
38  for (const auto& pe : fPE) {
39  input[i] = pe;
40  ++i;
41  }
42  }
43 
44  void PixelMap::Add(const unsigned int& wire,
45  const double& tdc,
46  const unsigned int& view,
47  const double& pe)
48  {
49  const HitType label = kEmptyHit;
50  const double purity = 0.0;
51  if (fBound.IsWithin(wire, tdc, view)) {
52  fPE[GlobalToIndex(wire, tdc, view)] += pe;
53  fLab[GlobalToIndex(wire, tdc, view)] = label;
54  fPur[GlobalToIndexSingle(wire, tdc, view)] = purity;
55  if (view == 0) {
56  fPEX[GlobalToIndexSingle(wire, tdc, view)] += pe; //Why +=?
57  fLabX[GlobalToIndexSingle(wire, tdc, view)] = label;
58  fPurX[GlobalToIndexSingle(wire, tdc, view)] = purity;
59  }
60  if (view == 1) {
61  fPEY[GlobalToIndexSingle(wire, tdc, view)] += pe;
62  fLabY[GlobalToIndexSingle(wire, tdc, view)] = label;
63  fPurY[GlobalToIndexSingle(wire, tdc, view)] = purity;
64  }
65  if (view == 2) {
66  fPEZ[GlobalToIndexSingle(wire, tdc, view)] += pe;
67  fLabZ[GlobalToIndexSingle(wire, tdc, view)] = label;
68  fPurZ[GlobalToIndexSingle(wire, tdc, view)] = purity;
69  }
70  }
71  }
72 
73  unsigned int PixelMap::GlobalToIndex(const unsigned int& wire,
74  const double& tdc,
75  const unsigned int& view)
76  {
77 
78  unsigned int internalWire = wire - fBound.FirstWire(view);
79 
80  double upperTL = fBound.LastTDC(view);
81  double lowerTL = fBound.FirstTDC(view);
82  double timestep = (upperTL - lowerTL) / double(fNTdc);
83  double roundChannel = round((tdc - lowerTL) / timestep);
84 
85  unsigned int internalTdc = roundChannel;
86 
87  unsigned int index = internalWire * fNTdc + internalTdc % fNTdc;
88 
89  assert(index < fPE.size());
90 
91  return index;
92  }
93 
94  unsigned int PixelMap::LocalToIndex(const unsigned int& wire, const unsigned int& tdc) const
95  {
96  unsigned int index = wire * fNTdc + tdc % fNTdc;
97 
98  assert(index < fPE.size());
99  return index;
100  }
101 
102  unsigned int PixelMap::GlobalToIndexSingle(const unsigned int& wire,
103  const double& tdc,
104  const unsigned int& view)
105 
106  {
107 
108  unsigned int internalWire = wire - fBound.FirstWire(view);
109 
110  double upperTL = fBound.LastTDC(view);
111  double lowerTL = fBound.FirstTDC(view);
112  double timestep = (upperTL - lowerTL) / double(fNTdc);
113  double roundChannel = round((tdc - lowerTL) / timestep);
114 
115  unsigned int internalTdc = roundChannel;
116 
117  unsigned int index = internalWire * fNTdc + internalTdc % fNTdc;
118 
119  assert(index < fPEX.size());
120 
121  return index;
122  }
123 
124  void PixelMap::Print() const
125  {
126 
127  // Start by doing even wires
128  for (unsigned int iTdc = 0; iTdc < fNTdc; ++iTdc) {
129  for (unsigned int iWire = 0; iWire < fNWire; iWire += 2) {
130  unsigned int index = LocalToIndex(iWire, iTdc);
131  if (fPE[index] > 0) { std::cout << "*"; }
132  else {
133  std::cout << " ";
134  }
135  }
136  std::cout << std::endl;
137  }
138  // Then do odd wires
139  for (unsigned int iTdc = 0; iTdc < fNTdc; ++iTdc) {
140  for (unsigned int iWire = 1; iWire < fNWire; iWire += 2) {
141  unsigned int index = LocalToIndex(iWire, iTdc);
142  if (fPE[index] > 0) { std::cout << "*"; }
143  else {
144  std::cout << " ";
145  }
146  }
147  std::cout << std::endl;
148  }
149  }
150 
151  TH2F* PixelMap::ToTH2() const
152  {
153 
154  // Create a histogram, use twice as many tdcs to distinguish views
155  TH2F* hist = new TH2F("PixelMap", ";Wire;Tdc", fNWire, 0, fNWire, fNTdc * 3, 0, fNTdc * 3);
156 
157  for (unsigned int iWire = 0; iWire < fNWire; ++iWire) {
158  for (unsigned int iTdc = 0; iTdc < fNTdc; ++iTdc) {
159  // Add 1 to in each bin to skip underflow
160  hist->SetBinContent(
161  iWire + 1, iTdc + fNTdc * (iWire % 3) + 1, fPE[LocalToIndex(iWire, iTdc)]);
162  }
163  }
164  return hist;
165  }
166 
167  TH2F* PixelMap::ToLabTH2() const
168  {
169 
170  // Create a histogram, use twice as many tdcs to distinguish views
171  TH2F* hist = new TH2F("PixelMap", ";Wire;Tdc", fNWire, 0, fNWire, fNTdc * 3, 0, fNTdc * 3);
172 
173  for (unsigned int iWire = 0; iWire < fNWire; ++iWire) {
174  for (unsigned int iTdc = 0; iTdc < fNTdc; ++iTdc) {
175  // Add 1 to in each bin to skip underflow
176  hist->SetBinContent(
177  iWire + 1, iTdc + fNTdc * (iWire % 3) + 1, (double)fLab[LocalToIndex(iWire, iTdc)]);
178  }
179  }
180  return hist;
181  }
182 
183  TH2F* PixelMap::SingleViewToTH2(const unsigned int& view) const
184  {
185 
186  // Create a histogram
187  TH2F* hist = new TH2F("PixelMap", ";Wire;Tdc", fNWire, 0, fNWire, fNTdc, 0, fNTdc);
188 
189  for (unsigned int iWire = 0; iWire < fNWire; ++iWire) {
190  for (unsigned int iTdc = 0; iTdc < fNTdc; ++iTdc) {
191  // Add 1 to in each bin to skip underflow
192  if (view == 0) {
193  hist->SetBinContent(iWire + 1, iTdc + 1, fPEX[LocalToIndex(iWire, iTdc)]);
194  }
195  if (view == 1) {
196  hist->SetBinContent(iWire + 1, iTdc + 1, fPEY[LocalToIndex(iWire, iTdc)]);
197  }
198  if (view == 2) {
199  hist->SetBinContent(iWire + 1, iTdc + 1, fPEZ[LocalToIndex(iWire, iTdc)]);
200  }
201  }
202  }
203  return hist;
204  }
205 
206  std::ostream& operator<<(std::ostream& os, const PixelMap& m)
207  {
208  os << "PixelMap with " << m.NPixel() << " pixels, " << m.NWire() << " wires"
209  << " by " << m.NTdc() << " tdcs";
210  return os;
211  }
212 }
unsigned int GlobalToIndex(const unsigned int &wire, const double &tdc, const unsigned int &view)
Take global wire, tdc (detector) and return index in fPE vector.
Definition: PixelMap.cxx:73
TH2F * ToLabTH2() const
Definition: PixelMap.cxx:167
void FillInputVector(float *input) const
Definition: PixelMap.cxx:34
std::vector< float > fPE
Vector of PE measurements for pixels.
Definition: PixelMap.h:73
std::vector< HitType > fLab
Vector of Truth labels for pixels.
Definition: PixelMap.h:81
std::vector< double > fPurY
Vector of Y purity for pixels.
Definition: PixelMap.h:79
PixelMap, basic input to CVN neural net.
Definition: PixelMap.h:20
Utility class for truth labels.
std::vector< double > fPurX
Vector of X purity for pixels.
Definition: PixelMap.h:78
PixelMap for CVN.
TH2F * ToTH2() const
Return the pixel map as a 2D histogram for visualization.
Definition: PixelMap.cxx:151
int FirstWire(const unsigned int view) const
Definition: Boundary.h:37
std::vector< double > fPur
Vector of purity for pixels.
Definition: PixelMap.h:77
unsigned int LocalToIndex(const unsigned int &wire, const unsigned int &tdc) const
Take local wire, tdc (within map) and return index in fPE vector.
Definition: PixelMap.cxx:94
double FirstTDC(const unsigned int view) const
Definition: Boundary.h:39
std::vector< float > fPEX
Vector of X PE measurements for pixels.
Definition: PixelMap.h:74
unsigned int NWire() const
Length in wires.
Definition: PixelMap.h:26
std::vector< HitType > fLabX
Vector of X Truth labels for pixels.
Definition: PixelMap.h:82
double LastTDC(const unsigned int view) const
Definition: Boundary.h:40
void Print() const
Definition: PixelMap.cxx:124
std::vector< float > fPEZ
Vector of Y PE measurements for pixels.
Definition: PixelMap.h:76
std::ostream & operator<<(std::ostream &os, const Boundary &b)
Definition: Boundary.cxx:47
std::vector< HitType > fLabZ
Vector of Y Truth labels for pixels.
Definition: PixelMap.h:84
std::vector< double > fPurZ
Vector of Y purity for pixels.
Definition: PixelMap.h:80
unsigned int fTotHits
Number of hits that make up the pixel map.
Definition: PixelMap.h:89
TH2F * hist
Definition: plot.C:134
TH2F * SingleViewToTH2(const unsigned int &view) const
Definition: PixelMap.cxx:183
HitType
Definition: HitType.h:12
void Add(const unsigned int &wire, const double &tdc, const unsigned int &view, const double &pe)
Definition: PixelMap.cxx:44
unsigned int NTdc() const
Width in tdcs.
Definition: PixelMap.h:29
unsigned int fNWire
Number of wires, length of pixel map.
Definition: PixelMap.h:87
bool IsWithin(const unsigned int wire, const double cell, const unsigned int view)
Definition: Boundary.cxx:40
Boundary fBound
Definition: PixelMap.h:91
unsigned int GlobalToIndexSingle(const unsigned int &wire, const double &tdc, const unsigned int &view)
Take global wire, tdc (detector) and return index in fPE vector.
Definition: PixelMap.cxx:102
std::vector< float > fPEY
Vector of Y PE measurements for pixels.
Definition: PixelMap.h:75
unsigned int fNTdc
Number of tdcs, width of pixel map.
Definition: PixelMap.h:88
unsigned int NPixel() const
Total number of pixels in map.
Definition: PixelMap.h:32
std::vector< HitType > fLabY
Vector of Y Truth labels for pixels.
Definition: PixelMap.h:83