QwAnalysis
QwDriftChamberHDC.cc
Go to the documentation of this file.
1 /*File: QwDriftChamberHDC.C *
2 * *
3 * Author: P. M. King , Rakitha Beminiwattha *
4 * Time-stamp: <2008-07-08 15:40> *
5 **********************************************************/
6 
7 #include "QwDriftChamberHDC.h"
8 
9 #include "QwParameterFile.h"
10 #include "QwLog.h"
11 #include <boost/bind.hpp>
12 
13 // Register this subsystem with the factory
15 
16 
18 {
19  UInt_t bank_index = 0;
20  Double_t raw_time_arb_unit = 0.0;
21  Double_t ref_time_arb_unit = 0.0;
22  Double_t time_arb_unit = 0.0;
23  // EQwDetectorPackage package = kPackageNull;
24 
25  Bool_t local_debug = false;
26  Int_t slot_num = 0;
27 
28  TString reference_name1 = "MasterTrigger";
29  TString reference_name2 = "CopyMasterTrigger";
30 
31  std::vector<QwHit>::iterator end=fTDCHits.end();
32  for ( std::vector<QwHit>::iterator hit=fTDCHits.begin(); hit!=end; hit++ )
33  {
34 
35  bank_index = hit -> GetSubbankID();
36  slot_num = hit -> GetModule();
37  raw_time_arb_unit = (Double_t) hit -> GetRawTime();
38 
39 
40  // John L. introduced this package match reference time subtraction for only Region 3,
41  // I was told it would be better to extend this concept to Region2, so I did, and I
42  // got the worst result..... Q2 = +2.735199e-02 its RMS +1.192504e-02
43  // if I don't use this method in Region2, Q2 = +2.292274e-02 and RMS = +7.133603e-03
44  // if I don't use this method in Region3, I couldn't get "peak" structure
45  // in time distributions of four VDC chambers.
46  //
47  // Why does this method give us a good result for only Region 3? MUX?
48  // I don't know ..
49  // Friday, February 17 14:01:12 EST 2012, jhlee
50 
51  // package = hit -> GetPackage();
52  // if (package == kPackageUp) { // Up is 1
53  // reference_name1 = "TrigScintPkg1";
54  // }
55  // else if (package == kPackageDown) {// Down is 2
56  // reference_name1 = "TrigScintPkg2";
57  // }
58  // else {
59  // reference_name1 = "MasterTrigger";
60  // }
61 
62  ref_time_arb_unit = fF1RefContainer -> GetReferenceTimeAU(bank_index, reference_name1);
63  //
64  // if there is no reference time due to a channel error, try to use a copy of mater trigger
65  //
66  if(ref_time_arb_unit==0.0) {
67  ref_time_arb_unit = fF1RefContainer->GetReferenceTimeAU(bank_index, reference_name2);
68  }
69  // second time, it returns 0.0, we simply ignore this event ....
70  // set time zero. ReferenceSignalCorrection() will return zero, and increase RFM counter...
71  //
72  time_arb_unit = fF1TDContainer->ReferenceSignalCorrection(raw_time_arb_unit, ref_time_arb_unit, bank_index, slot_num);
73 
74  hit -> SetTime(time_arb_unit);
75  hit -> SetRawRefTime((UInt_t) ref_time_arb_unit);
76 
77  if(local_debug) {
78  QwMessage << this->GetSubsystemName()
79  << " BankIndex " << std::setw(2) << bank_index
80  << " Slot " << std::setw(2) << slot_num
81  << " RawTime : " << std::setw(6) << raw_time_arb_unit
82  << " RefTime : " << std::setw(6) << ref_time_arb_unit
83  << " time : " << std::setw(6) << time_arb_unit
84  << std::endl;
85 
86  }
87  }
88 
89 
90 
91  return;
92 }
93 
94 
95 
96 
97 // void QwDriftChamberHDC::SubtractReferenceTimes()
98 // {
99 // std::vector<Double_t> reftimes;
100 // std::vector<Bool_t> refchecked;
101 // std::vector<Bool_t> refokay;
102 // Bool_t allrefsokay;
103 // // Int_t counter = 1;
104 
105 // std::size_t ref_size = 0;
106 // std::size_t i = 0;
107 // std::size_t j = 0;
108 
109 // ref_size = fReferenceData.size();
110 
111 
112 // reftimes.resize ( ref_size );
113 // refchecked.resize( ref_size );
114 // refokay.resize ( ref_size );
115 
116 // for ( i=0; i<ref_size; i++ ) {
117 // reftimes.at(i) = 0.0;
118 // refchecked.at(i) = kFALSE;
119 // refokay.at(i) = kFALSE;
120 // }
121 
122 // allrefsokay = kTRUE;
123 
124 // UInt_t bankid = 0;
125 // Double_t raw_time_arb_unit = 0.0;
126 // Double_t ref_time_arb_unit = 0.0;
127 // Double_t time_arb_unit = 0.0;
128 
129 // Bool_t local_debug = true;
130 
131 // for ( std::vector<QwHit>::iterator hit=fTDCHits.begin(); hit!=fTDCHits.end(); hit++ )
132 // {
133 // // Only try to check the reference time for a bank if there is at least one
134 // // non-reference hit in the bank.
135 // bankid = hit->GetSubbankID();
136 // if ( not refchecked.at(bankid) ) {
137 // if ( fReferenceData.at(bankid).empty() ) {
138 // QwWarning << "QwDriftChamberHDC::SubtractReferenceTimes: Subbank ID "
139 // << bankid << " is missing a reference time." << QwLog::endl;
140 // refokay.at(bankid) = kFALSE;
141 // allrefsokay = kFALSE;
142 // }
143 // else {
144 // if(fReferenceData.at(bankid).size() not_eq 1) {
145 // std::cout << "Multiple hits are recorded in the reference channel, we use the first hit signal as the refererence signal." << std::endl;
146 // }
147 // reftimes.at(bankid) = fReferenceData.at(bankid).at(0);
148 // refokay.at(bankid) = kTRUE;
149 // }
150 // if ( refokay.at(bankid) ){
151 // for ( j=0; j<fReferenceData.at(bankid).size(); j++ )
152 // {
153 // fReferenceData.at(bankid).at(j) -= reftimes.at(bankid);
154 // }
155 // }
156 // refchecked.at(bankid) = kTRUE;
157 // }
158 
159 // if ( refokay.at(bankid) ) {
160 // Int_t slot_num = hit -> GetModule();
161 // raw_time_arb_unit = (Double_t) hit -> GetRawTime();
162 // ref_time_arb_unit = (Double_t) reftimes.at(bankid);
163 // // raw_time = (Double_t) hit -> GetRawTime();
164 // // ref_time = (Double_t) reftimes.at(bankid);
165 // // Int_t bank_index = hit->GetSubbankID();
166 // // Int_t slot_num = hit->GetModule();
167 
168 // time_arb_unit = fF1TDContainer->ReferenceSignalCorrection(raw_time_arb_unit, ref_time_arb_unit, bankid, slot_num);
169 
170 // hit -> SetTime(time_arb_unit);
171 // hit -> SetRawRefTime((UInt_t) ref_time_arb_unit);
172 
173 // // hit -> SetTime(time+educated_guess_t0_correction); // an educated guess
174 
175 
176 // if(local_debug) {
177 // QwMessage << this->GetSubsystemName()
178 // << " BankIndex " << std::setw(2) << bankid
179 // << " Slot " << std::setw(2) << slot_num
180 // << " RawTime : " << std::setw(6) << raw_time_arb_unit
181 // << " RefTime : " << std::setw(6) << ref_time_arb_unit
182 // << " time : " << std::setw(6) << time_arb_unit
183 // << std::endl;
184 
185 // }
186 // }
187 // }
188 
189 // bankid = 0;
190 
191 // if ( not allrefsokay ) {
192 // std::vector<QwHit> tmp_hits;
193 // tmp_hits.clear();
194 // for ( std::vector<QwHit>::iterator hit=fTDCHits.begin(); hit!=fTDCHits.end(); hit++ )
195 // {
196 // bankid = hit->GetSubbankID();
197 // if ( refokay.at(bankid) ) tmp_hits.push_back(*hit);
198 // }
199 // // std::cout << "FTDC size " << fTDCHits.size() << "tmp hit size " << tmp_hits.size() << std::endl;
200 // fTDCHits.clear();
201 // fTDCHits = tmp_hits;
202 // // std::cout << "FTDC size " << fTDCHits.size() << "tmp hit size " << tmp_hits.size() << std::endl;
203 // }
204 
205 // return;
206 // }
207 
208 
209 Double_t QwDriftChamberHDC::CalculateDriftDistance(Double_t drifttime, QwDetectorID detector)
210 {
211  //0.00545449393 0.0668865488 0.000352462179 -2.00383196E-05 3.57577417E-07 -2.82802562E-09 7.89009965E-12
212  // Double_t dt_ = 0.12 * (drifttime-trig_h1 + 933.0);
213  //Double_t dt_ = 0.12 * (drifttime);
214 
215  Double_t dt_= drifttime;
216  //Double_t dd_ = 0.0,t0=0.0;
217  Double_t dd_ = 0.0;
218  Double_t resolution=1.0;
219  //dt_-=t0;
220  if(dt_>=0 && dt_<130){
221  Int_t index = (Int_t) (dt_/resolution);
222  dd_=( dt_-resolution*index ) /resolution * ( fTtoDNumbers.at ( index+1 )-fTtoDNumbers.at ( index ) ) +fTtoDNumbers.at ( index );}
223  else{
224  dd_=-50;
225  }
226  //dd_ = -0.0138896
227  // + 0.00987685 * dt_
228  // + 0.00100368 * dt_ * dt_
229  // + (-1.79785E-06 * dt_ * dt_ * dt_)
230  // + ( -8.96859E-08 * dt_ * dt_ * dt_ * dt_)
231  // + (6.11736E-10 * dt_ * dt_ * dt_ * dt_ * dt_)
232  // + ( -1.15889E-12 * dt_ * dt_ * dt_ * dt_ * dt_ * dt_);
233  /*
234  dd_ = 0.078067
235  + 0.0437269 * dt_
236  + 0.00117295 * dt_ * dt_
237  + (-2.25244E-05 * dt_ * dt_ * dt_ )
238  + (1.56369E-07 * dt_ * dt_ * dt_ * dt_ )
239  + (-4.93323E-10 * dt_ * dt_ * dt_ * dt_ * dt_ )
240  + (5.91555E-13 * dt_ * dt_ * dt_ * dt_ * dt_ * dt_ );
241  */
242  /* modified on 418
243  dd_ = 0.134955
244  + 0.0173593 * dt_
245  + 0.00254912 * dt_ * dt_
246  + (-4.26414E-05 * dt_ * dt_ * dt_ )
247  + (2.86094E-07 * dt_ * dt_ * dt_ * dt_ )
248  + (-8.85512E-10 * dt_ * dt_ * dt_ * dt_ * dt_ )
249  + (1.04669E-12 * dt_ * dt_ * dt_ * dt_ * dt_ * dt_ );
250  */
251  /*dd_ = -0.0382463
252  + 0.0704644 * dt_
253  - 0.00089724454912 * dt_ * dt_
254  + (4.8703E-05 * dt_ * dt_ * dt_ )
255  + (-8.40592E-07 * dt_ * dt_ * dt_ * dt_ )
256  + (5.61285E-09 * dt_ * dt_ * dt_ * dt_ * dt_ )
257  + (-1.31489E-11 * dt_ * dt_ * dt_ * dt_ * dt_ * dt_ );
258  */
259  //dd_ = 0.015993
260  // + 0.0466621 * dt_
261  // + 0.00180132 * dt_ * dt_
262  // + (-5.96573E-05 * dt_ * dt_ * dt_ )
263  // + (1.18497E-06 * dt_ * dt_ * dt_ * dt_ )
264  // + (-1.2071E-08 * dt_ * dt_ * dt_ * dt_ * dt_ )
265  // + (4.50584E-11 * dt_ * dt_ * dt_ * dt_ * dt_ * dt_ );
266  /* dd_ = -0.0181483
267  + 0.0867558 * dt_
268  -0.00115513 * dt_ * dt_
269  + (4.41846E-05 * dt_ * dt_ * dt_)
270  + (-6.9308E-07 * dt_ * dt_ * dt_ * dt_)
271  + (4.30973E-09 * dt_ * dt_ * dt_ * dt_ * dt_)
272  + (-9.32219E-12 * dt_ * dt_ * dt_ * dt_ * dt_ * dt_);
273  */
274  //dd_ = -0.119434
275  // + 0.0321642 * dt_
276  // + 0.0011334 * dt_ * dt_
277  // + (-1.08359E-05 * dt_ * dt_ * dt_)
278  // + (-2.78272E-08 * dt_ * dt_ * dt_ * dt_)
279  // + (5.97029E-10 * dt_ * dt_ * dt_ * dt_ * dt_)
280  // + (-1.67945E-12 * dt_ * dt_ * dt_ * dt_ * dt_ * dt_);
281 
282  //std::cout<<" Drift distance "<<dd_<<" Drift time "<<dt_<<" Original value "<<drifttime<<std::endl;
283 
284  // This formula returns a drift distance in [mm], but the tracking code
285  // expects a number in [cm]. Since the geometry definitions are in [cm],
286  // we should stick to [cm] as unit of distance. (wdconinc)
287  return dd_ / 10.0;
288 }
289 
290 
291 void QwDriftChamberHDC::FillRawTDCWord (Int_t bank_index, Int_t slot_num, Int_t chan, UInt_t data)
292 {
293  Bool_t local_debug = false;
294  Int_t tdcindex = 0;
295 
296  tdcindex = GetTDCIndex(bank_index,slot_num);
297 
298  if (tdcindex not_eq -1) {
299 
300  Int_t hitcnt = 0;
301  EQwDirectionID direction = kDirectionNull;
302 
303  fF1RefContainer->SetReferenceSignal(bank_index, slot_num, chan, data, local_debug);
304 
305 
306  Int_t plane = fTDCPtrs.at(tdcindex).at(chan).fPlane;
307  Int_t wire = fTDCPtrs.at(tdcindex).at(chan).fElement;
308  EQwDetectorPackage package = fTDCPtrs.at(tdcindex).at(chan).fPackage;
309  Int_t octant = fTDCPtrs.at(tdcindex).at(chan).fOctant;
310 
311 
312 
313  if (plane == -1 or wire == -1){
314  // This channel is not connected to anything. Do nothing.
315  }
316  else if (plane == kReferenceChannelPlaneNumber){
317  fReferenceData.at(wire).push_back(data);
318  //now wire contains the value fCurrentBankIndex so we can assign the ref timing data to it.
319  }
320  else {
321 
322  direction = (EQwDirectionID)fDirectionData.at(package-1).at(plane-1);
323  //Wire Direction is accessed from the vector -Rakitha (10/23/2008)
324  //hitCount gives the total number of hits on a given wire -Rakitha (10/23/2008)
325  hitcnt = std::count_if(fTDCHits.begin(), fTDCHits.end(),
326  boost::bind(
327  &QwHit::WireMatches, _1, kRegionID2, boost::ref(package), boost::ref(plane), boost::ref(wire)
328  )
329  );
330 
331  fTDCHits.push_back(
332  QwHit(
333  bank_index,
334  slot_num,
335  chan,
336  hitcnt,
337  kRegionID2,
338  package,
339  octant,
340  plane,
341  direction,
342  wire,
343  data
344  )
345  );
346  //in order-> bank index, slot num, chan, hitcount, region=2, package, plane,,direction, wire,wire hit time
347  if(local_debug) {
348  std::cout << "At QwDriftChamberHDC::FillRawTDCWord " << "\n";
349  std::cout << " hitcnt " << hitcnt
350  << " plane " << plane
351  << " wire " << wire
352  << " package " << package
353  << " fTDCHits.size() " << fTDCHits.size()
354  << std::endl;
355  }
356 
357  }
358 
359  }
360 
361  return;
362 }
363 
364 
365 
366 
368  const EQwDetectorPackage package,
369  const Int_t octant,
370  const Int_t plane,
371  const Int_t wire)
372 {
373  if (plane == kReferenceChannelPlaneNumber ){
374  LinkReferenceChannel(chan, plane, wire);
375  }
376  else {
377  fTDCPtrs.at(fCurrentTDCIndex).at(chan).fOctant = octant;
378  fTDCPtrs.at(fCurrentTDCIndex).at(chan).fPackage = package;
379  fTDCPtrs.at(fCurrentTDCIndex).at(chan).fPlane = plane;
380  fTDCPtrs.at(fCurrentTDCIndex).at(chan).fElement = wire;
381 
382  // std::cout << "fWiresPerPlane.size() " << fWiresPerPlane.size()
383  // << " plane " << plane
384  // << std::endl;
385 
386  if (plane >= (Int_t) fWiresPerPlane.size()){ // plane is Int_t
387  fWiresPerPlane.resize(plane+1);
388  // size() is one more larger than last plane number
389  // For HDC, plane 1,2,....,12
390  // vector 0,1,2,....,12
391  // thus, vector.size() returns 13
392  // So the magic number "1" is.
393  // Wednesday, July 28 21:56:34 EDT 2010, jhlee
394  }
395  if (wire>=fWiresPerPlane.at(plane)){
396  fWiresPerPlane.at(plane) = wire+1;
397  }
398  }
399  return OK;
400 }
401 
402 
404 {
405  bool temp_local_debug = false;
406 
407  if (temp_local_debug){
408  std::cout << " QwDriftChamberHDC::AddChannelDefinition"<<std::endl;
409  }
410 
411  std::size_t i =0;
412 
413  fWireData.resize(fWiresPerPlane.size());
414 
415  for (i=0; i<fWiresPerPlane.size(); i++) {
416 
417  if(temp_local_debug){
418  std::cout << "wire #" << i
419  << " " << fWiresPerPlane.at(i)
420  << std::endl;
421  }
422  fWireData.at(i).resize(fWiresPerPlane.at(i));
423  }
424 
425  Int_t mytdc = 0;
426  for ( i=0; i<fTDC_Index.size(); i++){
427  for (size_t j=0; j<fTDC_Index.at(i).size(); j++){
428  mytdc = fTDC_Index.at(i).at(j);
429  if (mytdc not_eq -1){
430  for (size_t k=0; k<fTDCPtrs.at(mytdc).size(); k++){
431  // Int_t package = fTDCPtrs.at(mytdc).at(k).fPackage;
432  Int_t plane = fTDCPtrs.at(mytdc).at(k).fPlane;
433  if (( plane>0) and (plane not_eq (Int_t) kReferenceChannelPlaneNumber) ){
434  Int_t wire = fTDCPtrs.at(mytdc).at(k).fElement;
435  fWireData.at(plane).at(wire).SetElectronics(i,j,k);
436  if(temp_local_debug) {
437  std::cout << "mytdc " << mytdc
438  << "wire #" << wire
439  << " plane " << plane
440  << " (i j k) (" << i <<" " << j << " "<< k << ")"
441  << std::endl;
442  }
443  }
444  }
445  }
446  }
447  }
448 
449  return OK;
450 }
451 
453 {
454  if (not HasDataLoaded()) return;
455 
456  SubtractReferenceTimes(); // A.U. unit
457  UpdateHits(); // Fill QwDetectorInfo, fTimeRes (ns), and fTimeNs (ns) in QwHits
458 
459  SubtractWireTimeOffset(); // Subtract t0 offset (ns) and educated guesss t0 (a.u.) from fTime (a.u.) and fTimeNs (ns)
460  FillDriftDistanceToHits(); // must call GetTimeNs() instead of GetTime()
461 
462  return;
463 }
464 
465 
466 Int_t QwDriftChamberHDC::LoadChannelMap(TString mapfile)
467 {
468  Bool_t local_debug = false;
469  LoadTtoDParameters ( "R2_TtoDTable.map" );
470  LoadTimeWireOffset ( "R2_timeoffset.map" );
471 
472  TString varname, varvalue;
473  UInt_t value = 0;
474  UInt_t chan = 0;
475  UInt_t DIRMODE = 0;
476  Int_t plane = 0;
477  Int_t wire = 0;
478  TString name = "";
479 
480  EQwDetectorPackage package = kPackageNull;
481  EQwDirectionID direction = kDirectionNull;
482 
483  fDirectionData.resize(2);//currently we have 2 package - Rakitha (10/23/2008)
484  fDirectionData.at(0).resize(12); //currently we have 12 wire planes in each package - Rakitha (10/23/2008)
485  fDirectionData.at(1).resize(12); //currently we have 12 wire planes in each package - Rakitha (10/23/2008)
486 
487  QwParameterFile mapstr(mapfile.Data()); //Open the file
488  fDetectorMaps.insert(mapstr.GetParamFileNameContents());
489 
490  while (mapstr.ReadNextLine()) {
491  mapstr.TrimComment('!'); // Remove everything after a '!' character.
492  mapstr.TrimWhitespace(); // Get rid of leading and trailing spaces.
493  if (mapstr.LineIsEmpty()) continue;
494 
495  if (mapstr.HasVariablePair("=",varname,varvalue)) {
496  // This is a declaration line. Decode it.
497  varname.ToLower();
498  value = QwParameterFile::GetUInt(varvalue);
499  if (value ==0){
500  value = atol(varvalue.Data());
501  }
502  if (varname=="roc") {
503  RegisterROCNumber(value,0);
504  DIRMODE=0;
505  }
506  else if (varname=="bank") {
507  RegisterSubbank(value);
508  DIRMODE=0;
509  }
510  else if (varname=="pkg") {
511  //this will identify the coming sequence is wire plane to direction mapping - Rakitha
512  DIRMODE=1;
513  package=(EQwDetectorPackage)value;
514  }
515  else if (varname=="slot") {
516  RegisterSlotNumber(value);
517  DIRMODE=0;
518  }
519 
520  }
521  else if (DIRMODE==0) {
522  // Break this line into tokens to process it.
523  chan = mapstr.GetTypedNextToken<Int_t>();
524  plane = mapstr.GetTypedNextToken<Int_t>();
525  // wire = mapstr.GetTypedNextToken<Int_t>();
526  name = mapstr.GetTypedNextToken<TString>();
527 
528  Int_t octant = 0;
529  fR2Octant = gQwOptions.GetValue<int>("R2-octant");
530  if (package == kPackage1) octant = (fR2Octant + 4) % 8;
531  if (package == kPackage2) octant = fR2Octant;
532 
533  if(local_debug) {
534  printf("chan %8d plan %4d wire %12s\n", chan, plane, name.Data());
535  }
536  if (plane==kReferenceChannelPlaneNumber) {
537  fF1RefContainer -> AddF1TDCReferenceSignal(new F1TDCReferenceSignal(fCurrentBankIndex, fCurrentSlot, chan, name));
538 
539  if (name=="MasterTrigger" ) {
540  wire = 0;
541  BuildWireDataStructure(chan, package, octant, plane, wire);
542  }
543  }
544  else {
545  wire = name.Atoi();
546  // VDC and HDC
547  BuildWireDataStructure(chan, package, octant, plane, wire);
548  }
549 
550  }
551  else if (DIRMODE==1) {
552  //this will decode the wire plane directions - Rakitha
553  plane = mapstr.GetTypedNextToken<Int_t>();
554  direction = (EQwDirectionID) mapstr.GetTypedNextToken<Int_t>();
555  fDirectionData.at(package-1).at(plane-1) = direction;
556  }
557 
558  }
559 
560 
561 
562 
563  // Construct the wire data structures.
565 
566  /*
567  for (size_t i=0; i<fDirectionData.at(0).size(); i++){
568  std::cout<<"Direction data Plane "<<i+1<<" "<<fDirectionData.at(0).at(i)<<std::endl;
569  }
570  */
571  //
572 
573  mapstr.Close(); // Close the file (ifstream)
574  // / ReportConfiguration();
575 
576  return OK;
577 }
578 
579 
580 
581 
582 // Int_t QwDriftChamberHDC::ProcessConfigurationBuffer(const UInt_t roc_id, const UInt_t bank_id, UInt_t* buffer, UInt_t num_words)
583 // {
584 // Int_t subbank_index = 0;
585 // Bool_t local_debug = false;
586 
587 // subbank_index = GetSubbankIndex(roc_id, bank_id);
588 // if ( local_debug ) {
589 // std::cout << "QwDriftChamberVDC::ProcessConfigurationBuffer" << std::endl;
590 // std::cout << " roc id " << roc_id
591 // << " bank_id " << bank_id
592 // << " subbank_index " << subbank_index
593 // << " num_words " << num_words
594 // << std::endl;
595 // }
596 
597 // if (subbank_index>=0 and num_words>0) {
598 // // SetDataLoaded(kTRUE);
599 // if (local_debug) {
600 // std::cout << "QwDriftChamberHDC::ProcessConfigurationBuffer: "
601 // << "Begin processing ROC" << roc_id << std::endl;
602 // PrintConfigrationBuffer(buffer,num_words);
603 // }
604 // }
605 
606 // return OK;
607 // }
608 
609 
611 {
612  options.AddOptions("Tracking options") ("R2-octant",
613  po::value<int>()->default_value(1),
614  "MD Package 2 of R2 is in front of" );
615 }
616 
618 {
619  fR2Octant = options.GetValue<int>("R2-octant");
620 }
621 
622 void QwDriftChamberHDC::ConstructHistograms(TDirectory *folder, TString& prefix)
623 {
624  // If we have defined a subdirectory in the ROOT file, then change into it.
625  if ( folder ) folder->cd();
626  // Now create the histograms...
627  TString region = GetSubsystemName();
628  // Loop over the number of planes.
629 
630  const Short_t buffer_size = 2000;
631  Float_t bin_offset = -0.5;
632 
633  std::size_t total_plane_number = 0;
634  total_plane_number = fWiresPerPlane.size();
635 
636  TotHits.resize(total_plane_number);
637  TOFP.resize(total_plane_number);
638  TOFP_raw.resize(total_plane_number);
639  WiresHit.resize(total_plane_number);
640  TOFW.resize(total_plane_number);
641  TOFW_raw.resize(total_plane_number);
642  HitsWire.resize(total_plane_number);
643 
644  std::size_t iplane = 0;
645 
646  //std::cout << "QwDriftChamberHDC::ConstructHistograms, "
647  // << "we are contructing histograms with index from 0 to " <<total_plane_number
648  // << "\n"
649  // << "Thus, fWiresPerPlane.size() returns "
650  // << total_plane_number
651  // << " and its array definition is ["
652  // << total_plane_number
653  // << "]."
654  // << " And hist[i] <-> hist.at(i) <-> fWiresPerplane[i] <-> fWiresPerPlane.at(i)"
655  // << std::endl;
656 
657  // wire_per_plane is the number of wire per plane?
658  //
659  // we skip the first zero-th plane or wire histogram. thus
660  // i starts with '1'. hist[0] is NULL
661 
662  for ( iplane=1; iplane<total_plane_number; iplane++) {
663 
664  // push_back can "push" iplane = 1 into TotHits.at(0) ??
665  TotHits[iplane] = new TH1F(
666  Form("%s%sHitsOnEachWirePlane%d", prefix.Data(), region.Data(), (Int_t) iplane),
667  Form("Total hits on all wires in plane %d", (Int_t) iplane),
668  fWiresPerPlane[iplane], bin_offset, fWiresPerPlane[iplane]+bin_offset
669  );
670 
671  TotHits[iplane]->GetXaxis()->SetTitle("Wire #");
672  TotHits[iplane]->GetYaxis()->SetTitle("Events");
673 
674  WiresHit[iplane] = new TH1F(
675  Form("%s%sWiresHitPlane%d", prefix.Data(), region.Data(), (Int_t) iplane),
676  Form("Number of Wires Hit in plane %d",(Int_t) iplane),
677  20, bin_offset, 20+bin_offset
678  );
679  WiresHit[iplane]->GetXaxis()->SetTitle("Wires Hit per Event");
680  WiresHit[iplane]->GetYaxis()->SetTitle("Events");
681 
682  HitsWire[iplane] = new TH2F(
683  Form("%s%sHitsOnEachWirePerEventPlane%d", prefix.Data(), region.Data(), (Int_t) iplane),
684  Form("hits on all wires per event in plane %d", (Int_t) iplane),
685  fWiresPerPlane[iplane],bin_offset,fWiresPerPlane[iplane]+bin_offset,
686  7, -bin_offset, 7-bin_offset
687  );
688  HitsWire[iplane]->GetXaxis()->SetTitle("Wire Number");
689  HitsWire[iplane]->GetYaxis()->SetTitle("Hits");
690 
691  TOFP[iplane] = new TH1F(
692  Form("%s%sTimeofFlightPlane%d", prefix.Data(), region.Data(), (Int_t) iplane),
693  Form("Subtracted time of flight for events in plane %d", (Int_t) iplane),
694  400,0,0
695  );
696  TOFP[iplane] -> SetDefaultBufferSize(buffer_size);
697  TOFP[iplane] -> GetXaxis()->SetTitle("Time of Flight");
698  TOFP[iplane] -> GetYaxis()->SetTitle("Hits");
699 
700 
701  TOFP_raw[iplane] = new TH1F(
702  Form("%s%sRawTimeofFlightPlane%d", prefix.Data(), region.Data(), (Int_t) iplane),
703  Form("Raw time of flight for events in plane %d", (Int_t) iplane),
704  // 400,-65000,65000);
705  400, 0,0
706  );
707  TOFP_raw[iplane] -> SetDefaultBufferSize(buffer_size);
708  TOFP_raw[iplane]->GetXaxis()->SetTitle("Time of Flight");
709  TOFP_raw[iplane]->GetYaxis()->SetTitle("Hits");
710 
711  TOFW[iplane] = new TH2F(
712  Form("%s%sTimeofFlightperWirePlane%d", prefix.Data(), region.Data(), (Int_t) iplane),
713  Form("Subtracted time of flight for each wire in plane %d",(Int_t) iplane),
714  fWiresPerPlane[iplane], bin_offset, fWiresPerPlane[iplane]+bin_offset,
715  100,-40000,65000
716  );
717  // why this range is not -65000 ??
718  TOFW[iplane]->GetXaxis()->SetTitle("Wire Number");
719  TOFW[iplane]->GetYaxis()->SetTitle("Time of Flight");
720 
721  TOFW_raw[iplane] = new TH2F(
722  Form("%s%sRawTimeofFlightperWirePlane%d", prefix.Data() ,region.Data(),(Int_t)iplane),
723  Form("Raw time of flight for each wire in plane %d",(Int_t)iplane),
724  fWiresPerPlane[iplane], bin_offset, fWiresPerPlane[iplane]+bin_offset,
725  100,-40000,65000
726  );
727  // why this range is not -65000 ??
728  TOFW_raw[iplane]->GetXaxis()->SetTitle("Wire Number");
729  TOFW_raw[iplane]->GetYaxis()->SetTitle("Time of Flight");
730  }
731  return;
732 }
733 
734 
735 
737 {
738  Bool_t local_debug = false;
739  if (not HasDataLoaded()) return;
740 
741  QwDetectorID this_detid;
742  QwDetectorInfo *this_det;
743 
744  // Fill all of the histograms.
745 
746  std::vector<Int_t> wireshitperplane(fWiresPerPlane.size(),0);
747 
748  UInt_t raw_time = 0;
749  Double_t time = 0.0;
750 
751  Int_t plane = 0;
752  Int_t element = 0;
753 
754  std::vector<QwHit>::iterator end=fTDCHits.end();
755  for(std::vector<QwHit>::iterator hit=fTDCHits.begin(); hit!=end; hit++) {
756 
757  this_detid = hit->GetDetectorID();
758  plane = this_detid.fPlane;
759  element = this_detid.fElement;
760 
761  if (plane<=0 or element<=0) {
762  if (local_debug) {
763  QwMessage << "QwDriftChamberHDC::FillHistograms: Bad plane or element index:"
764  << " fPlane = " << plane
765  << " fElement= " << element
766  << QwLog::endl;
767  }
768  continue;
769  }
770 
771 
772  this_det= &(fWireData.at(plane).at(element));
773 
774  if (hit->IsFirstDetectorHit()) {
775  // If this is the first hit for this detector, then let's plot the
776  // total number of hits this wire had.
777  HitsWire[plane]->Fill(element,this_det->GetNumHits());
778 
779  // Also increment the total number of events in whichthis wire was hit.
780  TotHits[plane]->Fill(element,1);
781  // Increment the number of wires hit in this plane
782  wireshitperplane.at(plane) += 1;
783  }
784 
785  raw_time = hit->GetRawTime();
786  time = hit->GetTimeNs();
787 
788  // Fill ToF histograms
789  TOFP_raw[plane]->Fill(raw_time);
790  TOFW_raw[plane]->Fill(element, raw_time);
791  TOFP [plane]->Fill(time);
792  TOFW [plane]->Fill(element, time);
793  }
794 
795  std::size_t iplane = 0;
796 
797  for (iplane=1; iplane<fWiresPerPlane.size(); iplane++) {
798  WiresHit[iplane]->Fill(wireshitperplane[iplane]);
799  }
800  return;
801 }
802 
803 
805 {
806  SetDataLoaded(kFALSE);
807  QwDetectorID this_det;
808  // Loop through fTDCHits, to decide which detector elements need to be cleared.
809  std::vector<QwHit>::iterator end=fTDCHits.end();
810  for (std::vector<QwHit>::iterator hit1=fTDCHits.begin(); hit1!=end; hit1++) {
811  this_det = hit1->GetDetectorID();
812  fWireData.at(this_det.fPlane).at(this_det.fElement).ClearHits();
813  }
814  fTDCHits.clear();
815 
817  std::size_t i = 0;
818  for (i=0; i<fReferenceData.size(); i++) {
819  fReferenceData.at(i).clear();
820  }
821  return;
822 }
823 
824 
825 
827 {
828 
829 
830  EQwDetectorPackage package = kPackageNull;
831  Int_t plane = 0;
832 
833  QwDetectorID local_id;
834  QwDetectorInfo * local_info;
835 
836  std::vector<QwHit>::iterator end=fTDCHits.end();
837  for(std::vector<QwHit>::iterator iter=fTDCHits.begin(); iter!=end; ++iter)
838  {
839 
840  local_id = iter->GetDetectorID();
841  package = local_id.fPackage;
842  plane = local_id.fPlane - 1;
843  // ahha, here is a hidden magic number 1.
844  local_info = fDetectorInfo.in(package).at(plane);
845  iter->SetDetectorInfo(local_info);
846  iter->ApplyTimeCalibration(fF1TDCResolutionNS); // Fill fTimeRes and fTimeNs in QwHit
847  }
848 
849 
850  return;
851 }
852 
853 
855 {
856  Int_t plane = 0;
857  Int_t wire = 0;
858  EQwDetectorPackage package = kPackageNull;
859  Double_t t0_NS = 0.0;
860 
861  Double_t educated_guess_t0_correction_AU = 11255.0;
862  Double_t educated_guess_t0_correction_NS = educated_guess_t0_correction_AU*fF1TDCResolutionNS;
863 
864  //hit -> SetTime(time+educated_guess_t0_correction); // an educated guess
865 
866  std::size_t nhits=fTDCHits.size();
867 
868  for(std::size_t i=0;i<nhits;i++)
869  {
870  package = fTDCHits.at(i).GetPackage();
871  plane = fTDCHits.at(i).GetPlane();
872  wire = fTDCHits.at(i).GetElement();
873  t0_NS = fTimeWireOffsets.at ( package-1 ).at ( plane-1 ).at ( wire-1 );
874  t0_NS = t0_NS - educated_guess_t0_correction_NS;
875 
876  fTDCHits.at(i).SubtractTimeNsOffset(t0_NS); // time unit is ns, Replace fTimeNs
877  fTDCHits.at(i).SubtractTimeAuOffset(t0_NS/fF1TDCResolutionNS); // time unit is a.u. Replace fTime
878  }
879  return ;
880 }
881 
882 void QwDriftChamberHDC::LoadTtoDParameters ( TString ttod_map )
883 {
884  QwParameterFile mapstr(ttod_map.Data());
885  fDetectorMaps.insert(mapstr.GetParamFileNameContents());
886 
887  while (mapstr.ReadNextLine())
888  {
889  mapstr.TrimComment('!');
890  mapstr.TrimWhitespace();
891  if (mapstr.LineIsEmpty()) continue;
892 
893  /* Double_t t = */ mapstr.GetTypedNextToken<Double_t>();
894  Double_t d = mapstr.GetTypedNextToken<Double_t>();
895  fTtoDNumbers.push_back(d);
896  }
897 
898  mapstr.Close(); // Close the file (ifstream)
899 }
900 
901 
903 {
904  //std::cout << "beginning to load t0 file... " << std::endl;
905  //
906  QwParameterFile mapstr ( t0_map.Data() );
907  fDetectorMaps.insert(mapstr.GetParamFileNameContents());
908 
909  TString varname,varvalue;
910  Int_t plane=0,wire=0;
911  Double_t t0 = 0.0;
912  EQwDetectorPackage package = kPackageNull;
913 
914  while ( mapstr.ReadNextLine() )
915  {
916  mapstr.TrimComment ( '!' );
917  mapstr.TrimWhitespace();
918  if ( mapstr.LineIsEmpty() ) continue;
919  if ( mapstr.HasVariablePair ( "=",varname,varvalue ) )
920  {
921  varname.ToLower();
922  if ( varname=="package" )
923  {
924  package = ( EQwDetectorPackage ) atoi ( varvalue.Data() );
925  if ( package> ( Int_t ) fTimeWireOffsets.size() ) fTimeWireOffsets.resize ( package );
926  }
927  else if ( varname=="plane" )
928  {
929  //std::cout << "package: " << fTimeWireOffsets.at(package-1).size()<< std::endl;
930  plane = atoi ( varvalue.Data() );
931  if ( plane> ( Int_t ) fTimeWireOffsets.at ( package-1 ).size() ) fTimeWireOffsets.at ( package-1 ).resize ( plane );
932  //std::cout << "plane: " << fTimeWireOffsets.at(package-1).size()<< std::endl;
933 
934  // To Siyuan, * : can package be obtained before plane in while loop? if plane is before package
935  // we have at(-1), thus, if condition is always "false", I guess.
936  // * : if, else if then can we expect something more?
937  // from Han
938  }
939  continue;
940  }
941 
942  wire = mapstr.GetTypedNextToken<Int_t>();
943  t0 = mapstr.GetTypedNextToken<Int_t>();
944 
945  if ( wire > ( Int_t ) fTimeWireOffsets.at ( package-1 ).at ( plane-1 ).size() )
946  {
947  fTimeWireOffsets.at ( package-1 ).at ( plane-1 ).resize ( wire );
948 
949  fTimeWireOffsets.at ( package-1 ).at ( plane-1 ).at ( wire-1 ) = t0;
950  }
951 
952  }
953  //
954 
955  mapstr.Close(); // Close the file (ifstream)
956  return OK;
957 }
#define QwMessage
Predefined log drain for regular messages.
Definition: QwLog.h:50
std::vector< TH1F * > TOFP
std::vector< std::vector< Int_t > > fTDC_Index
std::vector< TH1F * > WiresHit
std::map< TString, TString > fDetectorMaps
Definition: VQwSubsystem.h:322
size_t fCurrentBankIndex
Name of this subsystem (the region).
std::vector< std::vector< std::vector< Double_t > > > fTimeWireOffsets
std::vector< TH2F * > HitsWire
std::vector< TH2F * > TOFW_raw
const QwGeometry in(const EQwRegionID &r) const
Get detectors in given region.
Definition: QwGeometry.h:92
An options class.
Definition: QwOptions.h:133
static UInt_t GetUInt(const TString &varvalue)
Double_t CalculateDriftDistance(Double_t drifttime, QwDetectorID detector)
std::vector< std::vector< UInt_t > > fDirectionData
Bool_t HasDataLoaded() const
Definition: VQwSubsystem.h:94
Double_t GetReferenceTimeAU(Int_t bank_index, TString name)
QwOptions gQwOptions
Definition: QwOptions.cc:27
std::vector< TH2F * > TOFW
EQwDetectorPackage
Definition: QwTypes.h:70
virtual void ConstructHistograms()
Construct the histograms for this subsystem.
Definition: VQwSubsystem.h:209
std::vector< TH1F * > TOFP_raw
po::options_description_easy_init AddOptions(const std::string &blockname="Specialized options")
Add an option to a named block or create new block.
Definition: QwOptions.h:164
Int_t fElement
trace number for R1; wire number for R2 &amp; R3; PMT number for others
Definition: QwTypes.h:253
T GetValue(const std::string &key)
Get a templated value.
Definition: QwOptions.h:240
static void DefineOptions()
Define options function (note: no virtual static functions in C++)
Definition: VQwSubsystem.h:88
A logfile class, based on an identical class in the Hermes analyzer.
void FillHistograms()
Fill the histograms for this subsystem.
Int_t LinkReferenceChannel(const UInt_t chan, const Int_t plane, const Int_t wire)
std::vector< std::vector< QwDetectorID > > fTDCPtrs
QwGeometry fDetectorInfo
Geometry information of this subsystem.
void ProcessOptions(QwOptions &options)
Process the command line options.
Double_t ReferenceSignalCorrection(Double_t raw_time, Double_t ref_time, Int_t bank_index, Int_t slot)
void FillRawTDCWord(Int_t bank_index, Int_t slot_num, Int_t chan, UInt_t data)
void SetReferenceSignal(Int_t bank_index, Int_t slot, Int_t chan, UInt_t data, Bool_t debug=false)
Int_t fPlane
R or theta index for R1; plane index for R2 &amp; R3.
Definition: QwTypes.h:251
QwF1TDContainer * fF1TDContainer
Bool_t WireMatches(EQwRegionID region, EQwDetectorPackage package, Int_t plane, Int_t wire)
Definition: QwHit.cc:357
std::vector< TH1F * > TotHits
Int_t LoadChannelMap(TString mapfile)
Mandatory map file definition.
Int_t BuildWireDataStructure(const UInt_t chan, const EQwDetectorPackage package, const Int_t octant, const Int_t plane, const Int_t wire)
std::vector< Double_t > fTtoDNumbers
std::vector< std::vector< Double_t > > fReferenceData
Int_t RegisterSubbank(const UInt_t bank_id)
static std::ostream & endl(std::ostream &)
End of the line.
Definition: QwLog.cc:299
Int_t GetTDCIndex(size_t bank_index, size_t slot_num) const
EQwDirectionID
Definition: QwTypes.h:41
Int_t RegisterSlotNumber(const UInt_t slot_id)
static const Int_t kReferenceChannelPlaneNumber
Hit structure uniquely defining each hit.
Definition: QwHit.h:43
void LoadTtoDParameters(TString ttod_map)
std::vector< QwHit > fTDCHits
Int_t LoadTimeWireOffset(TString t0_map)
#define OK
void FillDriftDistanceToHits()
std::vector< Int_t > fWiresPerPlane
#define RegisterSubsystemFactory(A)
Definition: QwFactory.h:230
TString GetSubsystemName() const
Definition: VQwSubsystem.h:93
Int_t RegisterROCNumber(const UInt_t roc_id, const UInt_t bank_id)
Tell the object that it will decode data from this ROC and sub-bank.
F1TDCReferenceContainer * fF1RefContainer
std::vector< std::vector< QwDetectorInfo > > fWireData
void SetDataLoaded(Bool_t flag)
Definition: VQwSubsystem.h:305