Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
JEventProcessor_pi0bcalskim.cc
Go to the documentation of this file.
1 // $Id$
2 //
3 // File: JEventProcessor_pi0bcalskim.cc
4 // Created: Mon Dec 1 14:57:11 EST 2014 (copied structure from pi0fcalskim plugin)
5 // Creator: wmcginle (on Linux ifarm1101 2.6.32-220.7.1.el6.x86_64 x86_64)
6 //
7 
8 #include <math.h>
9 #include <TLorentzVector.h>
10 #include <vector>
11 #include <deque>
12 #include <string>
13 #include <iostream>
14 #include <algorithm>
15 #include <stdio.h>
16 #include <stdlib.h>
18 
19 
20 #include "TRACKING/DMCThrown.h"
21 // Routine used to create our JEventProcessor
22 #include "PID/DVertex.h"
23 #include "DANA/DApplication.h"
24 #include "JANA/JApplication.h"
25 #include "JANA/JFactory.h"
26 #include "BCAL/DBCALShower.h"
27 #include "RF/DRFTime.h"
28 #include "PID/DEventRFBunch.h"
29 
30 #include "DLorentzVector.h"
31 #include "TTree.h"
32 #include "units.h"
34 
35 extern "C"{
36  void InitPlugin(JApplication *app){
37  InitJANAPlugin(app);
38  app->AddProcessor(new JEventProcessor_pi0bcalskim());
39  }
40 } // "C"
41 
42 
43 //------------------
44 // JEventProcessor_pi0bcalskim (Constructor)
45 //------------------
47 {
48 
49  MIN_SH1_E = 0.2;
50  MIN_SH2_E = 0.2;
51 
52  WRITE_EVIO = 1;
53 
54  gPARMS->SetDefaultParameter( "PI0BCALSKIM:WRITE_EVIO", WRITE_EVIO );
55  gPARMS->SetDefaultParameter("PI0BCALSKIM:MIN_SH1_E" , MIN_SH1_E );
56  gPARMS->SetDefaultParameter("PI0BCALSKIM:MIN_SH2_E" , MIN_SH2_E );
57 
58  num_epics_events = 0;
59 
60 }
61 
62 //------------------
63 // ~JEventProcessor_pi0bcalskim (Destructor)
64 //------------------
66 {
67 
68 }
69 
70 //------------------
71 // init
72 //------------------
74 {
75  //if( ! WRITE_EVIO) cerr << " output isnt working " << endl;
76 
77  return NOERROR;
78 }
79 
80 //------------------
81 // brun
82 //------------------
83 jerror_t JEventProcessor_pi0bcalskim::brun(JEventLoop *eventLoop, int32_t runnumber)
84 {
85  /* Example
86  // only write out BCAL data for these events
87  const DEventWriterEVIO* locEventWriterEVIO = NULL;
88  eventLoop->GetSingle(locEventWriterEVIO);
89 
90  if(locEventWriterEVIO) {
91  locEventWriterEVIO->SetDetectorsToWriteOut("BCAL","pi0bcalskim");
92  }
93  */
94 
95 
96  return NOERROR;
97 }
98 
99 //------------------
100 // evnt
101 //------------------
102 jerror_t JEventProcessor_pi0bcalskim::evnt(JEventLoop *loop, uint64_t eventnumber)
103 {
104  vector< const DBCALShower* > locBCALShowers;
105  loop->Get(locBCALShowers);
106  vector< const DTrackTimeBased*> locTrackTimeBased;
107  loop->Get(locTrackTimeBased);
108  vector<const DVertex*> kinfitVertex;
109  loop->Get(kinfitVertex);
110 
111  const DEventWriterEVIO* locEventWriterEVIO = NULL;
112  loop->GetSingle(locEventWriterEVIO);
113 
114  vector< const JObject* > locObjectsToSave;
115 
116  // always write out BOR events
117  if(loop->GetJEvent().GetStatusBit(kSTATUS_BOR_EVENT)) {
118  //jout << "Found BOR!" << endl;
119  locEventWriterEVIO->Write_EVIOEvent( loop, "pi0bcalskim" );
120  return NOERROR;
121  }
122 
123  // write out the first few EPICS events to save run number & other meta info
124  if(loop->GetJEvent().GetStatusBit(kSTATUS_EPICS_EVENT) && (num_epics_events<5)) {
125  //jout << "Found EPICS!" << endl;
126  locEventWriterEVIO->Write_EVIOEvent( loop, "pi0bcalskim" );
128  return NOERROR;
129  }
130 
131  if(locBCALShowers.size() < 2 ) return NOERROR;
132 
133  vector<const DTrackFitter*>fitters;
134  loop->Get(fitters);
135 
136  if(fitters.size()<1){
137  _DBG_<<"Unable to get a DTrackFinder object!"<<endl;
138  return RESOURCE_UNAVAILABLE;
139  }
140 
141  const DTrackFitter *fitter = fitters[0];
142 
143  bool Candidate = false;
144  double sh1_E, sh2_E, inv_mass, kinfitVertexZ=0.0, kinfitVertexX=0.0, kinfitVertexY=0.0;
145  vector <const DBCALShower *> matchedShowers;
146  DVector3 mypos(0.0,0.0,0.0);
147  for(unsigned int i = 0 ; i < locTrackTimeBased.size() ; ++i)
148  {
149  vector<DTrackFitter::Extrapolation_t>extrapolations=locTrackTimeBased[i]->extrapolations.at(SYS_BCAL);
150  if (extrapolations.size()>0){
151  for(unsigned int j = 0 ; j < locBCALShowers.size() ; ++j)
152  {
153  double x = locBCALShowers[j]->x;
154  double y = locBCALShowers[j]->y;
155  double z = locBCALShowers[j]->z;
156  // double E = locBCALShowers[j]->E;
157  DVector3 pos_bcal(x,y,z);
158  double R = pos_bcal.Perp();
159  // double phi = pos_bcal.Phi();
160  // double L2 = 0.81*2.54+65.0;
161  // double L3 = L2 + 0.81*2.54*2;
162  // double L4 = L3 + 0.81*2.54*3;
163  // double L5 = L4 + 0.97*2.54*4;
164  if (fitter->ExtrapolateToRadius(R,extrapolations,mypos)){
165 
166  double dPhi =mypos.Phi()-pos_bcal.Phi();
167  if (dPhi< -M_PI) dPhi+=2.*M_PI;
168  if (dPhi> M_PI) dPhi-=2.*M_PI;
169  double dZ = TMath::Abs(mypos.Z() - z);
170  if(dZ < 30.0 && fabs(dPhi) < 0.18) {
171  matchedShowers.push_back(locBCALShowers[j]);
172  }
173  }
174  }
175  }
176  }
177 
178 
179  //japp->RootWriteLock();
180 
181  vector<const DEventRFBunch*> locEventRFBunches;
182  loop->Get(locEventRFBunches);
183  if(locEventRFBunches.size() > 0) {
184  locObjectsToSave.push_back(static_cast<const JObject *>(locEventRFBunches[0]));
185  }
186 
187  for (unsigned int i = 0 ; i < kinfitVertex.size(); i++)
188  {
189  // if the vertex information exists, save it as well
190  if(i == 0)
191  locObjectsToSave.push_back(static_cast<const JObject *>(kinfitVertex[0]));
192 
193  kinfitVertexX = kinfitVertex[i]->dSpacetimeVertex.X();
194  kinfitVertexY = kinfitVertex[i]->dSpacetimeVertex.Y();
195  kinfitVertexZ = kinfitVertex[i]->dSpacetimeVertex.Z();
196  //kinfitVertexT = kinfitVertex[i]->dSpacetimeVertex.T();
197  }
198 
199  for(unsigned int i=0; i<locBCALShowers.size() ; i++)
200  {
201  if (find(matchedShowers.begin(), matchedShowers.end(),locBCALShowers[i]) != matchedShowers.end()) continue;
202  sh1_E = locBCALShowers[i]->E_raw;
203  const DBCALShower *s1 = locBCALShowers[i];
204  double sh1_x = s1->x - kinfitVertexX ;
205  double sh1_y = s1->y - kinfitVertexY ;
206  double sh1_z = s1->z - kinfitVertexZ ;
207  double sh1_R = sqrt(sh1_x*sh1_x+sh1_y*sh1_y+sh1_z*sh1_z);
208  TLorentzVector sh1_p(sh1_E*sh1_x/sh1_R,sh1_E*sh1_y/sh1_R,sh1_E*sh1_z/sh1_R,sh1_E);
209  for(unsigned int j = i+1 ; j < locBCALShowers.size() ; j++)
210  {
211  if (find(matchedShowers.begin(), matchedShowers.end(),locBCALShowers[j]) != matchedShowers.end()) continue;
212  const DBCALShower *s2 = locBCALShowers[j];
213  sh2_E = locBCALShowers[j]->E_raw;
214  double sh2_x = s2->x - kinfitVertexX;
215  double sh2_y = s2->y - kinfitVertexY;
216  double sh2_z = s2->z - kinfitVertexZ;
217  double sh2_R = sqrt(sh2_x*sh2_x + sh2_y*sh2_y + sh2_z*sh2_z);
218  TLorentzVector sh2_p(sh2_E*sh2_x/sh2_R,sh2_E*sh2_y/sh2_R,sh2_E*sh2_z/sh2_R,sh2_E);
219  TLorentzVector ptot = sh1_p+sh2_p;
220  inv_mass = ptot.M();
221  Candidate |= ( (sh2_E>0.4) && (inv_mass<0.25) && (inv_mass>0.05));
222  if((sh2_E>0.4) && (inv_mass<0.25) && (inv_mass>0.05)) {
223  if(find(locObjectsToSave.begin(), locObjectsToSave.end(), locBCALShowers[i]) == locObjectsToSave.end())
224  locObjectsToSave.push_back(static_cast<const JObject *>(locBCALShowers[i]));
225  if(find(locObjectsToSave.begin(), locObjectsToSave.end(), locBCALShowers[j]) == locObjectsToSave.end())
226  locObjectsToSave.push_back(static_cast<const JObject *>(locBCALShowers[j]));
227  }
228  }
229  }
230  if(Candidate){
231  if( WRITE_EVIO ) {
232  // cout << " inv mass = " << inv_mass << " sh1 E = " << sh1_E << " sh2 E = " << sh2_E << " event num = " << eventnumber << endl;
233  //cout << "writing out " << eventnumber << endl;
234  locEventWriterEVIO->Write_EVIOEvent( loop, "pi0bcalskim", locObjectsToSave );
235  }
236  }
237 
238 
239  //japp->RootUnLock();
240 
241 
242 
243 
244 
245  return NOERROR;
246 }
247 
248 //------------------
249 // erun
250 //------------------
252 {
253  // This is called whenever the run number changes, before it is
254  // changed to give you a chance to clean up before processing
255  // events from the next run number.
256  return NOERROR;
257 }
258 
259 //------------------
260 // Fin
261 //------------------
263 {
264  // Called before program exit after event processing is finished.
265  return NOERROR;
266 }
267 
jerror_t init(void)
Called once at program start.
bool ExtrapolateToRadius(double R, const vector< Extrapolation_t > &extraps, DVector3 &pos, DVector3 &mom, double &t, double &s) const
jerror_t brun(jana::JEventLoop *eventLoop, int32_t runnumber)
Called everytime a new run number is detected.
jerror_t erun(void)
Called everytime run number changes, provided brun has been called.
The DTrackFitter class is a base class for different charged track fitting algorithms. It does not actually fit the track itself, but provides the interface and some common support features most algorthims will need to implement.
Definition: DTrackFitter.h:61
TVector3 DVector3
Definition: DVector3.h:14
Double_t x[NCHANNELS]
Definition: st_tw_resols.C:39
#define y
Definition: GlueX.h:19
InitPlugin_t InitPlugin
jerror_t fini(void)
Called after last event of last event source has been processed.
#define _DBG_
Definition: HDEVIO.h:12
jerror_t evnt(jana::JEventLoop *eventLoop, uint64_t eventnumber)
Called every event.
double sqrt(double)
bool Write_EVIOEvent(JEventLoop *locEventLoop, string locOutputFileNameSubString) const
const DTrackFitter * fitter