StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StFttDb.cxx
1 /***************************************************************************
2  * StFttDb.cxx
3  * jdb Feb, 2022
4  ***************************************************************************
5  * Description: This interface between FTT and the STAR database
6  ***************************************************************************/
7 
8 #include "StFttDb.h"
9 #include "StMaker.h"
10 #include "StMessMgr.h"
11 #include "StEvent/StFttRawHit.h"
12 #include "StEvent/StFttCluster.h"
13 #include "StEvent/StFttPoint.h"
14 #include <math.h>
15 
16 #include "tables/St_fttHardwareMap_Table.h"
17 #include "tables/St_fttDataWindows_Table.h"
18 
19 
20 ClassImp(StFttDb)
21 
22 
23 double StFttDb::stripPitch = 3.2; // mm
24 double StFttDb::rowLength = 180; // mm
25 double StFttDb::lowerQuadOffsetX = 101.6; // mm
26 double StFttDb::idealPlaneZLocations[] = { 280.90499, 303.70498, 326.60501, 349.40499 };
27 
28 vector<string> StFttDb::orientationLabels = { "Horizontal", "Vertical", "DiagonalH", "DiagonalV", "Unknown" };
29 
30 
31 StFttDb::StFttDb(const char *name) : TDataSet(name) {};
32 
33 StFttDb::~StFttDb() {}
34 
35 int StFttDb::Init(){
36 
37  return kStOK;
38 }
39 
40 void StFttDb::setDbAccess(int v) {mDbAccess = v;}
41 void StFttDb::setRun(int run) {mRun = run;}
42 
43 int StFttDb::InitRun(int runNumber) {
44  mRun=runNumber;
45  return kStOK;
46 }
47 
48 
49 size_t StFttDb::uuid( StFttRawHit * h, bool includeStrip ) {
50  // this UUID is not really universally unique
51  // it is unique up to the hardware location
52  // at the give precision
53  // NOT including strip level is useful for cluster
54  // calculations that combine all strips from given
55  // plane, quad, row, orientation
56 
57 
58  size_t _uuid = (size_t)h->orientation() + (nStripOrientations) * ( h->row() + nRowsPerQuad * ( h->quadrant() + nQuadPerPlane * h->plane() ) );
59 
60  if ( includeStrip ){
61  _uuid = (size_t) h->strip() * maxStripPerRow *( h->orientation() + (nStripOrientations) * ( h->row() + nRowsPerQuad * ( h->quadrant() + nQuadPerPlane * h->plane() ) ) );
62  }
63 
64  return _uuid;
65 }
66 
67 size_t StFttDb::uuid( StFttCluster * c ) {
68  // this UUID is not really universally unique
69  // it is unique up to the hardware location
70 
71  size_t _uuid = (size_t)c->orientation() + (nStripOrientations) * ( c->row() + nRowsPerQuad * ( c->quadrant() + nQuadPerPlane * c->plane() ) );
72  return _uuid;
73 }
74 
75 
76 uint16_t StFttDb::packKey( int feb, int vmm, int ch ) const{
77  // feb = [1 - 6] = 3 bits
78  // vmm = [1 - 4] = 3 bits
79  // ch = [1 - 64] = 7 bits
80  return feb + (vmm << 3) + (ch << 6);
81 }
82 void StFttDb::unpackKey( int key, int &feb, int &vmm, int &ch ) const{
83  feb = key & 0b111;
84  vmm = (key >> 3) & 0b111;
85  ch = (key >> 6) & 0b1111111;
86  return;
87 }
88 uint16_t StFttDb::packVal( int row, int strip ) const{
89  // row = [1 - 4] = 3 bits
90  // strip = [1 - 152] = 8 bits
91  return row + ( strip << 3 );
92 }
93 void StFttDb::unpackVal( int val, int &row, int &strip ) const{
94  row = val & 0b111; // 3 bits
95  strip = (val >> 3) & 0b11111111; // 8 bit
96  return;
97 }
98 
99 void StFttDb::loadDataWindowsFromDb( St_fttDataWindows * dataset ) {
100  if (dataset) {
101  Int_t rows = dataset->GetNRows();
102 
103  if ( !rows ) return;
104 
105  dwMap.clear();
106 
107  fttDataWindows_st *table = dataset->GetTable();
108  for (Int_t i = 0; i < rows; i++) {
109  for ( int j = 0; j < StFttDb::nVMM; j++ ) {
110  // printf( "[feb=%d, vmm=%d, ch=%d] ==> [row=%d, strip%d]\n", table[i].feb[j], table[i].vmm[j], table[i].vmm_ch[j], table[i].row[j], table[i].strip[j] );
111 
112 
113  // uint16_t key = packKey( table[i].feb[j], table[i].vmm[j], table[i].vmm_ch[j] );
114  // uint16_t val = packVal( table[i].row[j], table[i].strip[j] );
115  // mMap[ key ] = val;
116  // rMap[ val ] = key;
117  FttDataWindow fdw;
118  fdw.uuid = table[i].uuid[j];
119  fdw.mode = table[i].mode[j];
120  fdw.min = table[i].min[j];
121  fdw.max = table[i].max[j];
122  fdw.anchor = table[i].anchor[j];
123  dwMap[ fdw.uuid ] = fdw;
124 
125 
126  // std::cout << (int)table[i].feb[j] << std::endl;
127  }
128  // sample output of first member variable
129  }
130  } else {
131  std::cout << "ERROR: dataset does not contain requested table" << std::endl;
132  }
133 }
134 
135 void StFttDb::loadDataWindowsFromFile( std::string fn ) {
136 
137 
138 }
139 
140 
141 void StFttDb::loadHardwareMapFromDb( St_fttHardwareMap * dataset ) {
142  if (dataset) {
143  Int_t rows = dataset->GetNRows();
144  if (rows > 1) {
145  std::cout << "INFO: found INDEXED table with " << rows << " rows" << std::endl;
146  }
147 
148  mMap.clear();
149  rMap.clear();
150 
151  fttHardwareMap_st *table = dataset->GetTable();
152  for (Int_t i = 0; i < rows; i++) {
153  for ( int j = 0; j < 1250; j++ ) {
154  uint16_t key = packKey( table[i].feb[j], table[i].vmm[j], table[i].vmm_ch[j] );
155  uint16_t val = packVal( table[i].row[j], table[i].strip[j] );
156  mMap[ key ] = val;
157  rMap[ val ] = key;
158  }
159  // sample output of first member variable
160  }
161  } else {
162  std::cout << "ERROR: dataset does not contain requested table" << std::endl;
163  }
164 }
165 
166 
167 void StFttDb::loadHardwareMapFromFile( std::string fn ){
168  std::ifstream inf;
169  inf.open( fn.c_str() );
170 
171  mMap.clear();
172  if ( !inf ) {
173  LOG_WARN << "sTGC Hardware map file not found" << endm;
174  return;
175  }
176 
177  mMap.clear();
178  string hs0, hs1, hs2, hs3, hs4;
179  // HEADER:
180  // Row_num FEB_num VMM_num VMM_ch strip_ch
181  inf >> hs0 >> hs1 >> hs2 >> hs3 >> hs4;
182 
183  if ( mDebug ){
184  printf( "Map Header: %s, %s, %s, %s, %s", hs0.c_str(), hs1.c_str(), hs2.c_str(), hs3.c_str(), hs4.c_str() );
185  puts("");
186  }
187 
188  uint16_t row, feb, vmm, ch, strip;
189  while( inf >> row >> feb >> vmm >> ch >> strip ){
190  // pack the key (feb, vmm, ch)
191  uint16_t key = packKey( feb, vmm, ch );
192  uint16_t val = packVal( row, strip );
193  mMap[ key ] = val;
194  rMap[ val ] = key;
195  if ( mDebug ){
196  printf( "key=%d", key );
197  printf( "in=(feb=%d, vmm=%d, ch=%d)\n", feb, vmm, ch );
198  int ufeb, uvmm, uch;
199  unpackKey( key, ufeb, uvmm, uch );
200  printf( "key=(feb=%d, vmm=%d, ch=%d)\n", ufeb, uvmm, uch ); puts("");
201  assert( feb == ufeb && vmm == uvmm && ch == uch );
202  int urow, ustrip;
203  printf( "val=%d", val );
204  unpackVal( val, urow, ustrip );
205  assert( row == urow && strip == ustrip );
206  printf( "(row=%d, strip=%d)\n", row, strip );
207  }
208  }
209  inf.close();
210 }
211 
212 // same for all planes
213 // we have quadrants like:
214 //
215 // D | A
216 // ------
217 // C | B
218 // Row 3 and 4 are always diagonal
219 // odd (even) FOB are horizontal (vertical) for A and C (B and D)
220 // even (odd) FOB are vertical (horizontal) for A and C (B and D)
221 UChar_t StFttDb::getOrientation( int rob, int feb, int vmm, int row ) const {
222  if ( mDebug ) {
223  printf( "getOrientation( %d, %d, %d, %d )", rob, feb, vmm, row ); puts("");
224  }
225 
226  if ( rob % 2 == 0 ){ // even rob
227  if ( feb % 2 != 0 ) { // odd
228  // row 3 and 4 are always diagonal
229  if ( 3 == row || 4 == row )
230  return kFttDiagonalH;
231  return kFttHorizontal;
232  }
233  // row 3 and 4 are always diagonal
234  if ( 3 == row || 4 == row )
235  return kFttDiagonalV;
236  // even
237  return kFttVertical;
238  } else { // odd rob
239 
240  if ( feb % 2 != 0 ) { // odd
241  // row 3 and 4 are always diagonal
242  if ( 3 == row || 4 == row )
243  return kFttDiagonalV;
244  return kFttVertical;
245  }
246  // row 3 and 4 are always diagonal
247  if ( 3 == row || 4 == row )
248  return kFttDiagonalH;
249  // even
250  return kFttHorizontal;
251  }
252  // should never get here!
253  return kFttUnknownOrientation;
254 }
255 
256 /* get
257  * returns the mapping for a given input
258  *
259  * input:
260  * rob: 1 - 16
261  * feb: 1 - 6
262  * vmm: 1 - 4
263  * ch : 0 - 63
264  *
265  * output:
266  * row: 0 - 4
267  * strip: 0 - 162
268  * orientation: one of {Horizontal, Vertical, Diagonal, Unknown}
269  *
270  */
271 bool StFttDb::hardwareMap( int rob, int feb, int vmm, int ch, int &row, int &strip, UChar_t &orientation ) const{
272  uint16_t key = packKey( feb, vmm, ch );
273  if ( mMap.count( key ) ){
274  uint16_t val = mMap.at( key );
275  unpackVal( val, row, strip );
276  orientation = getOrientation( rob, feb, vmm, row );
277  return true;
278  }
279  return false;
280 }
281 
282 bool StFttDb::hardwareMap( StFttRawHit * hit ) const{
283  uint16_t key = packKey( hit->feb()+1, hit->vmm()+1, hit->channel() );
284  if ( mMap.count( key ) ){
285  uint16_t val = mMap.at( key );
286  int row=-1, strip=-1;
287  unpackVal( val, row, strip );
288 
289  u_char iPlane = hit->sector() - 1;
290  u_char iQuad = hit->rdo() - 1;
291  int rob = iQuad + ( iPlane *nQuadPerPlane ) + 1;
292 
293  UChar_t orientation = getOrientation( rob, hit->feb()+1, hit->vmm()+1, row );
294  hit->setMapping( iPlane, iQuad, row, strip, orientation );
295  return true;
296  }
297  return false;
298 }
299 
300 
301 UChar_t StFttDb::plane( StFttRawHit * hit ){
302  if ( hit->plane() < nPlane )
303  return hit->plane();
304  return hit->sector() - 1;
305 }
306 
307 UChar_t StFttDb::quadrant( StFttRawHit * hit ){
308  if ( hit->quadrant() < nQuad )
309  return hit->quadrant();
310  return hit->rdo() - 1;
311 }
312 
313 UChar_t StFttDb::rob( StFttRawHit * hit ){
314  return quadrant(hit) + ( plane(hit) * nQuadPerPlane ) + 1;
315 }
316 
317 UChar_t StFttDb::rob( StFttCluster * clu ){
318  return clu->quadrant() + ( clu->plane() * StFttDb::nQuadPerPlane );
319 }
320 
321 UChar_t StFttDb::fob( StFttRawHit * hit ){
322  return hit->feb() + ( quadrant( hit ) * nFobPerQuad ) + ( plane(hit) * nFobPerPlane ) + 1;
323 }
324 
325 UChar_t StFttDb::orientation( StFttRawHit * hit ){
326  if ( hit->orientation() < kFttUnknownOrientation ){
327  return hit->orientation();
328  }
329  return kFttUnknownOrientation;
330 }
331 
332 void StFttDb::getGloablOffset( UChar_t plane, UChar_t quad,
333  float &dx, float &sx,
334  float &dy, float &sy,
335  float &dz, float &sz ){
336  // TODO: connect to DB for calibrated positions.
337  // for now we use the ideal positions (from simulated geometry)
338  // calibration will come later
339 
340  // scale factors
341  sx = 1.0;
342  sy = 1.0;
343  sz = 1.0;
344 
345  // shifts
346  dx = 0.0;
347  dy = 0.0;
348  dz = 0.0;
349 
350  if ( plane < 4 )
351  dz = StFttDb::idealPlaneZLocations[plane];
352 
353  // upper quadrants are not displaced
354  if ( quad == 0 )
355  dx = 0.0;
356  else if ( quad == 1 )
357  dx = StFttDb::lowerQuadOffsetX;
358  else if ( quad == 2 )
359  dx = StFttDb::lowerQuadOffsetX;
360  else if ( quad == 3 )
361  dx = 0.0;
362 
363  // these are the reflections of a pentagon into the symmetric shape for quadrants A, B, C, D
364  if ( quad == 1 )
365  sy = -1.0;
366  else if ( quad == 2 ){
367  sx = -1.0;
368  sy = -1.0;
369  } else if ( quad == 3 )
370  sx = -1.0;
371 
372 }
void setRun(int run)
enable(1) or disable(0) offline DB access
Definition: StFttDb.cxx:41
void setDbAccess(int v=1)
debug level
Definition: StFttDb.cxx:40
static size_t uuid(StFttRawHit *h, bool includeStrip=false)
set run#
Definition: StFttDb.cxx:49
Definition: Stypes.h:40