Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DCDCTrackHit_factory.cc
Go to the documentation of this file.
1 // $Id$
2 //
3 // File: DCDCTrackHit_factory.cc
4 // Created: Mon Oct 16 10:20:07 EDT 2006
5 // Creator: davidl (on Darwin swire-b241.jlab.org 8.7.0 powerpc)
6 //
7 
8 #include <cmath>
9 #include <pthread.h>
10 using namespace std;
11 
12 #include "DCDCTrackHit_factory.h"
13 #include "DCDCHit.h"
14 #include <HDGEOMETRY/DGeometry.h>
16 #include <TRACKING/DMCTrackHit.h>
17 
19  if (cdcwires.size()){
20  for (unsigned int i=0;i<cdcwires.size();i++){
21  for (unsigned int j=0;j<cdcwires[i].size();j++){
22  delete cdcwires[i][j];
23  }
24  }
25  }
26 }
27 
28 //------------------
29 // init
30 //------------------
32 {
33  MATCH_TRUTH_HITS=false;
34 
35  gPARMS->SetDefaultParameter("CDC:MATCH_TRUTH_HITS",MATCH_TRUTH_HITS,"Match truth hits to CDC hits (DEF=false)");
36 
37  return NOERROR;
38 }
39 
40 //------------------
41 // brun
42 //------------------
43 jerror_t DCDCTrackHit_factory::brun(JEventLoop *loop, int32_t runnumber)
44 {
45  // Get pointer to DGeometry object
46  DApplication* dapp=dynamic_cast<DApplication*>(eventLoop->GetJApplication());
47  dgeom = dapp->GetDGeometry(runnumber);
48 
49  // Get the CDC wire table from the XML
50  //jout<< "Getting map of cdc wires from the XML" <<endl;
51  dgeom->GetCDCWires(cdcwires);
52 
53  // Fill array with the number of straws for each layer
54  for (unsigned int i=0;i<cdcwires.size();i++){
55  Nstraws[i]=cdcwires[i].size();
56  }
57 
58  // Get drift time parameters
59  JCalibration *jcalib = dapp->GetJCalibration((loop->GetJEvent()).GetRunNumber());
60  map<string, double> cdc_drift_parms;
61  jcalib->Get("CDC/cdc_drift_parms", cdc_drift_parms);
62  CDC_DRIFT_BSCALE_PAR1 = cdc_drift_parms["bscale_par1"];
63  CDC_DRIFT_BSCALE_PAR2 = cdc_drift_parms["bscale_par2"];
64 
65  typedef map<string,double>::iterator iter_double;
66  vector< map<string, double> > tvals;
67  if (jcalib->Get("CDC/cdc_drift_table", tvals)==false){
68  for(unsigned int i=0; i<tvals.size(); i++){
69  map<string, double> &row = tvals[i];
70  iter_double iter = row.find("t");
71  cdc_drift_table.push_back(1000.*iter->second);
72  }
73  }
74  if(cdc_drift_table.empty()){
75  jerr << endl;
76  jerr << " No values found for \"CDC/cdc_drift_table\"!" <<endl;
77  jerr << endl;
78  jerr << " This probably means you'r using an old calibration DB." << endl;
79  jerr << " Check your JANA_CALIB_URL environment variable." << endl;
80  jerr << " (This message printed from DCDCTrackHit_factory::brun())" << endl;
81  exit(-1);
82  }
83  cdc_drift_table_min = cdc_drift_table[0];
84  cdc_drift_table_max = cdc_drift_table[cdc_drift_table.size()-1];
85 
86  return NOERROR;
87 }
88 
90  if (cdcwires.size()){
91  for (unsigned int i=0;i<cdcwires.size();i++){
92  for (unsigned int j=0;j<cdcwires[i].size();j++){
93  delete cdcwires[i][j];
94  }
95  }
96  }
97  cdcwires.clear();
98  return NOERROR;
99 }
100 
101 //------------------
102 // evnt
103 //------------------
104 jerror_t DCDCTrackHit_factory::evnt(JEventLoop *loop, uint64_t eventnumber)
105 {
106  /// Convert from ring/straw indexing to x/y position
107  /// of wire center and stereo angle.
108  vector<const DCDCHit*> cdchits;
109  loop->Get(cdchits);
110  if (cdchits.size()==0) return NOERROR;
111 
112  // If this is simulated data then we want to match up the truth hit
113  // with this "real" hit. Ideally, this would be done at the
114  // DCDCHit object level, but the organization of the data in HDDM
115  // makes that difficult. Here we have the full wire definition so
116  // we make the connection here.
117  vector<const DMCTrackHit*> mctrackhits;
118  if (MATCH_TRUTH_HITS)loop->Get(mctrackhits);
119 
120  for(unsigned int i=0; i<cdchits.size(); i++){
121  const DCDCHit* cdchit = cdchits[i];
122 
123  if(cdchit->ring>CDC_MAX_RINGS || cdchit->ring<1
124  || cdchit->straw>Nstraws[cdchit->ring-1] || cdchit->straw<1){
125  cerr<<__FILE__<<":"<<__LINE__<<" Ring or straw out of range! ring="
126  <<cdchit->ring<<" (should be 1-"<<CDC_MAX_RINGS<<") straw="
127  <<cdchit->straw<<" (should be 1-"<<Nstraws[cdchit->ring-1]<<")"<<endl;
128  continue;
129  }
130 
131  DCDCTrackHit *hit = new DCDCTrackHit;
132  hit->wire = cdcwires[cdchit->ring-1][cdchit->straw-1];
133  hit->is_stereo=((cdchit->ring>4&&cdchit->ring<13)
134  ||(cdchit->ring>16&&cdchit->ring<25))
135  ?true:false;
136  hit->tdrift = cdchit->t;
137 
138  double w_eff=29.5e-9;
139  double gas_gain=1e5;
140  double electron_charge=1.6022e-4; /* fC */
141  double dEscale=w_eff/(gas_gain*electron_charge);
142  hit->dE=cdchit->q*dEscale;
143  hit->dE_amp=cdchit->amp*dEscale; // using Naomi's scale factor
144 
145  // Calculate drift distance assuming no tof to wire for now.
146  // The tracking package will replace this once an estimate
147  // of the tof to the wire is known. This algorithm was copied
148  // from the method DTrackFitterKalmanSIMD::ComputeCDCDrift
149  // so that a distance could be used for drawing the CDC
150  // drift times in hdview2, even when track fitting isn't done.
151  double B = 2.0; // just assume 2T B-field everywhere
152  double dtc =(CDC_DRIFT_BSCALE_PAR1 + CDC_DRIFT_BSCALE_PAR2 * B)* hit->tdrift;
153  double tcorr = hit->tdrift - dtc;
154  unsigned int index=0;
155  if(tcorr < cdc_drift_table_min){
156  index = 0;
157  }else if (tcorr >= cdc_drift_table_max){
158  index = cdc_drift_table.size()-2;
159  }else{
161  }
163  double frac=(tcorr-cdc_drift_table[index])/dt;
164  hit->dist = 0.01*(double(index)+frac); // the actual drift distance is calculated later, use a placeholder value here
165 
166  hit->AddAssociatedObject(cdchit);
167 
168  if (MATCH_TRUTH_HITS==true&&mctrackhits.size()>0){
169  // Estimate for drift distance, ignoring flight time
170  double d=0.;
171  if (cdchit->t>0) d=0.0279*sqrt(cdchit->t);
172 
173  // Try matching truth hit with this "real" hit.
174  const DMCTrackHit *mctrackhit = DTrackHitSelectorTHROWN::GetMCTrackHit(hit->wire,d, mctrackhits);
175 
176 
177  if(mctrackhit)hit->AddAssociatedObject(mctrackhit);
178  }
179 
180  _data.push_back(hit);
181  }
182 
183  return NOERROR;
184 }
185 
186 //------------------
187 // locate
188 // Locate a position in vector xx given x
189 // (this copied from DTrackFitterKalmanSIMD.cc)
190 //------------------
191 unsigned int DCDCTrackHit_factory::locate(vector<double>&xx,double x){
192  int ju,jm,jl;
193  int ascnd;
194 
195  int n=xx.size();
196 
197  jl=-1;
198  ju=n;
199  ascnd=(xx[n-1]>=xx[0]);
200  while(ju-jl>1){
201  jm=(ju+jl)>>1;
202  if ( (x>=xx[jm])==ascnd)
203  jl=jm;
204  else
205  ju=jm;
206  }
207  if (x==xx[0]) return 0;
208  else if (x==xx[n-1]) return n-2;
209  return jl;
210 }
211 
jerror_t evnt(JEventLoop *loop, uint64_t eventnumber)
Invoked via JEventProcessor virtual method.
DApplication * dapp
const DCDCWire * wire
Definition: DCDCTrackHit.h:37
Double_t x[NCHANNELS]
Definition: st_tw_resols.C:39
jerror_t brun(JEventLoop *loop, int32_t runnumber)
float amp
Definition: DCDCHit.h:21
static char index(char c)
Definition: base64.cpp:115
static const DMCTrackHit * GetMCTrackHit(const DCoordinateSystem *wire, double rdrift, vector< const DMCTrackHit * > &mctrackhits, int trackno_filter=-1)
static void locate(const double *xx, int n, double x, int *j)
bool GetCDCWires(vector< vector< DCDCWire * > > &cdcwires) const
Definition: DGeometry.cc:773
float t
Definition: DCDCHit.h:22
vector< double > cdc_drift_table
DGeometry * GetDGeometry(unsigned int run_number)
int ring
Definition: DCDCHit.h:18
#define CDC_MAX_RINGS
float q
Definition: DCDCHit.h:20
double sqrt(double)
unsigned int locate(vector< double > &xx, double x)
int straw
Definition: DCDCHit.h:19