Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DEventProcessor_cdc_covariance_hists.cc
Go to the documentation of this file.
1 // $Id$
2 //
3 // File: DEventProcessor_cdc_covariance_hists.cc
4 // Created: Thu Apr 23 08:30:24 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>
26 #include <HDGEOMETRY/DGeometry.h>
27 #include <DVector2.h>
28 #include <particleType.h>
29 
30 
31 // Routine used to create our DEventProcessor
32 extern "C"{
33 void InitPlugin(JApplication *app){
34  InitJANAPlugin(app);
35  app->AddProcessor(new DEventProcessor_cdc_covariance_hists());
36 }
37 } // "C"
38 
39 
40 //------------------
41 // DEventProcessor_cdc_covariance_hists
42 //------------------
44 {
45  pthread_mutex_init(&mutex, NULL);
46 
47  Nevents = 0;
48 }
49 
50 //------------------
51 // ~DEventProcessor_cdc_covariance_hists
52 //------------------
54 {
55 
56 }
57 
58 //------------------
59 // init
60 //------------------
62 {
63  // Create TRACKING directory
64  TDirectory *dir = (TDirectory*)gROOT->FindObject("TRACKING");
65  if(!dir)dir = new TDirectoryFile("TRACKING","TRACKING");
66  dir->cd();
67 
68  cdc_cov = new TProfile2D("cdc_cov","CDC Covariance calculated from residuals", 27, 0.5, 27.5, 27, 0.5, 27.5);
69  cdc_cov->SetStats(0);
70  cdc_cov->SetXTitle("Ring number");
71  cdc_cov->SetYTitle("Ring number");
72  cdc_cov_calc = (TProfile2D*)cdc_cov->Clone("cdc_cov_calc");
73  cdc_cov_calc->SetTitle("CDC Covariance calculated from materials");
74 
75  dir->cd("../");
76 
77  return NOERROR;
78 }
79 
80 //------------------
81 // brun
82 //------------------
83 jerror_t DEventProcessor_cdc_covariance_hists::brun(JEventLoop *loop, int32_t runnumber)
84 {
85  // We need to make a DReferenceTrajectory which means we need the B-field
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  LockState();
92  {
93  bfield=dapp->GetBfield(runnumber);
94 
95  // Get radius of innermost CDC layer
96  //const DCDCWire *wire1=DCDCTrackHit_factory::GetCDCWire(1,1);
97  //R_cdc1 = wire1->origin.Perp();
98  R_cdc1 = 10.960;
99  }
100  UnlockState();
101 
102  return NOERROR;
103 }
104 
105 //------------------
106 // erun
107 //------------------
109 {
110  return NOERROR;
111 }
112 
113 //------------------
114 // fini
115 //------------------
117 {
118  return NOERROR;
119 }
120 
121 //------------------
122 // evnt
123 //------------------
124 jerror_t DEventProcessor_cdc_covariance_hists::evnt(JEventLoop *loop, uint64_t eventnumber)
125 {
126  vector<const DMCThrown*> mcthrowns;
127  vector<const DMCTrackHit*> mctrackhits;
128  vector<const DMCTrajectoryPoint*> mctrajectorypoints;
129  vector<const DCDCTrackHit*> cdctrackhits;
130 
131  loop->Get(mcthrowns);
132  loop->Get(mctrackhits);
133  loop->Get(mctrajectorypoints);
134  loop->Get(cdctrackhits);
135 
136  Nevents++;
137 
138  // Only look at events with one thrown and one reconstructed particle
139  if(mcthrowns.size() !=1){
140  _DBG_<<" mcthrowns.size()="<<mcthrowns.size()<<endl;
141  return NOERROR;
142  }
143 
144  // Look for truth point corresponding to hit of innermost
145  // CDC layer. If we don't find one, skip this event.
146  const DMCTrackHit *mctrackhit1 = NULL;
147  for(unsigned int i=0; i<mctrackhits.size(); i++){
148  const DMCTrackHit *hit = mctrackhits[i];
149  if(hit->system == SYS_CDC){
150  if((hit->r < (R_cdc1+0.8)) && (hit->r > (R_cdc1-0.8))){
151  mctrackhit1 = hit;
152  break;
153  }
154  }
155  }
156  if(!mctrackhit1)return NOERROR;
157  DVector3 pos_mctrackhit1;
158  pos_mctrackhit1.SetXYZ(mctrackhit1->r*cos(mctrackhit1->phi), mctrackhit1->r*sin(mctrackhit1->phi), mctrackhit1->z);
159 
160  // Look for trajectory point closest to first CDC layer hit.
161  // Simultaneously, record distance to first layer.
162  DVector3 pos_cdc1;
163  DVector3 mom_cdc1;
164  double s1 = 0.0;
165  double diff_min = 1.0E6;
166  double s1_min = 0.0;
167  for(unsigned int i=0; i<mctrajectorypoints.size(); i++){
168  const DMCTrajectoryPoint *traj = mctrajectorypoints[i];
169  DVector3 pos_traj(traj->x, traj->y, traj->z);
170  double diff = (pos_traj-pos_mctrackhit1).Mag();
171  s1 += (double)traj->step;
172  if(diff<diff_min){
173  pos_cdc1.SetXYZ(traj->x, traj->y, traj->z);
174  mom_cdc1.SetXYZ(traj->px, traj->py, traj->pz);
175  s1_min = s1;
176  diff_min = diff;
177  }
178  }
179  s1 = s1_min;
180 
181  DReferenceTrajectory *rt = new DReferenceTrajectory(bfield);
182  DApplication* dapp = dynamic_cast<DApplication*>(loop->GetJApplication());
183  rt->SetDRootGeom(dapp->GetRootGeom());
184  rt->Swim(pos_cdc1, mom_cdc1);
185 
186  // FILL HISTOGRAMS
187  // Since we are filling histograms local to this plugin, it will not interfere with other ROOT operations: can use plugin-wide ROOT fill lock
188  RootFillLock(this); //ACQUIRE ROOT FILL LOCK
189 
190  // Loop over CDC hits
191  vector<double> resi_by_layer(27,-1000);
192  vector<const DReferenceTrajectory::swim_step_t*> step_by_layer(27, (const DReferenceTrajectory::swim_step_t*)NULL);
193  for(unsigned int i=0; i<cdctrackhits.size(); i++){
194  const DCDCTrackHit *cdctrackhit = cdctrackhits[i];
195 
196  double s;
197  DVector3 pos_doca, mom_doca;
198  double doca = rt->DistToRT(cdctrackhit->wire, &s);
199  rt->GetLastDOCAPoint(pos_doca, mom_doca);
200 
201  // Estimate TOF assuming pion
202  double mass = 0.13957;
203  double beta = 1.0/sqrt(1.0 + pow(mass/mom_doca.Mag(), 2.0))*2.998E10;
204  double tof = (s+s1)/beta/1.0E-9; // in ns
205  double dist = (cdctrackhit->tdrift - tof)*55E-4;
206  double resi = dist - doca;
207 
208  int layer = cdctrackhit->wire->ring;
209  if(layer>=1 && layer<=27){
210  resi_by_layer[layer-1] = resi;
211  step_by_layer[layer-1] = rt->GetLastSwimStep();
212  }
213  }
214 
215  // Fill direct covariance matrix
216  for(int layer1=1; layer1<=27; layer1++){
217  double resi1 = resi_by_layer[layer1-1];
218  if(!finite(resi1) || fabs(resi1)>100.0)continue;
219 
220  for(int layer2=layer1; layer2<=27; layer2++){
221  double resi2 = resi_by_layer[layer2-1];
222  if(!finite(resi2) || fabs(resi2)>100.0)continue;
223 
224  cdc_cov->Fill(layer1, layer2, resi1*resi2);
225  cdc_cov->Fill(layer2, layer1, resi1*resi2);
226  }
227  }
228 
229  // Fill covariance matrix calculated from materials
230  const DReferenceTrajectory::swim_step_t* step0 = step_by_layer[0];
231  if(step0){
232  for(int layerA=1; layerA<=27; layerA++){
233  const DReferenceTrajectory::swim_step_t* stepA = step_by_layer[layerA-1];
234  if(!stepA)continue;
235 
236  for(int layerB=layerA; layerB<=27; layerB++){
237  const DReferenceTrajectory::swim_step_t* stepB = step_by_layer[layerB-1];
238  if(!stepB)continue;
239 
240  // Correlations between A and B are due only to material between
241  // the first detector and the most upstream of A or B.
242  double sA = stepA->s;
243  double sB = stepB->s;
244  const DReferenceTrajectory::swim_step_t *step_end = sA<sB ? stepA:stepB;
245 
246  if(step0->s>step_end->s)continue; // Bullet proof
247 
248  double itheta02 = step_end->itheta02 - step0->itheta02;
249  double itheta02s = step_end->itheta02s - step0->itheta02s;
250  double itheta02s2 = step_end->itheta02s2 - step0->itheta02s2;
251  double sigmaAB = sA*sB*itheta02 -(sA+sB)*itheta02s + itheta02s2;
252 
253  cdc_cov_calc->Fill(layerA, layerB, sigmaAB);
254  cdc_cov_calc->Fill(layerB, layerA, sigmaAB);
255  }
256  }
257  }
258 
259  RootFillUnLock(this); //RELEASE ROOT FILL LOCK
260 
261  delete rt;
262 
263  return NOERROR;
264 }
265 
DApplication * dapp
jerror_t init(void)
Invoked via DEventProcessor virtual method.
const DCDCWire * wire
Definition: DCDCTrackHit.h:37
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)
Definition: GlueX.h:17
DetectorSystem_t system
particle type
Definition: DMCTrackHit.h:25
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
jerror_t erun(void)
Invoked via DEventProcessor virtual method.
#define _DBG_
Definition: HDEVIO.h:12
jerror_t brun(jana::JEventLoop *loop, int32_t runnumber)
void SetDRootGeom(const DRootGeom *RootGeom)
double sqrt(double)
double sin(double)
double Nevents
jerror_t fini(void)
Invoked via DEventProcessor virtual method.
int ring
Definition: DCDCWire.h:80
TDirectory * dir
Definition: bcal_hist_eff.C:31
jerror_t evnt(jana::JEventLoop *loop, uint64_t eventnumber)
Invoked via DEventProcessor virtual method.