RegPixelMap.cxx
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 ///// \file RegPixelMap.h
3 ///// \brief RegPixelMap for Regression modified from Dominick's PixelMap
4 ///// \author Jianming Bian - bianjm@uci.edu
5 //////////////////////////////////////////////////////////////////////////
6 
7 #include <cassert>
8 #include <iostream>
9 #include "CVN/func/RegPixelMap.h"
10 
11 namespace cvn
12 {
13 
14  RegPixelMap::RegPixelMap(unsigned int nPlane, unsigned int nCell,
15  const Boundary& bound):
16  fNPlane(nPlane),
17  fNCell(nCell),
18  fPE(nPlane*nCell),
19  fPEX(nPlane*nCell),
20  fPEY(nPlane*nCell),
21  fPur(nPlane*nCell),
22  fPurX(nPlane*nCell),
23  fPurY(nPlane*nCell),
24  fLab(nPlane*nCell),
25  fLabX(nPlane*nCell),
26  fLabY(nPlane*nCell),
27  fBound(bound)
28  {}
29 
30 
31  RegPixelMap::RegPixelMap(unsigned int nPlane, unsigned int nCell,
32  unsigned int vtxPlane, unsigned int vtxXCell, unsigned int vtxYCell,
33  const Boundary& bound):
34  fNPlane(nPlane),
35  fNCell(nCell),
36  fVtxPlane(vtxPlane),
37  fVtxXCell(vtxXCell),
38  fVtxYCell(vtxYCell),
39  fPE(nPlane*nCell),
40  fPEX(nPlane*nCell),
41  fPEY(nPlane*nCell),
42  fPur(nPlane*nCell),
43  fPurX(nPlane*nCell),
44  fPurY(nPlane*nCell),
45  fLab(nPlane*nCell),
46  fLabX(nPlane*nCell),
47  fLabY(nPlane*nCell),
48  fBound(bound)
49  {}
50 
51 
52 
53  void RegPixelMap::FillInputVector(float* input) const
54  {
55  unsigned int i = 0;
56 
57  for(const auto& pe:fPE){
58  input[i] = pe;
59  ++i;
60  }
61 
62  }
63 
64 
65  void RegPixelMap::Add(const unsigned int& plane, const unsigned int& cell,
66  const float& pe, const HitType label = kEmptyHit, const double purity=0.0 )
67  {
68  unsigned int view = plane % 2;
69  if(fBound.IsWithin(plane, cell)){
70  fPE[GlobalToIndex(plane,cell)] += pe/2100.;
71  fLab[GlobalToIndex(plane,cell)] = label;
72  fPur[GlobalToIndexSingle(plane,cell)] = purity;
73  if(view==0){
74  fPEX[GlobalToIndexSingle(plane,cell)] += pe/2100.;//Why +=?
75  fLabX[GlobalToIndexSingle(plane,cell)] = label;
76  fPurX[GlobalToIndexSingle(plane,cell)] = purity;
77  }
78  else{
79  fPEY[GlobalToIndexSingle(plane,cell)] += pe/2100.;
80  fLabY[GlobalToIndexSingle(plane,cell)] = label;
81  fPurY[GlobalToIndexSingle(plane,cell)] = purity;
82  }
83  }
84  }
85 
86  void RegPixelMap::Add(const unsigned int plane, const unsigned int cell, const int view,
87  const float pe, const HitType label = kEmptyHit, const double purity=0.0)
88  {
89  if(fBound.IsWithin(plane, cell)){
90  fPE[GlobalToIndex(plane,cell)] += pe/2100.;
91  fLab[GlobalToIndex(plane,cell)] = label;
92  fPur[GlobalToIndexSingle(plane,cell)] = purity;
93  }
94  int localplaneid = plane - (int)fVtxPlane;
95  if(view==0){
96  int localcellid = cell - (int)fVtxXCell;
97  if(localplaneid>=-30&&localplaneid<=120&&localcellid>=-70&&localcellid<=70){
98  int index = 141*(localplaneid+30) + (localcellid+70);
99  if(index>=0&&index<(int)fPEX.size()){
100 // std::cout<<"in PixelMap, view, localplane, localcell, plane, cell, index, pe*50 "<<view<<" "<<localplaneid<<" "<<localcellid<<" "<<(localplaneid+30)/2<<" "<<localcellid+70<<" "<<index<<" "<<pe*50<<std::endl;
101  fPEX[index] = pe;
102  fLabX[index] = label;
103  fPurX[index] = purity;
104  }
105  }
106  }
107  if(view==1) {
108  int localcellid = cell - (int)fVtxYCell;
109  if(localplaneid>=-30&&localplaneid<=120&&localcellid>=-70&&localcellid<=70){
110  int index = 141*(localplaneid+30) + (localcellid+70);
111  if(index>=0&&index<(int)fPEY.size()){
112 // std::cout<<"in PixelMap, view, plane, cell, index, pe*50 "<<view<<" "<<localplaneid<<" "<<localcellid<<" "<<(localplaneid+30-1)/2<<" "<<localcellid+70<<" "<<index<<" "<<pe*50<<std::endl;
113  fPEY[index] = pe;
114  fLabY[index] = label;
115  fPurY[index] = purity;
116  }
117  }
118  }
119  }
120 
121  unsigned int RegPixelMap::GlobalToIndex(const unsigned int& plane,
122  const unsigned int& cell)
123  {
124  unsigned int view = plane % 2;
125 
126  unsigned int internalPlane = plane - fBound.FirstPlane();
127  unsigned int internalCell = cell - fBound.FirstCell(view);
128 
129  unsigned int index = internalPlane * fNCell + internalCell % fNCell;
130 
131  assert(index < fPE.size());
132 
133  return index;
134  }
135 
136  unsigned int RegPixelMap::LocalToIndex(const unsigned int& plane,
137  const unsigned int& cell) const
138  {
139  unsigned int index = plane * fNCell + cell % fNCell;
140 
141  assert(index < fPE.size());
142  return index;
143  }
144 
145  unsigned int RegPixelMap::GlobalToIndexSingle(const unsigned int& plane,
146  const unsigned int& cell)
147  {
148  unsigned int view = plane % 2;
149 
150 // unsigned int internalPlane = ((plane - fBound.FirstPlane())/2);//from standard Pixelmap
151  unsigned int internalPlane = ((plane - fBound.FirstPlane()));
152 
153  unsigned int internalCell = cell - fBound.FirstCell(view);
154 
155  unsigned int index = internalPlane * fNCell + internalCell % fNCell;
156 
157  assert(index < fPEX.size());
158 
159  return index;
160  }
161 
162 
163 
165  {
166 
167  // Start by doing even planes
168  for(unsigned int iCell = 0; iCell < fNCell; ++iCell)
169  {
170  for(unsigned int iPlane = 0; iPlane < fNPlane; iPlane += 2)
171  {
172  unsigned int index = LocalToIndex(iPlane, iCell);
173  if( fPE[index] > 0)
174  {
175  std::cout << "*";
176  }
177  else
178  {
179  std::cout << " ";
180  }
181 
182  }
183  std::cout << std::endl;
184  }
185  // Then do odd planes
186  for(unsigned int iCell = 0; iCell < fNCell; ++iCell)
187  {
188  for(unsigned int iPlane = 1; iPlane < fNPlane; iPlane += 2)
189  {
190  unsigned int index = LocalToIndex(iPlane, iCell);
191  if( fPE[index] > 0)
192  {
193  std::cout << "*";
194  }
195  else
196  {
197  std::cout << " ";
198  }
199 
200  }
201  std::cout << std::endl;
202  }
203 
204  }
205 
206  TH2F* RegPixelMap::ToTH2(TString name) const
207  {
208 
209  // Create a histogram, use twice as many cells to distinguish views
210  TH2F* hist = new TH2F(name, ";Plane;Cell", fNPlane, 0, fNPlane,
211  fNCell*2, 0, fNCell*2);
212  // TH2F* hist = new TH2F("RegPixelMap", ";Plane;Cell", fNPlane, 0, fNPlane,
213  // fNCell*2, 0, fNCell*2);
214 
215  for(unsigned int iPlane = 0; iPlane < fNPlane; ++iPlane)
216  {
217  for(unsigned int iCell = 0; iCell < fNCell; ++iCell)
218  {
219  // Add 1 to in each bin to skip underflow
220  hist->SetBinContent(iPlane+1, iCell + fNCell*(iPlane%2) + 1,
221  fPE[LocalToIndex(iPlane, iCell)]);
222 
223  }
224  }
225  return hist;
226  }
227 
228  TH2F* RegPixelMap::ToLabTH2(TString name) const
229  {
230 
231  // Create a histogram, use twice as many cells to distinguish views
232  TH2F* hist = new TH2F(name, ";Plane;Cell", fNPlane, 0, fNPlane,
233  fNCell*2, 0, fNCell*2);
234 
235  for(unsigned int iPlane = 0; iPlane < fNPlane; ++iPlane)
236  {
237  for(unsigned int iCell = 0; iCell < fNCell; ++iCell)
238  {
239 
240  float pe = fPE[LocalToIndex(iPlane, iCell)];
241  float lab = (float)fLab[LocalToIndex(iPlane, iCell)];
242  // Draw label 0 as non-empty bin
243  if ( pe > 0 && lab < 1 ) lab = 0.1;
244 
245  // Add 1 to in each bin to skip underflow
246  hist->SetBinContent(iPlane+1, iCell + fNCell*(iPlane%2) + 1, lab);
247 
248  }
249  }
250  return hist;
251  }
252 
253  TH2F* RegPixelMap::SingleViewToTH2(const unsigned int& view, TString name) const
254  {
255 
256  TH2F* hist = new TH2F(name, ";Plane;Cell", (fNPlane/2)+(fNPlane%2),
257  0, (fNPlane/2)+(fNPlane%2), fNCell, 0, fNCell);
258 
259  for(unsigned int iPlane = 0; iPlane < ((fNPlane/2)+(fNPlane%2)); ++iPlane)
260  {
261  for(unsigned int iCell = 0; iCell < fNCell; ++iCell)
262  {
263 
264  double pe = (view == 0) ? fPEX[LocalToIndex(iPlane, iCell)] : fPEY[LocalToIndex(iPlane, iCell)];
265 
266  // Add 1 to in each bin to skip underflow
267  hist->SetBinContent(iPlane+1, iCell+1, pe);
268 
269  }
270  }
271  return hist;
272  }
273 
274  TH2F* RegPixelMap::SingleViewToLabTH2(const unsigned int& view, TString name) const
275  {
276 
277  TH2F* hist = new TH2F(name, ";Plane;Cell", (fNPlane/2)+(fNPlane%2),
278  0, (fNPlane/2)+(fNPlane%2), fNCell, 0, fNCell);
279 
280  for(unsigned int iPlane = 0; iPlane < ((fNPlane/2)+(fNPlane%2)); ++iPlane)
281  {
282  for(unsigned int iCell = 0; iCell < fNCell; ++iCell)
283  {
284  double pe = (view == 0) ? fPEX[LocalToIndex(iPlane, iCell)] : fPEY[LocalToIndex(iPlane, iCell)];
285  double lab = (view == 0) ? fLabX[LocalToIndex(iPlane, iCell)] : fLabY[LocalToIndex(iPlane, iCell)];
286 
287  // Draw label 0 as non-empty bin
288  if ( pe > 0 && lab < 1 ) lab = 0.1;
289 
290  // Add 1 to in each bin to skip underflow
291  hist->SetBinContent(iPlane+1, iCell+1, lab);
292 
293  }
294  }
295  return hist;
296  }
297 
298  std::ostream& operator<<(std::ostream& os, const RegPixelMap& m)
299  {
300  os << "RegPixelMap with " << m.NPixel() << " pixels, "
301  << m.NPlane() << " planes"
302  << " by " << m.NCell() << " cells" ;
303  return os;
304  }
305 }
std::vector< HitType > fLabY
Vector of Y Truth labels for pixels.
Definition: RegPixelMap.h:90
int FirstCell(const unsigned int &view) const
Definition: Boundary.h:44
Boundary fBound
Definition: RegPixelMap.h:92
const XML_Char * name
Definition: expat.h:151
std::vector< double > fPurX
Vector of X purity for pixels.
Definition: RegPixelMap.h:86
enum cvn::HType HitType
void Add(const unsigned int &plane, const unsigned int &cell, const float &pe, const HitType label, const double purity)
Definition: RegPixelMap.cxx:65
unsigned int GlobalToIndex(const unsigned int &plane, const unsigned int &cell)
Take global plane, cell (detector) and return index in fPE vector.
unsigned int fNPlane
Number of planes, length of pixel map.
Definition: RegPixelMap.h:76
unsigned int NPixel() const
Total number of pixels in map.
Definition: RegPixelMap.h:36
std::ostream & operator<<(std::ostream &os, const PixelMapProducer &p)
void FillInputVector(float *input) const
Definition: RegPixelMap.cxx:53
TH2F * ToTH2(TString name) const
Return the pixel map as a 2D histogram for visualization.
std::vector< float > fPEX
Vector of X PE measurements for pixels.
Definition: RegPixelMap.h:83
Defines an enumeration for prong classification.
std::vector< HitType > fLabX
Vector of X Truth labels for pixels.
Definition: RegPixelMap.h:89
std::vector< double > fPur
Vector of purity for pixels.
Definition: RegPixelMap.h:85
unsigned int fNCell
Number of cells, width of pixel map.
Definition: RegPixelMap.h:77
const char * label
TH2F * SingleViewToLabTH2(const unsigned int &view, TString name) const
unsigned int GlobalToIndexSingle(const unsigned int &plane, const unsigned int &cell)
Take global plane, cell (detector) and return index in fPE vector.
unsigned int fVtxYCell
Definition: RegPixelMap.h:80
std::vector< HitType > fLab
Vector of Truth labels for pixels.
Definition: RegPixelMap.h:88
unsigned int LocalToIndex(const unsigned int &plane, const unsigned int &cell) const
Take local plane, cell (within map) and return index in fPE vector.
unsigned int NCell() const
Width in cells.
Definition: RegPixelMap.h:33
std::vector< float > fPEY
Vector of Y PE measurements for pixels.
Definition: RegPixelMap.h:84
unsigned int NPlane() const
Length in planes.
Definition: RegPixelMap.h:30
RegPixelMap, basic input to CVN neural net.
Definition: RegPixelMap.h:22
OStream cout
Definition: OStream.cxx:6
unsigned int fVtxPlane
Definition: RegPixelMap.h:78
std::vector< float > fPE
Vector of PE measurements for pixels.
Definition: RegPixelMap.h:82
assert(nhit_max >=nhit_nbins)
TH2F * SingleViewToTH2(const unsigned int &view, TString name) const
unsigned int fVtxXCell
Definition: RegPixelMap.h:79
RegPixelMap for Regression modified from Dominick&#39;s PixelMap.
std::vector< double > fPurY
Vector of Y purity for pixels.
Definition: RegPixelMap.h:87
TH2F * ToLabTH2(TString name) const
bool IsWithin(const unsigned int &plane, const unsigned int &cell, const unsigned int &view=0)
Definition: Boundary.cxx:40
int FirstPlane() const
Definition: Boundary.h:42