Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DEventProcessor_fdc_covariance_hists.cc
Go to the documentation of this file.
1 // $Id$
2 //
3 // File: DEventProcessor_fdc_covariance_hists.cc
4 // Created: Mon Apr 20 10:18:30 EDT 2009
5 // Creator: davidl (on Darwin harriet.jlab.org 9.6.0 Darwin Kernel Version 9.6.0)
6 //
7 
8 #include <iostream>
9 #include <iomanip>
10 #include <cmath>
11 using namespace std;
12 
14 
15 #include <TROOT.h>
16 
17 #include <JANA/JApplication.h>
18 #include <JANA/JEventLoop.h>
19 using namespace jana;
20 
21 #include <DANA/DApplication.h>
22 #include <TRACKING/DMCThrown.h>
23 #include <TRACKING/DMCTrackHit.h>
25 #include <FDC/DFDCGeometry.h>
26 #include <FDC/DFDCPseudo.h>
27 #include <HDGEOMETRY/DGeometry.h>
28 #include <DVector2.h>
29 #include <particleType.h>
30 
31 
32 // Routine used to create our DEventProcessor
33 extern "C"{
34 void InitPlugin(JApplication *app){
35  InitJANAPlugin(app);
36  app->AddProcessor(new DEventProcessor_fdc_covariance_hists());
37 }
38 } // "C"
39 
40 
41 //------------------
42 // DEventProcessor_fdc_covariance_hists
43 //------------------
45 {
46  pthread_mutex_init(&mutex, NULL);
47 
48  Nevents = 0;
49 }
50 
51 //------------------
52 // ~DEventProcessor_fdc_covariance_hists
53 //------------------
55 {
56 
57 }
58 
59 //------------------
60 // init
61 //------------------
63 {
64  // Create TRACKING directory
65  TDirectory *dir = (TDirectory*)gROOT->FindObject("TRACKING");
66  if(!dir)dir = new TDirectoryFile("TRACKING","TRACKING");
67  dir->cd();
68 
69  fdc_cov = new TProfile2D("fdc_cov","FDC Covariance calculated from residuals", 24, 0.5, 24.5, 24, 0.5, 24.5);
70  fdc_cov->SetStats(0);
71  fdc_cov->SetXTitle("Layer number");
72  fdc_cov->SetYTitle("Layer number");
73  fdc_cov_calc = (TProfile2D*)fdc_cov->Clone("fdc_cov_calc");
74  fdc_cov_calc->SetTitle("FDC Covariance calculated from materials");
75 
76  dir->cd("../");
77 
78  return NOERROR;
79 }
80 
81 //------------------
82 // brun
83 //------------------
84 jerror_t DEventProcessor_fdc_covariance_hists::brun(JEventLoop *loop, int32_t runnumber)
85 {
86  DApplication* dapp = dynamic_cast<DApplication*>(loop->GetJApplication());
87  if(!dapp){
88  _DBG_<<"Cannot get DApplication from JEventLoop! (are you using a JApplication based program perhaps?)"<<endl;
89  return RESOURCE_UNAVAILABLE;
90  }
91  bfield=dapp->GetBfield(runnumber);
92 
93  // Get z-position of most upstream FDC layer
94  vector<double> z_wires;
95  dapp->GetDGeometry(runnumber)->GetFDCZ(z_wires);
96  Z_fdc1 = z_wires[0];
97 
98  return NOERROR;
99 }
100 
101 //------------------
102 // erun
103 //------------------
105 {
106  return NOERROR;
107 }
108 
109 //------------------
110 // fini
111 //------------------
113 {
114  return NOERROR;
115 }
116 
117 //------------------
118 // evnt
119 //------------------
120 jerror_t DEventProcessor_fdc_covariance_hists::evnt(JEventLoop *loop, uint64_t eventnumber)
121 {
122  vector<const DMCThrown*> mcthrowns;
123  vector<const DMCTrackHit*> mctrackhits;
124  vector<const DMCTrajectoryPoint*> mctrajectorypoints;
125  vector<const DFDCPseudo*> fdcpseudohits;
126 
127  loop->Get(mcthrowns);
128  loop->Get(mctrackhits);
129  loop->Get(mctrajectorypoints);
130  loop->Get(fdcpseudohits);
131 
132  Nevents++;
133 
134  // Only look at events with one thrown and one reconstructed particle
135  if(mcthrowns.size() !=1){
136  _DBG_<<" mcthrowns.size()="<<mcthrowns.size()<<endl;
137  return NOERROR;
138  }
139 
140  // Look for truth point corresponding to hit of upstream most
141  // FDC layer. If we don't find one, skip this event.
142  const DMCTrackHit *mctrackhit1 = NULL;
143  for(unsigned int i=0; i<mctrackhits.size(); i++){
144  const DMCTrackHit *hit = mctrackhits[i];
145  if(hit->system == SYS_FDC){
146  if((hit->z < (Z_fdc1+0.5)) && (hit->z > (Z_fdc1-0.5))){
147  mctrackhit1 = hit;
148  break;
149  }
150  }
151  }
152  if(!mctrackhit1)return NOERROR;
153  DVector3 pos_mctrackhit1;
154  pos_mctrackhit1.SetXYZ(mctrackhit1->r*cos(mctrackhit1->phi), mctrackhit1->r*sin(mctrackhit1->phi), mctrackhit1->z);
155 
156  // Look for trajectory point closest to first FDC plane.
157  // Simultaneously, record distance to first layer.
158  DVector3 pos_fdc1;
159  DVector3 mom_fdc1;
160  double s1 = 0.0;
161  double diff_min = 1.0E6;
162  double s1_min = 0.0;
163  for(unsigned int i=0; i<mctrajectorypoints.size(); i++){
164  const DMCTrajectoryPoint *traj = mctrajectorypoints[i];
165  DVector3 pos_traj(traj->x, traj->y, traj->z);
166  double diff = (pos_traj-pos_mctrackhit1).Mag();
167  s1 += (double)traj->step;
168  if(diff<diff_min){
169  pos_fdc1.SetXYZ(traj->x, traj->y, traj->z);
170  mom_fdc1.SetXYZ(traj->px, traj->py, traj->pz);
171  s1_min = s1;
172  diff_min = diff;
173  }
174  }
175  s1 = s1_min;
176 
177  DApplication* dapp = dynamic_cast<DApplication*>(loop->GetJApplication());
178  DReferenceTrajectory *rt = new DReferenceTrajectory(bfield);
179  rt->SetDRootGeom(dapp->GetRootGeom());
180  rt->Swim(pos_fdc1, mom_fdc1);
181 
182  // Lock mutex
183  pthread_mutex_lock(&mutex);
184 
185  // Loop over FDC hits
186  vector<double> resi_by_layer(24,-1000);
187  vector<const DReferenceTrajectory::swim_step_t*> step_by_layer(24, (const DReferenceTrajectory::swim_step_t*)NULL);
188  for(unsigned int i=0; i<fdcpseudohits.size(); i++){
189  const DFDCPseudo *fdcpseudohit = fdcpseudohits[i];
190 
191  double s;
192  DVector3 pos_doca, mom_doca;
193  double doca = rt->DistToRT(fdcpseudohit->wire, &s);
194  rt->GetLastDOCAPoint(pos_doca, mom_doca);
195 
196  // Estimate TOF assuming pion
197  double mass = 0.13957;
198  double beta = 1.0/sqrt(1.0 + pow(mass/mom_doca.Mag(), 2.0))*2.998E10;
199  double tof = (s+s1)/beta/1.0E-9; // in ns
200  double dist = (fdcpseudohit->time - tof)*55E-4;
201  double resi = dist - doca;
202 
203  int layer = fdcpseudohit->wire->layer;
204  if(layer>=1 && layer<=24){
205  resi_by_layer[layer-1] = resi;
206  step_by_layer[layer-1] = rt->GetLastSwimStep();
207  }
208  }
209 
210  // Fill direct covariance matrix
211  for(int layer1=1; layer1<=24; layer1++){
212  double resi1 = resi_by_layer[layer1-1];
213  if(!finite(resi1) || fabs(resi1)>100.0)continue;
214 
215  for(int layer2=layer1; layer2<=24; layer2++){
216  double resi2 = resi_by_layer[layer2-1];
217  if(!finite(resi2) || fabs(resi2)>100.0)continue;
218 
219  fdc_cov->Fill(layer1, layer2, resi1*resi2);
220  fdc_cov->Fill(layer2, layer1, resi1*resi2);
221  }
222  }
223 
224  // Fill covariance matrix calculated from materials
225  const DReferenceTrajectory::swim_step_t* step0 = step_by_layer[0];
226  if(step0){
227  for(int layerA=1; layerA<=24; layerA++){
228  const DReferenceTrajectory::swim_step_t* stepA = step_by_layer[layerA-1];
229  if(!stepA)continue;
230 
231  for(int layerB=layerA; layerB<=24; layerB++){
232  const DReferenceTrajectory::swim_step_t* stepB = step_by_layer[layerB-1];
233  if(!stepB)continue;
234 
235  // Correlations between A and B are due only to material between
236  // the first detector and the most upstream of A or B.
237  double sA = stepA->s;
238  double sB = stepB->s;
239  const DReferenceTrajectory::swim_step_t *step_end = sA<sB ? stepA:stepB;
240 
241  if(step0->s>step_end->s)continue; // Bullet proof
242 
243  double itheta02 = step_end->itheta02 - step0->itheta02;
244  double itheta02s = step_end->itheta02s - step0->itheta02s;
245  double itheta02s2 = step_end->itheta02s2 - step0->itheta02s2;
246  double sigmaAB = sA*sB*itheta02 -(sA+sB)*itheta02s + itheta02s2;
247 
248  fdc_cov_calc->Fill(layerA, layerB, sigmaAB);
249  fdc_cov_calc->Fill(layerB, layerA, sigmaAB);
250  }
251  }
252  }
253 
254  // Unlock mutex
255  pthread_mutex_unlock(&mutex);
256 
257  delete rt;
258 
259  return NOERROR;
260 }
261 
DApplication * dapp
bool GetFDCZ(vector< double > &z_wires) const
z-locations for each of the FDC wire planes in cm
Definition: DGeometry.cc:1221
const swim_step_t * GetLastSwimStep(void) const
Int_t layer
TVector3 DVector3
Definition: DVector3.h:14
DMagneticFieldMap * GetBfield(unsigned int run_number=1)
DVector3 GetLastDOCAPoint(void) const
void Swim(const DVector3 &pos, const DVector3 &mom, double q=-1000.0, const TMatrixFSym *cov=NULL, double smax=2000.0, const DCoordinateSystem *wire=NULL)
const DFDCWire * wire
DFDCWire for this wire.
Definition: DFDCPseudo.h:92
class DFDCPseudo: definition for a reconstructed point in the FDC
Definition: DFDCPseudo.h:74
jerror_t fini(void)
Invoked via DEventProcessor virtual method.
int layer
1-24
Definition: DFDCWire.h:16
DetectorSystem_t system
particle type
Definition: DMCTrackHit.h:25
jerror_t evnt(jana::JEventLoop *loop, uint64_t eventnumber)
Invoked via DEventProcessor virtual method.
DRootGeom * GetRootGeom(unsigned int run_number)
double DistToRT(double x, double y, double z) const
float z
coordinates of hit in cm and rad
Definition: DMCTrackHit.h:20
InitPlugin_t InitPlugin
Definition: GlueX.h:18
DGeometry * GetDGeometry(unsigned int run_number)
#define _DBG_
Definition: HDEVIO.h:12
void SetDRootGeom(const DRootGeom *RootGeom)
double time
time corresponding to this pseudopoint.
Definition: DFDCPseudo.h:93
double sqrt(double)
double sin(double)
jerror_t init(void)
Invoked via DEventProcessor virtual method.
double Nevents
jerror_t erun(void)
Invoked via DEventProcessor virtual method.
jerror_t brun(jana::JEventLoop *loop, int32_t runnumber)
TDirectory * dir
Definition: bcal_hist_eff.C:31