Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DEventProcessor_pidstudies_tree.cc
Go to the documentation of this file.
1 // $Id$
2 //
3 // File: DEventProcessor_pidstudies_tree.cc
4 // Created: Thu Sep 28 11:38:03 EDT 2011
5 // Creator: pmatt (on Darwin swire-b241.jlab.org 8.4.0 powerpc)
6 //
7 
9 #include "particleType.h"
10 
11 // The executable should define the ROOTfile global variable. It will
12 // be automatically linked when dlopen is called.
13 extern TFile *ROOTfile;
14 
15 // Routine used to create our DEventProcessor
16 extern "C"{
17 void InitPlugin(JApplication *app){
18  InitJANAPlugin(app);
19  app->AddProcessor(new DEventProcessor_pidstudies_tree());
20 }
21 } // "C"
22 
24  return (locTrackMatch1->dFOM > locTrackMatch2->dFOM);
25 };
26 
27 //------------------
28 // init
29 //------------------
31 {
32 
34  dPluginTree_MCReconstructionStatuses = new TTree("dPluginTree_MCReconstructionStatuses", "MC Reconstruction Statuses");
35  dPluginTree_MCReconstructionStatuses->Branch("dPluginBranch_MCReconstructionStatuses", "MCReconstructionStatuses", &dMCReconstructionStatuses);
36 
37  return NOERROR;
38 }
39 
40 //------------------
41 // brun
42 //------------------
43 jerror_t DEventProcessor_pidstudies_tree::brun(JEventLoop *eventLoop, int32_t runnumber)
44 {
45  return NOERROR;
46 }
47 
48 //------------------
49 // evnt
50 //------------------
51 jerror_t DEventProcessor_pidstudies_tree::evnt(JEventLoop *loop, uint64_t eventnumber)
52 {
53  unsigned int loc_i, loc_j;
54  DLorentzVector locThrownFourMomentum, locThrownSpacetimeVertex;
55  DLorentzVector locReconstructedFourMomentum, locReconstructedSpacetimeVertex;
56  const DChargedTrackHypothesis *locChargedTrackHypothesis;
57  MCReconstructionStatus *locMCReconstructionStatus;
58  ReconstructedHypothesis *locReconstructedHypothesis;
59  map<const DMCThrown*, const DChargedTrack*> locTrackMap;
60  map<const DMCThrown*, const DChargedTrack*>::iterator locTrackMapIterator;
61 
62  vector<const DMCThrown*> locMCThrownVector;
63  const DMCThrown *locMCThrown;
64  loop->Get(locMCThrownVector);
65 
66  vector<const DChargedTrack*> locChargedTrackVector;
67  const DChargedTrack *locChargedTrack;
68  loop->Get(locChargedTrackVector);
69 
70  // Find track matches
71  //Assume reconstructed track p/theta not much different between hypotheses: just choose the first hypothesis
72  double locFOM, locMinimumFOM = 0.1;
73  plugin_trackmatch_t *locTrackMatch;
74  dTrackMatches.resize(0);
75  for(loc_i = 0; loc_i < locMCThrownVector.size(); loc_i++){
76  locMCThrown = locMCThrownVector[loc_i];
77  for(loc_j = 0; loc_j < locChargedTrackVector.size(); loc_j++){
78  locChargedTrack = locChargedTrackVector[loc_j];
79  locFOM = Calc_MatchFOM(locMCThrown->momentum(), locChargedTrack->dChargedTrackHypotheses[0]->dTrackTimeBased->momentum());
80  if(locFOM > locMinimumFOM){
81  locTrackMatch = new plugin_trackmatch_t();
82  locTrackMatch->dMCThrown = locMCThrown;
83  locTrackMatch->dChargedTrack = locChargedTrack;
84  locTrackMatch->dFOM = locFOM;
85  dTrackMatches.push_back(locTrackMatch);
86  }
87  }
88  }
89  //sort track matches by fom
90  sort(dTrackMatches.begin(), dTrackMatches.end(), Compare_TrackMatches);
91 
92  //fill track map in order of best matches
93  while(dTrackMatches.size() > 0){
94  locMCThrown = dTrackMatches[0]->dMCThrown;
95  locChargedTrack = dTrackMatches[0]->dChargedTrack;
96  locTrackMap[locMCThrown] = locChargedTrack;
97  //erase all matches that include these objects
98  for(loc_i = dTrackMatches.size() - 1; loc_i >= 0; loc_i--){
99  locTrackMatch = dTrackMatches[loc_i];
100  if(locTrackMatch->dMCThrown == locMCThrown)
101  dTrackMatches.erase(dTrackMatches.begin() + loc_i);
102  else if(locTrackMatch->dChargedTrack == locChargedTrack)
103  dTrackMatches.erase(dTrackMatches.begin() + loc_i);
104  if(loc_i == 0) //unsigned int!
105  break;
106  }
107  }
108 
109  //fill information into trees
110  // Although we are only filling objects local to this plugin, TTree::Fill() periodically writes to file: Global ROOT lock
111  japp->RootWriteLock(); //ACQUIRE ROOT LOCK
112 
114  for(loc_i = 0; loc_i < locMCThrownVector.size(); loc_i++){
115  locMCThrown = locMCThrownVector[loc_i];
116  locMCReconstructionStatus = new MCReconstructionStatus();
117  //set thrown track information
118  locMCReconstructionStatus->dThrownID = Particle_t(locMCThrown->type);
119  locThrownFourMomentum.SetVect(locMCThrown->momentum());
120  locThrownFourMomentum.SetT(locMCThrown->energy());
121  locMCReconstructionStatus->dThrownFourMomentum = locThrownFourMomentum;
122  locThrownSpacetimeVertex.SetVect(locMCThrown->position());
123  locThrownSpacetimeVertex.SetT(locMCThrown->t0());
124  locMCReconstructionStatus->dThrownSpacetimeVertex = locThrownSpacetimeVertex;
125  //see if any match to charged particles
126  locChargedTrack = NULL;
127  for(locTrackMapIterator = locTrackMap.begin(); locTrackMapIterator != locTrackMap.end(); locTrackMapIterator++){
128  if(locMCThrown == (*locTrackMapIterator).first){
129  locChargedTrack = (*locTrackMapIterator).second;
130  break;
131  }
132  }
133  if(locChargedTrack == NULL){
134  //set reconstructed hypothesis information
135  dMCReconstructionStatuses->dMCReconstructionStatusVector.push_back(locMCReconstructionStatus);
136  continue;
137  }
138 
139  //set reconstructed hypothesis information
140  for(loc_i = 0; loc_i < locChargedTrack->dChargedTrackHypotheses.size(); loc_i++){
141  locChargedTrackHypothesis = locChargedTrack->dChargedTrackHypotheses[loc_i];
142  locReconstructedHypothesis = new ReconstructedHypothesis();
143 
144  locReconstructedHypothesis->dPID = locChargedTrackHypothesis->dPID;
145  locReconstructedFourMomentum.SetVect(locChargedTrackHypothesis->dTrackTimeBased->momentum());
146  locReconstructedFourMomentum.SetT(locChargedTrackHypothesis->dTrackTimeBased->energy());
147  locReconstructedSpacetimeVertex.SetVect(locChargedTrackHypothesis->dTrackTimeBased->position());
148  locReconstructedSpacetimeVertex.SetT(locChargedTrackHypothesis->dTrackTimeBased->t0());
149  locReconstructedHypothesis->dFourMomentum = locReconstructedFourMomentum;
150  locReconstructedHypothesis->dSpacetimeVertex = locReconstructedSpacetimeVertex;
151 
152  locReconstructedHypothesis->dChiSq_Overall = locChargedTrackHypothesis->dChiSq;
153  locReconstructedHypothesis->dNDF_Overall = locChargedTrackHypothesis->dNDF;
154 
155  locReconstructedHypothesis->dChiSq_Tracking = locChargedTrackHypothesis->dTrackTimeBased->chisq;
156  locReconstructedHypothesis->dNDF_Tracking = locChargedTrackHypothesis->dTrackTimeBased->Ndof;
157 
158  locReconstructedHypothesis->dChiSq_DCdEdx = locChargedTrackHypothesis->dTrackTimeBased->chi2_dedx;
159  locReconstructedHypothesis->dNDF_DCdEdx = 1;
160 
161  locReconstructedHypothesis->dChiSq_Timing = locChargedTrackHypothesis->dChiSq_Timing;
162  locReconstructedHypothesis->dNDF_Timing = locChargedTrackHypothesis->dNDF_Timing;
163 
164  locReconstructedHypothesis->dChiSq_TOFdEdx = 0.0;
165  locReconstructedHypothesis->dNDF_TOFdEdx = 0;
166 
167  locReconstructedHypothesis->dChiSq_BCALdEdx = 0.0;
168  locReconstructedHypothesis->dNDF_BCALdEdx = 0;
169 
170  locMCReconstructionStatus->dReconstructedHypothesisVector.push_back(locReconstructedHypothesis);
171  }
172 
173  //set reconstructed hypothesis information
174  dMCReconstructionStatuses->dMCReconstructionStatusVector.push_back(locMCReconstructionStatus);
175  }
177 
178  japp->RootUnLock(); //RELEASE ROOT LOCK
179 
180  return NOERROR;
181 }
182 
183 //------------------
184 // erun
185 //------------------
187 {
188  // Any final calculations on histograms (like dividing them)
189  // should be done here. This may get called more than once.
190  return NOERROR;
191 }
192 
193 //------------------
194 // fini
195 //------------------
197 {
198  return NOERROR;
199 }
200 
201 //Copied from DEventProcessor_phys_tree.cc
202 double DEventProcessor_pidstudies_tree::Calc_MatchFOM(const DVector3& locMomentum_Track1, const DVector3& locMomentum_Track2) const
203 {
204  // This is a kind of brain-dead algorithm. It wants to use both the
205  // momentum direction and magnitude to determine the FOM. For the
206  // magnitude, we use the curvature which becomes close to zero for
207  // high momentum tracks (good because a 4GeV/c and an 8GeV/c track
208  // both look more or less like straight lines).
209  //
210  // For the direction, we just use the relative angle between the
211  // two tracks.
212  //
213  // For both, we take a reciprocal so that the closer the match,
214  // the higher the FOM. We take a product of the 2 FOM components
215  // so that both components must have a high value in order for the
216  // total FOM to be large.
217 
218  double epsilon = 1.0E-6; // prevent reciprocals from resulting in infinity
219 
220  double curature_a = 1.0/locMomentum_Track1.Mag();
221  double curature_b = 1.0/locMomentum_Track1.Mag();
222  double curvature_diff = fabs(curature_a - curature_b);
223  double curvature_fom = 1.0/(curvature_diff + epsilon);
224 
225  double theta_rel = fabs(locMomentum_Track1.Angle(locMomentum_Track2));
226  double theta_fom = 1.0/(theta_rel + epsilon);
227 
228  double fom = curvature_fom*theta_fom;
229 
230  return fom;
231 }
232 
TFile * ROOTfile
TVector3 DVector3
Definition: DVector3.h:14
bool Compare_TrackMatches(DEventProcessor_pidstudies_tree::plugin_trackmatch_t *locTrackMatch1, DEventProcessor_pidstudies_tree::plugin_trackmatch_t *locTrackMatch2)
double energy(void) const
jerror_t brun(JEventLoop *eventLoop, int32_t runnumber)
Called everytime a new run number is detected.
double Calc_MatchFOM(const DVector3 &locMomentum_Track1, const DVector3 &locMomentum_Track2) const
const DVector3 & position(void) const
jerror_t erun(void)
Called everytime run number changes, provided brun has been called.
TLorentzVector DLorentzVector
DLorentzVector dThrownSpacetimeVertex
jerror_t fini(void)
Called after last event of last event source has been processed.
JApplication * japp
vector< ReconstructedHypothesis * > dReconstructedHypothesisVector
MCReconstructionStatuses * dMCReconstructionStatuses
InitPlugin_t InitPlugin
jerror_t init(void)
Called once at program start.
jerror_t evnt(JEventLoop *eventLoop, uint64_t eventnumber)
Called every event.
vector< MCReconstructionStatus * > dMCReconstructionStatusVector
const DVector3 & momentum(void) const
int type
GEANT particle ID.
Definition: DMCThrown.h:20
vector< const DChargedTrackHypothesis * > dChargedTrackHypotheses
Definition: DChargedTrack.h:28
Particle_t
Definition: particleType.h:12
vector< plugin_trackmatch_t * > dTrackMatches