RegCNNImageUtils.cxx
Go to the documentation of this file.
1 #include <vector>
2 #include <iostream>
3 
5 
7  // Set a default image size
8  SetPixelMapSize(2880,500);
9  fViewReverse = {false,false,false};
10  fNViews = 3;
11 }
12 
13 cnn::RegCNNImageUtils::RegCNNImageUtils(unsigned int nWires, unsigned int nTDCs, unsigned int nViews){
14  SetPixelMapSize(2880,500);
15 }
16 
18 
19 }
20 
21 void cnn::RegCNNImageUtils::SetViewReversal(bool reverseX, bool reverseY, bool reverseZ){
22  fViewReverse = {reverseX,reverseY,reverseZ};
23 }
24 
25 void cnn::RegCNNImageUtils::SetViewReversal(std::vector<bool> reverseViews){
26  if(reverseViews.size() != 3){
27  std::cout << "Expected three views for view reversals... using defaults." << std::endl;
28  }
29  else{
30  SetViewReversal(reverseViews[0],reverseViews[1],reverseViews[2]);
31  }
32  return;
33 }
34 
35 void cnn::RegCNNImageUtils::SetPixelMapSize(unsigned int nWires, unsigned int nTDCs){
36  fPixelMapWires = nWires;
37  fPixelMapTDCs = nTDCs;
38 }
39 
40 
42  return charge/500.;
43 }
44 
46 
48 
49  // Strip out the charge vectors and use these
50  std::vector<float> v0pe = pm.fPEX;
51  std::vector<float> v1pe = pm.fPEY;
52  std::vector<float> v2pe = pm.fPEZ;
53 
54  ConvertChargeVectorsToImageVectorF(v0pe, v1pe, v2pe, imageVec);
55 }
56 
57 void cnn::RegCNNImageUtils::ConvertChargeVectorsToImageVectorF(std::vector<float> &v0pe, std::vector<float> &v1pe,
58  std::vector<float> &v2pe, cnn::ImageVectorF &imageVec){
59 
60  cnn::ViewVectorF view0;
61  cnn::ViewVectorF view1;
62  cnn::ViewVectorF view2;
63 
64  ConvertChargeVectorsToViewVectors(v0pe, v1pe, v2pe, view0, view1, view2);
65 
66  cnn::ImageVectorF newImage = BuildImageVectorF(view0,view1,view2);
67 
68  imageVec = newImage;
69 }
70 
71 void cnn::RegCNNImageUtils::ConvertChargeVectorsToViewVectors(std::vector<float> &v0pe, std::vector<float> &v1pe, std::vector<float> &v2pe,
72  cnn::ViewVectorF& view0, cnn::ViewVectorF& view1, cnn::ViewVectorF& view2){
73 
74  // Reverse requested views
75  if(fViewReverse[0]) ReverseView(v0pe);
76  if(fViewReverse[1]) ReverseView(v1pe);
77  if(fViewReverse[2]) ReverseView(v2pe);
78 
79  // Write the values to the three vectors
80  for (unsigned int view = 0; view < fNViews; ++view){
81  cnn::ViewVectorF viewChargeVec;
82  //for (unsigned int wire = 0; wire < fPixelMapWires; ++wire){
83  for (unsigned int wire = 0; wire < fPixelMapWires+1; ++wire){
84  std::vector<float> wireTDCVec;
85  //for (unsigned int time = 0; time < fPixelMapTDCs; ++time){
86  for (unsigned int time = 0; time < fPixelMapTDCs+1; ++time) {
87 
88  // Get the index for the pixel map
89  unsigned int element = time + fPixelMapTDCs * wire;
90  // FIXME: needs to add additional dimensions because of TF model
91  if (time < fPixelMapTDCs && wire < fPixelMapWires){
92  // Scale Charges
93  float val = 0;
94  if(view == 0){ val = ConvertToScaledCharge(v0pe[element]); }
95  if(view == 1){ val = ConvertToScaledCharge(v1pe[element]); }
96  if(view == 2){ val = ConvertToScaledCharge(v2pe[element]); }
97  wireTDCVec.push_back(val);
98  } else {
99  wireTDCVec.push_back(0.);
100  }
101  }
102  viewChargeVec.push_back(wireTDCVec);
103  }
104  if(view == 0) view0 = viewChargeVec;
105  if(view == 1) view1 = viewChargeVec;
106  if(view == 2) view2 = viewChargeVec;
107  }
108 
109  return;
110 
111 }
112 
113 
114 void cnn::RegCNNImageUtils::ReverseView(std::vector<float> &peVec){
115 
116  std::vector<float> vecCopy(peVec.size(),0.);
117 
118  for (unsigned int w = 0; w < fPixelMapWires; ++w)
119  {
120  // Get our new plane number
121  unsigned int newPlane = fPixelMapWires - w - 1;
122 
123  for (unsigned int t = 0; t < fPixelMapTDCs; ++t)
124  {
125  float val = peVec[t + fPixelMapTDCs * w];
126  vecCopy[t + fPixelMapTDCs * newPlane] = val;
127  }
128  }
129 
130  // Copy the values back into the original vector
131  for(unsigned int e = 0; e < peVec.size(); ++e){
132  float val = vecCopy[e];
133  peVec[e] = val;
134  }
135 
136 }
137 
138 
140 
141  // Tensorflow wants things in the arrangement <wires, TDCs, views>
142  cnn::ImageVectorF image;
143  for(unsigned int w = 0; w < v0.size(); ++w){
144  std::vector<std::vector<float> > wireVec;
145  for(unsigned int t = 0; t < v0[0].size(); ++t){
146  std::vector<float> timeVec;
147  timeVec.push_back(v0[w][t]);
148  timeVec.push_back(v1[w][t]);
149  timeVec.push_back(v2[w][t]);
150  wireVec.push_back(timeVec);
151  } // Loop over tdcs
152  image.push_back(wireVec);
153  } // Loop over wires
154 
155 
156 
157  return image;
158 }
159 
160 
void ConvertChargeVectorsToViewVectors(std::vector< float > &v0pe, std::vector< float > &v1pe, std::vector< float > &v2pe, ViewVectorF &view0, ViewVectorF &view1, ViewVectorF &view2)
Base function for conversion of the Pixel Map to our required output format.
std::vector< bool > fViewReverse
Vector of bools to decide if any views need to be reversed.
void ReverseView(std::vector< float > &peVec)
Funtion to actually reverse the view.
unsigned int fNTdc
Number of tdcs, width of pixel map.
Definition: RegPixelMap.h:86
RegPixelMap, basic input to CNN neural net.
Definition: RegPixelMap.h:22
unsigned int fNWire
Number of wires, length of pixel map.
Definition: RegPixelMap.h:84
std::vector< float > fPEZ
Vector of Y PE measurements for pixels.
Definition: RegPixelMap.h:94
void SetPixelMapSize(unsigned int nWires, unsigned int nTDCs)
Set the input pixel map size.
std::vector< std::vector< float > > ViewVectorF
Useful typedefs.
void SetViewReversal(bool reverseX, bool reverseY, bool reverseZ)
Function to set any views that need reversing.
unsigned int fPixelMapWires
Input pixel map sizes.
std::vector< ViewVectorF > ImageVectorF
const double e
std::vector< float > fPEX
Vector of X PE measurements for pixels.
Definition: RegPixelMap.h:92
float ConvertToScaledCharge(float charge)
std::vector< float > fPEY
Vector of Y PE measurements for pixels.
Definition: RegPixelMap.h:93
Utilities for producing images for the RegCNN.
void ConvertPixelMapToImageVectorF(const RegPixelMap &pm, ImageVectorF &imageVec)
Convert a pixel map into an image vector (float version)
ImageVectorF BuildImageVectorF(ViewVectorF v0, ViewVectorF v1, ViewVectorF v2)
Make the image vector from the view vectors.
unsigned int fNViews
Number of views of each event.
unsigned int fPixelMapTDCs
void ConvertChargeVectorsToImageVectorF(std::vector< float > &v0pe, std::vector< float > &v1pe, std::vector< float > &v2pe, ImageVectorF &imageVec)
Float version of conversion for convenience of TF interface.
QTextStream & endl(QTextStream &s)