CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
HcalTBTDCUnpacker.cc
Go to the documentation of this file.
3 #include <math.h>
4 
5 // Timing channels
6 static const int lcTriggerTime = 1;
7 static const int lcTTCL1ATime = 2;
8 static const int lcBeamCoincidence = 3;
9 static const int lcLaserFlash = 4;
10 static const int lcQIEPhase = 5;
11 
12 static const int lcMuon1 = 90;
13 static const int lcMuon2 = 91;
14 static const int lcMuon3 = 92;
15 static const int lcScint1 = 93;
16 static const int lcScint2 = 94;
17 static const int lcScint3 = 95;
18 static const int lcScint4 = 96;
19 static const int lcBeamHalo1 = 50;
20 static const int lcBeamHalo2 = 51;
21 static const int lcBeamHalo3 = 55;
22 static const int lcBeamHalo4 = 63;
23 static const int lcTOF1S = 129;
24 static const int lcTOF1J = 130;
25 static const int lcTOF2S = 131;
26 static const int lcTOF2J = 132;
27 
28 namespace hcaltb {
29 
30  static FILE* dumpObs=0;
31 
32 HcalTBTDCUnpacker::HcalTBTDCUnpacker(bool include_unmatched_hits) :
33  includeUnmatchedHits_(include_unmatched_hits) {
34 // setupWC(); reads it from configuration file
35 // dumpObs=fopen("dump_obs.csv","w");
36 }
37 void HcalTBTDCUnpacker::setCalib(const std::vector<std::vector<std::string> >& calibLines_) {
38  for(int i=0;i<161;i++)
39  {
40  tdc_ped[i]=0.;tdc_convers[i]=1.;
41  }
42  for(unsigned int ii=0;ii<calibLines_.size();ii++)
43  {
44 // TDC configuration
45  if(calibLines_[ii][0]=="TDC")
46  {
47  if(calibLines_[ii].size()==4)
48  {
49  int channel=atoi(calibLines_[ii][1].c_str());
50  tdc_ped[channel]=atof(calibLines_[ii][2].c_str());
51  tdc_convers[channel]=atof(calibLines_[ii][3].c_str());
52  // printf("Got TDC %i ped %f , conversion %f\n",channel, tdc_ped[channel],tdc_convers[channel]);
53  }
54  else
55  {
56  throw cms::Exception("Incomplete configuration") <<
57  "Wrong TDC configuration format : expected 3 parameters, got "<<calibLines_[ii].size()-1;
58  }
59  } // End of the TDCs
60 
61 // Wire chambers calibration
62  if(calibLines_[ii][0]=="WC")
63  {
64  if(calibLines_[ii].size()==6)
65  {
66  int plane=atoi(calibLines_[ii][1].c_str());
67  wc_[plane].b0=atof(calibLines_[ii][2].c_str());
68  wc_[plane].b1=atof(calibLines_[ii][3].c_str());
69  wc_[plane].mean=atof(calibLines_[ii][4].c_str());
70  wc_[plane].sigma=atof(calibLines_[ii][5].c_str());
71  // printf("Got WC plane %i b0 %f, b1 %f, mean %f, sigma %f\n",plane,
72  // wc_[plane].b0,wc_[plane].b1,wc_[plane].mean,wc_[plane].sigma);
73  }
74  else
75  {
76  throw cms::Exception("Incomplete configuration") <<
77  "Wrong Wire Chamber configuration format : expected 5 parameters, got "<<calibLines_[ii].size()-1;
78  }
79  } // End of the Wire Chambers
80 
81  } // End of the CalibLines
82  }
83 
86  HcalTBTiming& timing) const {
87  std::vector<Hit> hits;
88 
89  unpackHits(raw, hits, timing);
90 
91  reconstructWC(hits, pos);
92  reconstructTiming(hits, timing);
93 
94  }
95 
98  unsigned int n_max_hits; // maximum number of hits possible in the block
99  unsigned int n_hits;
100  unsigned int hits[2];
101  };
102 
103  struct CombinedTDCQDCDataFormat {
105  unsigned int n_qdc_hits; // Count of QDC channels
106  unsigned int n_tdc_hits; // upper/lower TDC counts
107  unsigned short qdc_values[4];
108  };
109 
110 //static const double CONVERSION_FACTOR=25.0/32.0;
111 
113  std::vector<Hit>& hits,HcalTBTiming& timing) const {
114  const ClassicTDCDataFormat* tdc=(const ClassicTDCDataFormat*)raw.data();
115 
116  if (raw.size()<3*8) {
117  throw cms::Exception("Missing Data") << "No data in the TDC block";
118  }
119 
120  const unsigned int* hitbase=0;
121  unsigned int totalhits=0;
122 
123  // old TDC (767)
124  if (tdc->n_max_hits!=192) {
125  const CombinedTDCQDCDataFormat* qdctdc=(const CombinedTDCQDCDataFormat*)raw.data();
126  hitbase=(unsigned int*)(qdctdc);
127  hitbase+=6; // header
128  hitbase+=qdctdc->n_qdc_hits/2; // two unsigned short per unsigned long
129  totalhits=qdctdc->n_tdc_hits&0xFFFF; // mask off high bits
130 
131  // for (unsigned int i=0; i<qdctdc->n_qdc_hits; i++)
132  // printf("QADC: %02d %d\n",i,qdctdc->qdc_values[i]&0xFFF);
133 
134  } else {
135  hitbase=&(tdc->hits[0]);
136  totalhits=tdc->n_hits;
137  }
138 
139  for (unsigned int i=0; i<totalhits; i++) {
140  Hit h;
141  h.channel=(hitbase[i]&0x7FC00000)>>22; // hardcode channel assignment
142  h.time=(hitbase[i]&0xFFFFF)*tdc_convers[h.channel];
143  hits.push_back(h);
144  // printf("V767: %d %f\n",h.channel,h.time);
145  }
146 
147  // new TDC (V775)
148  int v775[32];
149  for (int i=0;i<32;i++) v775[i]=-1;
150  if (tdc->n_max_hits!=192) {
151  const CombinedTDCQDCDataFormat* qdctdc=(const CombinedTDCQDCDataFormat*)raw.data();
152  hitbase=(unsigned int*)(qdctdc);
153  hitbase+=6; // header
154  hitbase+=qdctdc->n_qdc_hits/2; // two unsigned short per unsigned long
155  hitbase+=(qdctdc->n_tdc_hits&0xFFFF); // same length
156  totalhits=(qdctdc->n_tdc_hits&0xFFFF0000)>>16; // mask off high bits
157  for (unsigned int i=0; i<totalhits; i++) {
158  Hit h;
159 // h.channel=129+i;
160  h.channel=129+((hitbase[i]&0x3F0000)>>16);
161  h.time=(hitbase[i]&0xFFF)*tdc_convers[h.channel] ;
162  hits.push_back(h);
163  if ( (h.channel-129)<32 )
164  v775[(h.channel-129)] = (hitbase[i]&0xFFF);
165  // printf("V775: %d %f\n",h.channel,h.time);
166  }
167  }
168  timing.setV775(v775);
169 }
170 
171 void HcalTBTDCUnpacker::reconstructTiming(const std::vector<Hit>& hits,
172  HcalTBTiming& timing) const {
173  std::vector<Hit>::const_iterator j;
174  double trigger_time=0;
175  double ttc_l1a_time=0;
176  double laser_flash=0;
177  double qie_phase=0;
178  double TOF1S_time=0;
179  double TOF1J_time=0;
180  double TOF2S_time=0;
181  double TOF2J_time=0;
182 
183  std::vector<double> m1hits, m2hits, m3hits, s1hits, s2hits, s3hits, s4hits,
184  bh1hits, bh2hits, bh3hits, bh4hits,beam_coinc;
185 
186  for (j=hits.begin(); j!=hits.end(); j++) {
187  switch (j->channel) {
188  case lcTriggerTime: trigger_time = j->time-tdc_ped[lcTriggerTime]; break;
189  case lcTTCL1ATime: ttc_l1a_time = j->time-tdc_ped[lcTTCL1ATime]; break;
190  case lcBeamCoincidence: beam_coinc.push_back(j->time-tdc_ped[lcBeamCoincidence]); break;
191  case lcLaserFlash: laser_flash = j->time-tdc_ped[lcLaserFlash]; break;
192  case lcQIEPhase: qie_phase = j->time-tdc_ped[lcQIEPhase]; break;
193  case lcMuon1: m1hits.push_back(j->time-tdc_ped[lcMuon1]); break;
194  case lcMuon2: m2hits.push_back(j->time-tdc_ped[lcMuon2]); break;
195  case lcMuon3: m3hits.push_back(j->time-tdc_ped[lcMuon3]); break;
196  case lcScint1: s1hits.push_back(j->time-tdc_ped[lcScint1]); break;
197  case lcScint2: s2hits.push_back(j->time-tdc_ped[lcScint2]); break;
198  case lcScint3: s3hits.push_back(j->time-tdc_ped[lcScint3]); break;
199  case lcScint4: s4hits.push_back(j->time-tdc_ped[lcScint4]); break;
200  case lcTOF1S: TOF1S_time = j->time-tdc_ped[lcTOF1S]; break;
201  case lcTOF1J: TOF1J_time = j->time-tdc_ped[lcTOF1J]; break;
202  case lcTOF2S: TOF2S_time = j->time-tdc_ped[lcTOF2S]; break;
203  case lcTOF2J: TOF2J_time = j->time-tdc_ped[lcTOF2J]; break;
204  case lcBeamHalo1: bh1hits.push_back(j->time-tdc_ped[lcBeamHalo1]); break;
205  case lcBeamHalo2: bh2hits.push_back(j->time-tdc_ped[lcBeamHalo2]); break;
206  case lcBeamHalo3: bh3hits.push_back(j->time-tdc_ped[lcBeamHalo3]); break;
207  case lcBeamHalo4: bh4hits.push_back(j->time-tdc_ped[lcBeamHalo4]); break;
208  default: break;
209  }
210  }
211 
212  timing.setTimes(trigger_time,ttc_l1a_time,laser_flash,qie_phase,TOF1S_time,TOF1J_time,TOF2S_time,TOF2J_time);
213  timing.setHits (m1hits,m2hits,m3hits,s1hits,s2hits,s3hits,s4hits,bh1hits,bh2hits,bh3hits,bh4hits,beam_coinc);
214 
215 }
216 
217 const int HcalTBTDCUnpacker::WC_CHANNELIDS[PLANECOUNT*3] = {
218  12, 13, 14, // WCA LR plane
219  10, 11, 14, // WCA UD plane
220  22, 23, 24, // WCB LR plane
221  20, 21, 24, // WCB UD plane
222  32, 33, 34, // WCC LR plane
223  30, 31, 34, // WCC UD plane
224  101, 102, 104, // WCD LR plane
225  107, 108, 110, // WCD UD plane
226  113, 114, 116, // WCE LR plane
227  97, 98, 99, // WCE UD plane
228  42, 43, -1, // WCF LR plane (was WC1)
229  44, 60, -1, // WCF UD plane (was WC1)
230  40, 41, -1, // WCG LR plane (was WC2)
231  45, 61, -1, // WCG UD plane (was WC2)
232  52, 53, -1, // WCH LR plane (was WC3)
233  54, 62, -1 // WCH UD plane (was WC3)
234 };
235 
236 static const double TDC_OFFSET_CONSTANT = 12000;
237 static const double N_SIGMA = 2.5;
238 
239 /* Obsolated - reads it from the configuration file
240 void HcalTBTDCUnpacker::setupWC() {
241 
242  wc_[0].b0 = -0.0870056; wc_[0].b1 = -0.193263; // WCA planes
243  wc_[1].b0 = -0.0288171; wc_[1].b1 = -0.191231;
244 
245  wc_[2].b0 = -0.2214840; wc_[2].b1 = -0.191683; // WCB planes
246  wc_[3].b0 = -1.0847300; wc_[3].b1 = -0.187691;
247 
248  wc_[4].b0 = -0.1981440; wc_[4].b1 = -0.192760; // WCC planes
249  wc_[5].b0 = 0.4230690; wc_[5].b1 = -0.192278;
250 
251  wc_[6].b0 = -0.6039130; wc_[6].b1 = -0.185674; // WCD planes
252  wc_[7].b0 = -0.4366590; wc_[7].b1 = -0.184992;
253 
254  wc_[8].b0 = 1.7016400; wc_[8].b1 = -0.185575; // WCE planes
255  wc_[9].b0 = -0.2324480; wc_[9].b1 = -0.185367;
256 
257  wc_[0].mean=225.2; wc_[0].sigma=5.704;
258  wc_[1].mean=223.5; wc_[1].sigma=5.862;
259  wc_[2].mean=227.2; wc_[2].sigma=5.070;
260  wc_[3].mean=235.7; wc_[3].sigma=6.090;
261  wc_[4].mean=243.3; wc_[4].sigma=7.804;
262  wc_[5].mean=230.3; wc_[5].sigma=28.91;
263 
264  wc_[6].mean=225.0; wc_[6].sigma=6.000;
265  wc_[7].mean=225.0; wc_[7].sigma=6.000;
266  wc_[8].mean=225.0; wc_[8].sigma=6.000;
267  wc_[9].mean=225.0; wc_[9].sigma=6.000;
268 }
269 */
270 
271 void HcalTBTDCUnpacker::reconstructWC(const std::vector<Hit>& hits, HcalTBEventPosition& ep) const {
272  // process all planes, looping over all hits...
273  const int MAX_HITS=100;
274  float hits1[MAX_HITS], hits2[MAX_HITS], hitsA[MAX_HITS];
275  int n1,n2,nA,chan1,chan2,chanA;
276 
277  std::vector<double> x;
278 
279  for (int plane=0; plane<PLANECOUNT; plane++) {
280  n1=0; n2=0; nA=0;
281 
282  std::vector<double> plane_hits;
283  double hit_time;
284  hits1[0]=-1000;
285  hits2[0]=-1000;
286  hitsA[0]=-1000;
287  hitsA[1]=-1000;
288 
289  chan1=WC_CHANNELIDS[plane*3];
290  chan2=WC_CHANNELIDS[plane*3+1];
291  chanA=WC_CHANNELIDS[plane*3+2];
292 
293  for (std::vector<Hit>::const_iterator j=hits.begin(); j!=hits.end(); j++) {
294  if (j->channel==chan1 && n1<MAX_HITS) {
295  hits1[n1]=j->time-TDC_OFFSET_CONSTANT; n1++;
296  }
297  if (j->channel==chan2 && n2<MAX_HITS) {
298  hits2[n2]=j->time-TDC_OFFSET_CONSTANT; n2++;
299  }
300  if (j->channel==chanA && nA<MAX_HITS) {
301  hitsA[nA]=j->time-TDC_OFFSET_CONSTANT; nA++;
302  }
303  }
304 
305  if (n1!=0 && n2!=0 && dumpObs!=0) {
306  fprintf(dumpObs,"%d,%f,%f,%f,%f\n",plane,hits1[0],hits2[0],hitsA[0],hitsA[1]);
307  fflush(dumpObs);
308  }
309 
310  // anode-matched hits
311  for (int ii=0; ii<n1; ii++) {
312  int jmin=-1, lmin=-1;
313  float dsumMin=99999;
314  for (int jj=0; jj<n2; jj++) {
315  for (int ll=0; ll<nA; ll++) {
316  float dsum=fabs(wc_[plane].mean - hits1[ii] - hits2[jj] + 2.0*hitsA[ll]);
317  if(dsum<(N_SIGMA*wc_[plane].sigma) && dsum<dsumMin){
318  jmin=jj;
319  lmin=ll;
320  dsumMin=dsum;
321  }
322  }
323  }
324  if (jmin>=0) {
325  hit_time = wc_[plane].b0 +
326  wc_[plane].b1 * (hits1[ii]-hits2[jmin]);
327  if((plane%2)==0)
328  {
329  plane_hits.push_back(-hit_time);
330  }else{
331  plane_hits.push_back(hit_time);
332  }
333  hits1[ii]=-99999;
334  hits2[jmin]=-99999;
335  hitsA[lmin]=99999;
336  }
337  }
338 
339  if (includeUnmatchedHits_||plane>9) // unmatched hits (all pairs get in here)
340  for (int ii=0; ii<n1; ii++) {
341  if (hits1[ii]<-99990) continue;
342  for (int jj=0; jj<n2; jj++) {
343  if (hits2[jj]<-99990) continue;
344  hit_time = wc_[plane].b0 +
345  wc_[plane].b1 * (hits1[ii]-hits2[jj]);
346  if((plane%2)==0)
347  {
348  plane_hits.push_back(-hit_time);
349  }else{
350  plane_hits.push_back(hit_time);
351  }
352  }
353  }
354 
355  if ((plane%2)==0) x=plane_hits;
356  else {
357  char chamber='A'+plane/2;
358  ep.setChamberHits(chamber,x,plane_hits);
359  }
360  }
361 
362 }
363 
364 }
static const int lcBeamCoincidence
int i
Definition: DBlmapReader.cc:9
void setChamberHits(char chamberch, const std::vector< double > &xvec, const std::vector< double > &yvec)
static const int lcTOF2S
void reconstructTiming(const std::vector< Hit > &hits, HcalTBTiming &timing) const
static const int lcBeamHalo4
static const int lcTOF1S
void unpackHits(const FEDRawData &raw, std::vector< Hit > &hits, HcalTBTiming &timing) const
static const int lcTTCL1ATime
static const int lcScint4
static const int lcScint3
size_t size() const
Lenght of the data buffer in bytes.
Definition: FEDRawData.h:49
static const int lcBeamHalo2
static const int lcLaserFlash
static const int lcScint1
void setV775(int *V775)
Definition: HcalTBTiming.cc:75
int j
Definition: DBlmapReader.cc:9
static const int lcMuon3
void setCalib(const std::vector< std::vector< std::string > > &calibLines_)
static const int lcBeamHalo1
static const int lcTriggerTime
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
static const int lcTOF1J
static FILE * dumpObs
static const int lcQIEPhase
void unpack(const FEDRawData &raw, HcalTBEventPosition &pos, HcalTBTiming &timing) const
void reconstructWC(const std::vector< Hit > &hits, HcalTBEventPosition &pos) const
static const double N_SIGMA
static const int lcBeamHalo3
void setTimes(const double trigger_time, const double ttc_l1a_time, const double laser_flash, const double qie_phase, const double TOF1S_time, const double TOF1J_time, const double TOF2S_time, const double TOF2J_time)
Definition: HcalTBTiming.cc:29
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:29
static const int lcMuon1
static const double TDC_OFFSET_CONSTANT
void setHits(const std::vector< double > &m1hits, const std::vector< double > &m2hits, const std::vector< double > &m3hits, const std::vector< double > &s1hits, const std::vector< double > &s2hits, const std::vector< double > &s3hits, const std::vector< double > &s4hits, const std::vector< double > &bh1hits, const std::vector< double > &bh2hits, const std::vector< double > &bh3hits, const std::vector< double > &bh4hits, const std::vector< double > &beamCoincidenceHits)
Definition: HcalTBTiming.cc:47
static const int lcMuon2
Definition: DDAxes.h:10
static const int lcTOF2J
static const int WC_CHANNELIDS[PLANECOUNT *3]
tuple size
Write out results.
struct hcaltb::HcalTBTDCUnpacker::WireChamberRecoData wc_[PLANECOUNT]
static const int lcScint2