RegCVNImageUtils.cxx
Go to the documentation of this file.
1 #include <iostream>
2 
4 
6  // Set a default image size
7  SetPixelMapSize(2880,500);
8  fCompressPixels = false;
9  fViewReverse = {false,false};
10  fNViews = 2;
11 }
12 
13 cvn::RegCVNImageUtils::RegCVNImageUtils(unsigned int nPlanes, unsigned int nCells, unsigned int nViews){
14  SetPixelMapSize(2880,500);
15 }
16 
18 
19 }
20 
21 void cvn::RegCVNImageUtils::SetViewReversal(bool reverseX, bool reverseY){
22  fViewReverse = {reverseX,reverseY};
23 }
24 
25 void cvn::RegCVNImageUtils::SetViewReversal(std::vector<bool> reverseViews){
26  if(reverseViews.size() != 2){
27  std::cout << "Expected two views for view reversals... using defaults." << std::endl;
28  }
29  else{
30  SetViewReversal(reverseViews[0],reverseViews[1]);
31  }
32  return;
33 }
34 
35 void cvn::RegCVNImageUtils::SetPixelMapSize(unsigned int nPlanes, unsigned int nCells){
36  fPixelMapPlanes = nPlanes;
37  fPixelMapCells = nCells;
38 }
39 
40 void cvn::RegCVNImageUtils::SetCompressPixels(bool compressPixels){
41  fCompressPixels = compressPixels;
42 }
43 
45  return charge*100.; //scale from PE to CNN input
46 }
47 
49  cvn::ImageVectorF &imageVec
50  ){
51 
52 // SetPixelMapSize(76, 141);
53  unsigned int pmNPlane = pm.fNPlane;
54  if(fCompressPixels==true) pmNPlane = pm.fNPlane/2 + pm.fNPlane%2;
55 // std::cout<<"pm.fNPlane, pmNPlane = "<<pm.fNPlane<<" "<<pmNPlane<<std::endl;
56  SetPixelMapSize(pmNPlane, pm.fNCell);
57 
58  // Strip out the charge vectors and use these
59  std::vector<float> v0pe = pm.fPEX;
60  std::vector<float> v1pe = pm.fPEY;
61 
62  ConvertChargeVectorsToImageVectorF(v0pe, v1pe, imageVec);
63 }
64 
65 void cvn::RegCVNImageUtils::ConvertChargeVectorsToImageVectorF(std::vector<float> &v0pe, std::vector<float> &v1pe,
66  cvn::ImageVectorF &imageVec){
67 
68  cvn::ViewVectorF view0;
69  cvn::ViewVectorF view1;
70 
71  ConvertChargeVectorsToViewVectors(v0pe, v1pe, view0, view1);
72 
73  cvn::ImageVectorF newImage = BuildImageVectorF(view0,view1);
74 
75  imageVec = newImage;
76 }
77 
78 void cvn::RegCVNImageUtils::ConvertChargeVectorsToViewVectors(std::vector<float> &v0pe, std::vector<float> &v1pe,
79  cvn::ViewVectorF& view0, cvn::ViewVectorF& view1){
80 
81  // Reverse requested views
82 // if(fViewReverse[0]) ReverseView(v0pe);
83 // if(fViewReverse[1]) ReverseView(v1pe);
84 
85  // Write the values to the three vectors
86  for (unsigned int view = 0; view < fNViews; ++view){
87  cvn::ViewVectorF viewChargeVec;
88  unsigned int maxPlane = fPixelMapPlanes;
89  if(fCompressPixels==true) maxPlane = fPixelMapPlanes+1;
90 // for (unsigned int plane = 0; plane < fPixelMapPlanes; ++plane){
91 // for (unsigned int plane = 0; plane < fPixelMapPlanes; ++plane){
92  for (unsigned int plane = 0; plane < maxPlane; ++plane){
93  std::vector<float> planeCellVec;
94  unsigned int maxCell = fPixelMapCells;
95  if(fCompressPixels==true) maxCell = fPixelMapCells+1;
96 // for (unsigned int cell = 0; cell < fPixelMapPlanes; ++cell){
97 // for (unsigned int cell = 0; cell < fPixelMapCells+1; ++cell){
98  for (unsigned int cell = 0; cell < maxCell; ++cell){
99 
100  // Get the index for the pixel map
101  unsigned int element = cell + fPixelMapCells * plane;
102  unsigned int elementx = cell + fPixelMapCells * 2*plane;
103  unsigned int elementy = cell + fPixelMapCells * (2*plane + 1);
104  // FIXME: needs to add additional dimensions because of TF model
105  if (cell < fPixelMapCells && plane < fPixelMapPlanes){
106  // Scale Charges
107  float val = 0;
108  if(view == 0 && elementx < v0pe.size()){
109  if(fCompressPixels == false) val = ConvertToScaledCharge(v0pe[element]);
110  if(fCompressPixels == true) val = ConvertToScaledCharge(v0pe[elementx]);
111 // if(val>0)std::cout<<"view, plane, cell "<<view<<" "<<plane<<" "<<cell<<" "<<elementx<<" "<<val<<std::endl;
112  }
113  if(view == 1 && elementy < v1pe.size()){
114  if(fCompressPixels == false) val = ConvertToScaledCharge(v1pe[element]);
115  if(fCompressPixels == true) val = ConvertToScaledCharge(v1pe[elementy]);
116 // if(val>0)std::cout<<"view, plane, cell "<<view<<" "<<plane<<" "<<cell<<" "<<elementy<<" "<<val<<std::endl;
117  }
118  planeCellVec.push_back(val);
119  } else planeCellVec.push_back(0.);
120  }
121  //cellPlaneVec.push_back(0); // this is to increase 1 dim 280 -> 281
122  viewChargeVec.push_back(planeCellVec);
123  }
124  if(view == 0) view0 = viewChargeVec;
125  if(view == 1) view1 = viewChargeVec;
126  }
127 
128  return;
129 
130 }
131 
132 
133 void cvn::RegCVNImageUtils::ReverseView(std::vector<float> &peVec){
134 
135  std::vector<float> vecCopy(peVec.size(),0.);
136 
137  for (unsigned int w = 0; w < fPixelMapPlanes; ++w)
138  {
139  // Get our new plane number
140  unsigned int newCell = fPixelMapPlanes - w - 1;
141 
142  for (unsigned int t = 0; t < fPixelMapCells; ++t)
143  {
144  float val = peVec[t + fPixelMapCells * w];
145  vecCopy[t + fPixelMapCells * newCell] = val;
146  }
147  }
148 
149  // Copy the values back into the original vector
150  for(unsigned int e = 0; e < peVec.size(); ++e){
151  float val = vecCopy[e];
152  peVec[e] = val;
153  }
154 
155 }
156 
157 
159 
160  // Tensorflow wants things in the arrangement <cells, Planes, views>
162  for(unsigned int w = 0; w < v0.size(); ++w){
163  std::vector<std::vector<float> > planeVec;
164  for(unsigned int t = 0; t < v0[0].size(); ++t){
165  std::vector<float> cellVec;
166  cellVec.push_back(v0[w][t]);
167  cellVec.push_back(v1[w][t]);
168  planeVec.push_back(cellVec);
169  } // Loop over cells
170  image.push_back(planeVec);
171  } // Loop over planes
172 
173 
174 
175  return image;
176 }
177 
178 
float ConvertToScaledCharge(float charge)
std::vector< ViewVectorF > ImageVectorF
void ConvertPixelMapToImageVectorF(const RegPixelMap &pm, ImageVectorF &imageVec)
Convert a pixel map into an image vector (float version)
std::vector< std::vector< float > > ViewVectorF
Useful typedefs.
ImageVectorF BuildImageVectorF(ViewVectorF v0, ViewVectorF v1)
Make the image vector from the view vectors.
unsigned int fNPlane
Number of planes, length of pixel map.
Definition: RegPixelMap.h:76
unsigned int fPixelMapPlanes
Input pixel map sizes.
std::vector< float > fPEX
Vector of X PE measurements for pixels.
Definition: RegPixelMap.h:83
unsigned int fNCell
Number of cells, width of pixel map.
Definition: RegPixelMap.h:77
std::vector< bool > fViewReverse
Vector of bools to decide if any views need to be reversed.
bool fCompressPixels
Compress pixels ?
void ConvertChargeVectorsToViewVectors(std::vector< float > &v0pe, std::vector< float > &v1pe, ViewVectorF &view0, ViewVectorF &view1)
Base function for conversion of the Pixel Map to our required output format.
std::vector< float > fPEY
Vector of Y PE measurements for pixels.
Definition: RegPixelMap.h:84
Utilities for producing images for the RegCVN.
void SetCompressPixels(bool compressPixels)
Set compress pixels.
RegPixelMap, basic input to CVN neural net.
Definition: RegPixelMap.h:22
OStream cout
Definition: OStream.cxx:6
void ConvertChargeVectorsToImageVectorF(std::vector< float > &v0pe, std::vector< float > &v1pe, ImageVectorF &imageVec)
Float version of conversion for convenience of TF interface.
unsigned int fNViews
Number of views of each event.
void SetViewReversal(bool reverseX, bool reverseY)
Function to set any views that need reversing.
void SetPixelMapSize(unsigned int nPlanes, unsigned int nCells)
Set the input pixel map size.
Float_t e
Definition: plot.C:35
Float_t w
Definition: plot.C:20
unsigned int fPixelMapCells
void ReverseView(std::vector< float > &peVec)
Funtion to actually reverse the view.