Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DEventProcessor_trackanal.cc
Go to the documentation of this file.
1 // $Id$
2 //
3 // File: DEventProcessor_trackanal.cc
4 // Created: Wed Jul 14 17:12:56 EDT 2010
5 // Creator: zihlmann (on Linux chaos.jlab.org 2.6.18-164.2.1.el5 i686)
6 //
7 
8 // the local stuff
10 
11 // the general stuff
12 #include <iostream>
13 #include <iomanip>
14 using namespace std;
15 
16 // the cern root stuff
17 #include <TThread.h>
18 #include <TDirectoryFile.h>
19 #include <TLorentzVector.h>
20 
21 #include <TROOT.h>
22 #include <TFile.h>
23 
24 #include <JANA/JApplication.h>
25 #include <JANA/JEventLoop.h>
26 using namespace jana;
27 
28 // the DANA stuff
33 #include <TRACKING/DMCThrown.h>
34 #include <TRACKING/DMCTrackHit.h>
37 #include <FDC/DFDCHit.h>
38 #include <CDC/DCDCHit.h>
39 #include <FDC/DFDCPseudo.h>
40 #include <CDC/DCDCTrackHit.h>
41 #include <PID/DKinematicData.h>
42 
43 extern "C"{
44 void InitPlugin(JApplication *app){
45  InitJANAPlugin(app);
46  app->AddProcessor(new DEventProcessor_trackanal());
47 }
48 } // "C"
49 
50 #define DEBUG 0
51 
52 //------------------
53 // DEventProcessor_trackanal (Constructor)
54 //------------------
56 {
57 
58 }
59 
60 //------------------
61 // ~DEventProcessor_trackanal (Destructor)
62 //------------------
64 {
65 
66 }
67 
68 //------------------
69 // init
70 //------------------
72 {
73  // Create histograms here
74  // create Tree to hold TOF data
75 
76  ROOTFile = new TFile("trackanal.root", "recreate");
77  //ROOTFile->cd();
78 
79  TrackTree = new TTree("TrackTree", "Track raw data");
80 
81  TrackTree->Branch("EventNum", &EventNum, "EventNum/I");
82  TrackTree->Branch("MaxT", &MaxT, "MaxT/I");
83  TrackTree->Branch("MaxC", &MaxC, "MaxC/I");
84  TrackTree->Branch("MaxF", &MaxF, "MaxF/I");
85 
86  TrackTree->Branch("NTrThrown", &NTrThrown, "NTrThrown/I");
87  TrackTree->Branch("ThrownPType", ThrownPType, "ThrownPType[NTrThrown]/I");
88  TrackTree->Branch("ThrownPp", ThrownPp, "ThrownPp[NTrThrown]/F");
89  TrackTree->Branch("ThrownQ", ThrownQ, "ThrownQ[NTrThrown]/F");
90 
91  TrackTree->Branch("NTrCand", &NTrCand, "NTrCand/I");
92  TrackTree->Branch("TrCandQ", TrCandQ, "TrCandQ[NTrCand]/F");
93  TrackTree->Branch("TrCandP", TrCandP, "TrCandP[NTrCand]/F");
94  TrackTree->Branch("TrCandN", TrCandN, "TrCandN[NTrCand]/F");
95  TrackTree->Branch("TrCandM", TrCandM, "TrCandM[NTrCand]/F");
96 
97  // NTRCand: number of track candidates
98  // TrCandQ: charge of the track candidates
99  // TrCandP: particle momentum of the track candidates
100  // TrCandN: number of detrees of freedom of track candidates
101  // TrCandM: number of hits in track candidate with found match to TruthPoints
102 
103  TrackTree->Branch("NTrFit", &NTrFit, "NTrFit/I");
104  TrackTree->Branch("trlistcand", trlistcand, "trlistcand[NTrFit]/I");
105  TrackTree->Branch("trlistPtype", trlistPtype, "trlistPtype[NTrFit]/I");
106  TrackTree->Branch("trlistFOM", trlistFOM, "trlistFOM[NTrFit]/F");
107  TrackTree->Branch("trlistPp", trlistPp, "trlistPp[NTrFit]/F");
108  TrackTree->Branch("trlistchisq", trlistchisq, "trlistchisq[NTrFit]/F");
109 
110 
111  TrackTree->Branch("NTrCandHits", &NTrCandHits, "NTrCandHits/I");
112  TrackTree->Branch("nh", nh, "nh[NTrCandHits]/F");
113  TrackTree->Branch("ptypes", ptypes, "ptypes[NTrCandHits]/F");
114 
115  MaxT = MaxTrThrown;
116  MaxC = MaxTrCand;
117  MaxF = MaxTrFit;
118 
119  Int_t MaxArray = MaxF*MaxF;
120 
121  // initialize varialbes just for the fun of it
122  for (Int_t i=0;i<MaxTrThrown;i++){
123  ThrownPType[i] = 0;
124  ThrownPp[i] = 0.0;
125  ThrownQ[i] = 999.;
126  TrCandQ[i] = 999.;
127  TrCandP[i] = 0.0;
128  TrCandN[i] = 0.0;
129  }
130  for (Int_t i=0;i<MaxTrFit;i++){
131  trlistcand[i] = 0;
132  trlistPtype[i] = 0;
133  trlistFOM[i] = 0.0;
134  trlistPp[i] = 0.0;
135  }
136  for (Int_t i=0;i<MaxArray;i++){
137  nh[i] = 0;
138  ptypes[i] = 0;
139  }
140 
141  pthread_mutex_init(&mutex, NULL);
142 
143  return NOERROR;
144 }
145 
146 //------------------
147 // brun
148 //------------------
149 jerror_t DEventProcessor_trackanal::brun(JEventLoop *eventLoop, int32_t runnumber)
150 {
151  return NOERROR;
152 }
153 
154 //------------------
155 // evnt
156 //------------------
157 jerror_t DEventProcessor_trackanal::evnt(JEventLoop *loop, uint64_t eventnumber)
158 {
159 
160 
161  // Fill histograms here
162  vector<const DMCThrown*> trThrown;
163  loop->Get(trThrown);
164 
165  vector<const DTrackCandidate*> trCand;
166  loop->Get(trCand);
167 
168  vector<const DTrackTimeBased*> trFit;
169  loop->Get(trFit);
170 
171  int ntr = trFit.size();
172 
173  pthread_mutex_lock(&mutex);
174 
175  if (ntr>MaxTrFit)
176  ntr = MaxTrFit;
177 
178  NTrFit = ntr;
179 
180  for (int i=0;i<NTrFit;i++){
181  int cid = trFit[i]->candidateid;
182 
183  trlistcand[i] = cid;
184  trlistFOM[i] = trFit[i]->FOM;
185  trlistchisq[i] = trFit[i]->chisq;
186  trlistPp[i] = trFit[i]->pmag();
187  trlistPtype[i] = 0;
188  float mass = (float)trFit[i]->mass();
189  if (mass>.9){
190  trlistPtype[i] = 14;
191  } else{
192  if (trFit[i]->charge() < 0){
193  trlistPtype[i] = 9;
194  } else{
195  trlistPtype[i] = 8;
196  }
197  }
198 
199  }
200 
201  EventNum = eventnumber;
202  NTrThrown = trThrown.size();
203 
204  for (int i=0;i<NTrThrown;i++){
205  ThrownPType[i] = trThrown[i]->type;
206  ThrownPp[i] = trThrown[i]->pmag();
207  ThrownQ[i] = trThrown[i]->charge();
208  }
209 
210  ntr = trCand.size();
211 
212  if (ntr>MaxC){
213  NTrCand = MaxC;
214  } else {
215  NTrCand = ntr;
216  }
217 
218  NTrCandHits = NTrCand*MaxTrFit;
219 
220  // loop over all track candidates and find how many wire chamber hits contribute
221  // and from which particle track they stem.
222 
223  for (int j=0;j<MaxF;j++){
224  for (int i=0;i<MaxF;i++){
225  ptypes[j*MaxF+i] = 0.0 ;
226  nh[j*MaxF+i] = 0.0;
227  }
228  }
229 
230  int hitcounter;
231  for (int k=0;k<NTrCand;k++) {
232 
233  if (k>=MaxC){
234  continue;
235  }
236 
237  TrCandQ[k] = trCand[k]->charge();
238  TrCandP[k] = trCand[k]->pmag();
239  TrCandN[k] = 0.;
240  TrCandM[k] = 0.;
241 
242  vector<const DFDCPseudo*> fdcPHits;
243  trCand[k]->Get(fdcPHits);
244  int npfdc = fdcPHits.size();
245  TrCandN[k] += (Float_t)npfdc;
246 
247  hitcounter = 0;
248  for (int j=0;j<npfdc;j++){
249 
250  // for each FDC Pseudo hit get associated object DMCTrackHit
251  vector<const DMCTrackHit*> mcTrackHits;
252  fdcPHits[j]->Get(mcTrackHits);
253  Int_t asize = mcTrackHits.size();
254 
255  for (int i=0;i<asize;i++) {
256  int tr = mcTrackHits[i]->track; // track number if >NTrThrown scondary
257 
258  if (tr > NTrThrown){
259  tr = 0; // any track other than thrown is set as zero
260  }
261 
262  // this means nh contains only hits from initially thrown particles.
263  nh[k*MaxF + tr] += 1.0;
264  hitcounter++;
265  int pt = mcTrackHits[i]->ptype;
266  if (pt>14){
267  pt = 0;
268  }
269  ptypes[k*MaxF + pt] += 1.0;
270  }
271  TrCandM[k] += (Float_t) asize;
272  }
273 
274 
275  vector<const DCDCTrackHit*> cdcTrackHits;
276  trCand[k]->Get(cdcTrackHits);
277  int nhitscdc = cdcTrackHits.size();
278  TrCandN[k] += (Float_t)nhitscdc;
279 
280  // loop over cdc hits used for this track candiate
281  for (int j=0;j<nhitscdc;j++){
282 
283  // for each hit get the associated hit object DMCTrackHit
284  vector<const DMCTrackHit*> cdcHits;
285  cdcTrackHits[j]->Get(cdcHits);
286 
287  Int_t asize = cdcHits.size();
288 
289  for (int n=0;n<asize;n++) {
290  int tr = cdcHits[n]->track;
291 
292  if (tr > NTrThrown){
293  tr = 0;
294  }
295  nh[k*MaxF + tr] += 1.;
296  hitcounter++;
297  int pt = cdcHits[n]->ptype;
298  if (pt>14){
299  pt = 0;
300  }
301  ptypes[k*MaxF + pt] += 1.;
302  }
303 
304  TrCandM[k] += (Float_t)asize;
305  }
306 
307  }
308 
309  if (DEBUG){
310  cout<<EventNum << " " << NTrThrown << " " << NTrCand << " " << NTrFit
311  << " " << NTrCandHits << endl;
312  }
313 
314  TrackTree->Fill();
315 
316  pthread_mutex_unlock(&mutex);
317 
318  return NOERROR;
319 }
320 
321 //------------------
322 // erun
323 //------------------
325 {
326  // Any final calculations on histograms (like dividing them)
327  // should be done here. This may get called more than once.
328  return NOERROR;
329 }
330 
331 //------------------
332 // fini
333 //------------------
335 {
336  // Called at very end. This will be called only once
337  pthread_mutex_lock(&mutex);
338 
339  //ROOTFile->cd();
340  ROOTFile->Write();
341  ROOTFile->Close();
342  //delete ROOTFile;
343  cout<<endl<<"Close ROOT file"<<endl;
344 
345  pthread_mutex_unlock(&mutex);
346 
347  return NOERROR;
348 }
349 
#define MaxTrCand
InitPlugin_t InitPlugin
jerror_t brun(jana::JEventLoop *eventLoop, int32_t runnumber)
#define DEBUG
#define MaxTrThrown
jerror_t evnt(jana::JEventLoop *eventLoop, uint64_t eventnumber)
#define MaxTrFit