Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DEventProcessor_cdc_hists.cc
Go to the documentation of this file.
1 // $Id: $
2 //
3 // File: DEventProcessor_cdc_hists.cc
4 //
5 
6 #include <iostream>
7 #include <cmath>
8 using namespace std;
9 
10 #include <TThread.h>
11 
12 #include <JANA/JEventLoop.h>
13 using namespace jana;
14 
16 
17 #include <DANA/DApplication.h>
18 #include <TRACKING/DMCThrown.h>
19 #include <TRACKING/DMCTrackHit.h>
20 #include <TRACKING/DMCThrown.h>
22 #include <CDC/DCDCHit.h>
23 #include <CDC/DCDCTrackHit.h>
24 #include <FDC/DFDCHit.h>
25 #include <DVector2.h>
26 
27 
28 // Routine used to create our DEventProcessor
29 extern "C"{
30 void InitPlugin(JApplication *app){
31  InitJANAPlugin(app);
32  app->AddProcessor(new DEventProcessor_cdc_hists());
33 }
34 } // "C"
35 
36 
37 //------------------
38 // DEventProcessor_cdc_hists
39 //------------------
41 {
42  cdc_ptr = &cdc;
43  cdchit_ptr = &cdchit;
44 
45  pthread_mutex_init(&mutex, NULL);
46 }
47 
48 //------------------
49 // ~DEventProcessor_cdc_hists
50 //------------------
52 {
53 }
54 
55 //------------------
56 // init
57 //------------------
59 {
60  // open ROOT file (if needed)
61  //if(ROOTfile != NULL) ROOTfile->cd();
62 
63  // Create THROWN directory
64  TDirectory *dir = new TDirectoryFile("CDC","CDC");
65  dir->cd();
66 
67  // Create Tree
68  cdctree = new TTree("cdc","CDC Truth points");
69  cdchittree = new TTree("cdchit","CDC Hits");
70  cdcbranch = cdctree->Branch("T","CDC_branch",&cdc_ptr);
71  cdchitbranch = cdchittree->Branch("H","CDChit_branch",&cdchit_ptr);
72 
73  idEdx = new TH1D("idEdx","Integrated dE/dx in CDC", 10000, 0.0, 1.0E-3);
74  idEdx->SetXTitle("dE/dx (GeV/cm)");
75  idEdx_vs_p = new TH2D("idEdx_vs_p","Integrated dE/dx vs. momentum in CDC", 100, 0.0, 1.0, 1000, 0.0, 1.0E1);
76  idEdx->SetXTitle("momentum (GeV/c)");
77  idEdx->SetYTitle("dE/dx (MeV/g^{-1}cm^{2})");
78 
79  // Go back up to the parent directory
80  dir->cd("../");
81 
82 
83  return NOERROR;
84 }
85 
86 //------------------
87 // brun
88 //------------------
89 jerror_t DEventProcessor_cdc_hists::brun(JEventLoop *eventLoop, int32_t runnumber)
90 {
91  DApplication *dapp = dynamic_cast<DApplication*>(app);
92  LockState();
93  bfield = dapp->GetBfield(runnumber);
94  UnlockState();
95 
96  return NOERROR;
97 }
98 
99 //------------------
100 // erun
101 //------------------
103 {
104 
105  return NOERROR;
106 }
107 
108 //------------------
109 // fini
110 //------------------
112 {
113  return NOERROR;
114 }
115 
116 //------------------
117 // evnt
118 //------------------
119 jerror_t DEventProcessor_cdc_hists::evnt(JEventLoop *loop, uint64_t eventnumber)
120 {
121  vector<const DMCTrackHit*> mctrackhits;
122  vector<const DCDCHit*> cdchits;
123  vector<const DCDCTrackHit*> cdctrackhits;
124  vector<const DFDCHit*> fdchits;
125  vector<const DMCThrown*> mcthrowns;
126  loop->Get(mctrackhits);
127  loop->Get(cdchits);
128  loop->Get(cdctrackhits);
129  loop->Get(fdchits);
130  loop->Get(mcthrowns);
131 
132  // Find number of wire hits in FDC
133  int Nfdc_wire_hits = 0;
134  for(unsigned int i=0; i<fdchits.size(); i++)if(fdchits[i]->type==0)Nfdc_wire_hits++;
135 
136  // Swim reference trajectory for first thrown track
137  DReferenceTrajectory* rt = new DReferenceTrajectory(bfield);
138  const DMCThrown *mcthrown = mcthrowns.size()>0 ? mcthrowns[0]:NULL;
139  if(mcthrown){
140  rt->Swim(mcthrown->position(), mcthrown->momentum(), mcthrown->charge());
141  }
142 
143  // Although we are only filling objects local to this plugin, TTree::Fill() periodically writes to file: Global ROOT lock
144  japp->RootWriteLock(); //ACQUIRE ROOT LOCK
145 
146  // Loop over all truth hits, ignoring all but CDC hits
147  for(unsigned int i=0; i<mctrackhits.size(); i++){
148  const DMCTrackHit *mctrackhit = mctrackhits[i];
149  if(mctrackhit->system != SYS_CDC)continue;
150 
151  double r = mctrackhit->r;
152  double phi = mctrackhit->phi;
153  double x = r*cos(phi);
154  double y = r*sin(phi);
155  cdc.pos_truth.SetXYZ(x, y, mctrackhit->z);
156 
157  cdctree->Fill();
158  }
159 
160  // Loop over all real hits
161  double Q = 0.0;
162  double dxtot = 0.0;
163  for(unsigned int i=0; i<cdchits.size(); i++){
164  const DCDCHit *hit = cdchits[i];
165  const DCDCWire *wire = (cdchits.size() == cdctrackhits.size()) ? cdctrackhits[i]->wire:NULL;
166 
167  cdchit.ring = hit->ring;
168  cdchit.straw = hit->straw;
169  cdchit.q = hit->q;
170  cdchit.dx = 0.0;
171  cdchit.t = hit->t;
172  cdchit.pthrown = mcthrowns.size()>0 ? mcthrowns[0]->momentum().Mag():-1000.0;
173  cdchit.ncdchits = (int)cdchits.size();
174  cdchit.ntothits = (int)cdchits.size() + Nfdc_wire_hits;
175 
176  if(mcthrown && wire){
177  cdchit.dx = rt->Straw_dx(wire, 0.8);
178  }
179 
180  if(cdchit.dx!=0.0){
181  Q += cdchit.q;
182  dxtot += cdchit.dx;
183  }
184 
185  // Find residual of hit with "thrown" track (if present)
186  if(mcthrown && wire){
187  double s;
188  double doca = rt->DistToRT(wire, &s);
189  double mass = 0.13957; // assume pion
190  double beta = 1.0/sqrt(1.0 + pow(mass/mcthrown->momentum().Mag(), 2.0))*2.998E10;
191  double tof = s/beta/1.0E-9; // in ns
192  double dist = (cdchit.t-tof)*55.0E-4;
193  cdchit.resi_thrown = doca-dist;
194  }else{
195  cdchit.resi_thrown = 0.0;
196  }
197 
198  cdchittree->Fill();
199  }
200 
201  // Fill dE/dx histograms
202  if(((Nfdc_wire_hits+cdchits.size()) >= 10) && (cdchits.size()>=5)){
203  if(dxtot>0.0){
204  idEdx->Fill(Q/dxtot);
205  if(mcthrown){
206  // The CDC gas is 85% Ar, 15% CO2 by mass.
207  // density of Ar: 1.977E-3 g/cm^3
208  // density of CO2: 1.66E-3 g/cm^3
209  double density = 0.85*1.66E-3 + 0.15*1.977E-3;
210  idEdx_vs_p->Fill(mcthrown->momentum().Mag(), Q/dxtot*1000.0/density);
211  }
212  }
213  }
214 
215  japp->RootUnLock(); //RELEASE ROOT LOCK
216 
217  delete rt;
218 
219  return NOERROR;
220 }
jerror_t brun(JEventLoop *eventLoop, int32_t runnumber)
DApplication * dapp
static double E1[100]
double Straw_dx(const DCoordinateSystem *wire, double radius) const
jerror_t evnt(JEventLoop *loop, uint64_t eventnumber)
Invoked via DEventProcessor virtual method.
Double_t x[NCHANNELS]
Definition: st_tw_resols.C:39
#define y
DMagneticFieldMap * GetBfield(unsigned int run_number=1)
const DVector3 & position(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)
jerror_t fini(void)
Invoked via DEventProcessor virtual method.
jerror_t init(void)
Invoked via DEventProcessor virtual method.
Definition: GlueX.h:17
JApplication * japp
DetectorSystem_t system
particle type
Definition: DMCTrackHit.h:25
float t
Definition: DCDCHit.h:22
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.
int ring
Definition: DCDCHit.h:18
double charge(void) const
float q
Definition: DCDCHit.h:20
double sqrt(double)
double sin(double)
const DVector3 & momentum(void) const
int ring
Definition: DCDCWire.h:80
TDirectory * dir
Definition: bcal_hist_eff.C:31
int straw
Definition: DCDCHit.h:19