Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
JEventProcessor_ST_online_Tresolution.cc
Go to the documentation of this file.
1 // $Id$
2 //
3 // File: JEventProcessor_ST_online_Tresolution.cc
4 // Created: Fri Jan 8 09:07:34 EST 2016
5 // Creator: mkamel (on Linux ifarm1401 2.6.32-431.el6.x86_64 x86_64)
6 //
7 
9 #include "TRIGGER/DTrigger.h"
10 using namespace jana;
11 
12 
13 // Routine used to create our JEventProcessor
14 #include <JANA/JApplication.h>
15 #include <JANA/JFactory.h>
16 extern "C"{
17 void InitPlugin(JApplication *app){
18  InitJANAPlugin(app);
19  app->AddProcessor(new JEventProcessor_ST_online_Tresolution());
20 }
21 } // "C"
22 
23 
24 //------------------
25 // JEventProcessor_ST_online_Tresolution (Constructor)
26 //------------------
28 {
29 
30 }
31 
32 //------------------
33 // ~JEventProcessor_ST_online_Tresolution (Destructor)
34 //------------------
36 {
37 
38 }
39 
40 //------------------
41 // init
42 //------------------
44 {
45  // This is called once at program startup. If you are creating
46  // and filling historgrams in this plugin, you should lock the
47  // ROOT mutex like this:
48  //
49  // japp->RootWriteLock();
50  // ... fill historgrams or trees ...
51  // japp->RootUnLock();
52  //
53  // **************** define histograms *************************
54 
55  TDirectory *main = gDirectory;
56  gDirectory->mkdir("st_Tresolution")->cd();
57  h2_CorrectedTime_z = new TH2I*[NCHANNELS];
58 
59  // All my Calculations in 2015 were using the binning below
60  NoBins_time = 80;
61  NoBins_z = 1300;
62  time_lower_limit = -4.0;
63  time_upper_limit = 4.0;
64  z_lower_limit = 35.0;
65  z_upper_limit = 100.0;
66  for (Int_t i = 0; i < NCHANNELS; i++)
67  {
68  h2_CorrectedTime_z[i] = new TH2I(Form("h2_CorrectedTime_z_%i", i+1), "Corrected Time vs. Z; Z (cm); Propagation Time (ns)", NoBins_z,z_lower_limit,z_upper_limit, NoBins_time, time_lower_limit, time_upper_limit);
69  }
70 
71  gDirectory->cd("../");
72  main->cd();
73 
74  return NOERROR;
75 }
76 
77 //------------------
78 // brun
79 //------------------
80 jerror_t JEventProcessor_ST_online_Tresolution::brun(JEventLoop *eventLoop, int32_t runnumber)
81 {
82  // This is called whenever the run number changes
83 
84  //RF Period
85  vector<double> locRFPeriodVector;
86  eventLoop->GetCalib("PHOTON_BEAM/RF/rf_period", locRFPeriodVector);
87  dRFBunchPeriod = locRFPeriodVector[0];
88 
89  // Obtain the target center along z;
90  map<string,double> target_params;
91  if (eventLoop->GetCalib("/TARGET/target_parms", target_params))
92  jout << "Error loading /TARGET/target_parms/ !" << endl;
93  if (target_params.find("TARGET_Z_POSITION") != target_params.end())
94  z_target_center = target_params["TARGET_Z_POSITION"];
95  else
96  jerr << "Unable to get TARGET_Z_POSITION from /TARGET/target_parms !" << endl;
97 
98  // Obtain the Start Counter geometry
99  DApplication* dapp = dynamic_cast<DApplication*>(eventLoop->GetJApplication());
100  if(!dapp)
101  _DBG_<<"Cannot get DApplication from JEventLoop! (are you using a JApplication based program?)"<<endl;
102  DGeometry* locGeometry = dapp->GetDGeometry(eventLoop->GetJEvent().GetRunNumber());
103  locGeometry->GetStartCounterGeom(sc_pos, sc_norm);
104  // Propagation Time constant
105  if(eventLoop->GetCalib("START_COUNTER/propagation_time_corr", propagation_time_corr))
106  jout << "Error loading /START_COUNTER/propagation_time_corr !" << endl;
107  return NOERROR;
108 }
109 
110 //------------------
111 // evnt
112 //------------------
113 jerror_t JEventProcessor_ST_online_Tresolution::evnt(JEventLoop *loop, uint64_t eventnumber)
114 {
115  // This is called for every event. Use of common resources like writing
116  // to a file or filling a histogram should be mutex protected. Using
117  // loop->Get(...) to get reconstructed objects (and thereby activating the
118  // reconstruction algorithm) should be done outside of any mutex lock
119  // since multiple threads may call this method at the same time.
120  // Here's an example:
121  //
122  // vector<const MyDataClass*> mydataclasses;
123  // loop->Get(mydataclasses);
124  //
125  // japp->RootWriteLock();
126  // ... fill historgrams or trees ...
127  // japp->RootUnLock();
128  double speed_light = 29.9792458;
129 
130  const DTrigger* locTrigger = NULL;
131  loop->GetSingle(locTrigger);
132  if(locTrigger->Get_L1FrontPanelTriggerBits() != 0)
133  return NOERROR;
134 
135  // Get the particleID object for each run
136  // Be sure that DRFTime_factory::init() and brun() are called
137  vector<const DParticleID *> locParticleID_algos;
138  loop->Get(locParticleID_algos);
139  if(locParticleID_algos.size() < 1)
140  {
141  _DBG_<<"Unable to get a DParticleID object! NO PID will be done!"<<endl;
142  return RESOURCE_UNAVAILABLE;
143  }
144  auto locParticleID = locParticleID_algos[0];
145 
146  // We want to be use some of the tools available in the RFTime factory
147  // Specifically stepping the RF back to a chosen time
148  auto locRFTimeFactory = static_cast<DRFTime_factory*>(loop->GetFactory("DRFTime"));
149 
150 
151  // SC hits
152  vector<const DSCHit *> scHitVector;
153  loop->Get(scHitVector);
154 
155  // RF time object
156  const DRFTime* thisRFTime = NULL;
157  vector <const DRFTime*> RFTimeVector;
158  loop->Get(RFTimeVector);
159  if (RFTimeVector.size() != 0)
160  thisRFTime = RFTimeVector[0];
161 
162  // Grab charged tracks
163  vector<const DChargedTrack *> chargedTrackVector;
164  loop->Get(chargedTrackVector);
165 
166  // Grab the associated detector matches object
167  const DDetectorMatches* locDetectorMatches = NULL;
168  loop->GetSingle(locDetectorMatches);
169 
170  // Grab the associated RF bunch object
171  const DEventRFBunch *thisRFBunch = NULL;
172  loop->GetSingle(thisRFBunch, "Calibrations");
173 
174  // FILL HISTOGRAMS
175  // Since we are filling histograms local to this plugin, it will not interfere with other ROOT operations: can use plugin-wide ROOT fill lock
176  japp->RootFillLock(this); //ACQUIRE ROOT FILL LOCK
177 
178  for (uint32_t i = 0; i < chargedTrackVector.size(); i++)
179  {
180  // Grab the charged track and declare time based track object
181  const DChargedTrack *thisChargedTrack = chargedTrackVector[i];
182  // Grab associated time based track object by selecting charged track with best FOM
183  const DTrackTimeBased *timeBasedTrack = thisChargedTrack->Get_BestTrackingFOM()->Get_TrackTimeBased();
184  // Implement quality cuts for the time based tracks
185  trackingFOMCut = 0.0027; // 3 sigma cut
186  //trackingFOMCut = 0.0001; // 5 sigma cut
187  if(timeBasedTrack->FOM < trackingFOMCut) continue;
188 
189  // Grab the ST hit match params object and cut on only tracks matched to the ST
190  shared_ptr<const DSCHitMatchParams> locBestSCHitMatchParams;
191  foundSC = locParticleID->Get_BestSCMatchParams(timeBasedTrack, locDetectorMatches, locBestSCHitMatchParams);
192  if (!foundSC) continue;
193 
194  // Define vertex vector and cut on target/scattering chamber geometry
195  vertex = timeBasedTrack->position();
196  z_v = vertex.z();
197  r_v = vertex.Perp();
198 
199  z_vertex_cut = fabs(z_target_center - z_v) <= 15.0;
200  r_vertex_cut = r_v < 0.5;
201  // Apply vertex cut
202  if (!z_vertex_cut) continue;
203  if (!r_vertex_cut) continue;
204  bool st_match = locDetectorMatches->Get_SCMatchParams(timeBasedTrack, st_params);
205  // If st_match = true, there is a match between this track and the ST
206  if (!st_match) continue;
207 
208  DVector3 IntersectionPoint, IntersectionMomentum;
209  shared_ptr<DSCHitMatchParams> locSCHitMatchParams;
210  vector<DTrackFitter::Extrapolation_t>extrapolations=timeBasedTrack->extrapolations.at(SYS_START);
211  bool sc_match_pid = locParticleID->Cut_MatchDistance(extrapolations, st_params[0]->dSCHit, st_params[0]->dSCHit->t, locSCHitMatchParams, true, &IntersectionPoint, &IntersectionMomentum);
212 
213  if(!sc_match_pid) continue;
214  // Cut on the number of particle votes to find the best RF time
215  if (thisRFBunch->dNumParticleVotes < 2) continue;
216  // Calculate the TOF estimate of the target time
217 
218  // Calculate the RF estimate of the target time
219  locCenteredRFTime = thisRFTime->dTime;
220  // RF time at center of target
221  locCenterToVertexRFTime = (timeBasedTrack->z() - z_target_center)*(1.0/speed_light); // Time correction for photon from target center to vertex of track
222  locVertexRFTime = locCenteredRFTime + locCenterToVertexRFTime;
223  sc_index= locSCHitMatchParams->dSCHit->sector - 1;
224  // Start Counter geometry in hall coordinates
225  sc_pos_soss = sc_pos[sc_index][0].z(); // Start of straight section
226  sc_pos_eoss = sc_pos[sc_index][1].z(); // End of straight section
227  sc_pos_eobs = sc_pos[sc_index][11].z(); // End of bend section
228  sc_pos_eons = sc_pos[sc_index][12].z(); // End of nose section
229  //Get the ST time walk corrected time
230  st_time = st_params[0]->dSCHit->t;
231  // Get the Flight time
232  FlightTime = locSCHitMatchParams->dFlightTime;
233  //St time corrected for the flight time
234  st_corr_FlightTime = st_time - FlightTime;
235  // SC_RFShiftedTime = locRFTimeFactory->Step_TimeToNearInputTime(locVertexRFTime, st_corr_FlightTime);
236  // Z intersection of charged track and SC
237  locSCzIntersection = IntersectionPoint.z();
238  ////////////////////////////////////////////////////////////////////
239  /// Fill the sc time histograms corrected for walk and propagation//
240  ////////////////////////////////////////////////////////////////////
241  // Read the constants from CCDB
242  double incpt_ss = propagation_time_corr[sc_index][0];
243  double slope_ss = propagation_time_corr[sc_index][1];
244  double incpt_bs = propagation_time_corr[sc_index][2];
245  double slope_bs = propagation_time_corr[sc_index][3];
246  double incpt_ns = propagation_time_corr[sc_index][4];
247  double slope_ns = propagation_time_corr[sc_index][5];
248  // Straight Sections
249  if (locSCzIntersection > sc_pos_soss && locSCzIntersection <= sc_pos_eoss)
250  {
251  Corr_Time_ss = st_corr_FlightTime - (incpt_ss + (slope_ss * locSCzIntersection));
252  SC_RFShiftedTime = locRFTimeFactory->Step_TimeToNearInputTime(locVertexRFTime, Corr_Time_ss);
253  h2_CorrectedTime_z[sc_index]->Fill(locSCzIntersection,Corr_Time_ss -SC_RFShiftedTime);
254  }
255  // Bend Sections
256  if(locSCzIntersection > sc_pos_eoss && locSCzIntersection <= sc_pos_eobs)
257  {
258  Corr_Time_bs = st_corr_FlightTime - (incpt_bs + (slope_bs * locSCzIntersection));
259  SC_RFShiftedTime = locRFTimeFactory->Step_TimeToNearInputTime(locVertexRFTime, Corr_Time_bs);
260  h2_CorrectedTime_z[sc_index]->Fill(locSCzIntersection,Corr_Time_bs - SC_RFShiftedTime);
261  }
262  // Nose Sections
263  if(locSCzIntersection > sc_pos_eobs && locSCzIntersection <= sc_pos_eons)
264  {
265  Corr_Time_ns = st_corr_FlightTime - (incpt_ns + (slope_ns * locSCzIntersection));
266  SC_RFShiftedTime = locRFTimeFactory->Step_TimeToNearInputTime(locVertexRFTime, Corr_Time_ns);
267  h2_CorrectedTime_z[sc_index]->Fill(locSCzIntersection,Corr_Time_ns - SC_RFShiftedTime);
268  }
269  } // sc charged tracks
270 
271  japp->RootFillUnLock(this); //RELEASE ROOT FILL LOCK
272 
273  return NOERROR;
274 }
275 
276 //------------------
277 // erun
278 //------------------
280 {
281  // This is called whenever the run number changes, before it is
282  // changed to give you a chance to clean up before processing
283  // events from the next run number.
284  return NOERROR;
285 }
286 
287 //------------------
288 // fini
289 //------------------
291 {
292  // Called before program exit after event processing is finished.
293  return NOERROR;
294 }
295 
DApplication * dapp
jerror_t init(void)
Called once at program start.
DRFTime_factory * locRFTimeFactory
double z(void) const
bool Get_SCMatchParams(const DTrackingData *locTrack, vector< shared_ptr< const DSCHitMatchParams > > &locMatchParams) const
jerror_t brun(jana::JEventLoop *eventLoop, int32_t runnumber)
Called everytime a new run number is detected.
TVector3 DVector3
Definition: DVector3.h:14
const Int_t NCHANNELS
const DChargedTrackHypothesis * Get_BestTrackingFOM(void) const
Definition: DChargedTrack.h:86
double dTime
Definition: DRFTime.h:24
const DVector3 & position(void) const
const DTrackTimeBased * Get_TrackTimeBased(void) const
uint32_t Get_L1FrontPanelTriggerBits(void) const
JApplication * japp
InitPlugin_t InitPlugin
jerror_t erun(void)
Called everytime run number changes, provided brun has been called.
DGeometry * GetDGeometry(unsigned int run_number)
#define _DBG_
Definition: HDEVIO.h:12
jerror_t fini(void)
Called after last event of last event source has been processed.
map< DetectorSystem_t, vector< DTrackFitter::Extrapolation_t > > extrapolations
unsigned int dNumParticleVotes
Definition: DEventRFBunch.h:32
double Step_TimeToNearInputTime(double locTimeToStep, double locTimeToStepTo) const
jerror_t evnt(jana::JEventLoop *eventLoop, uint64_t eventnumber)
Called every event.
int main(int argc, char *argv[])
Definition: gendoc.cc:6
bool GetStartCounterGeom(vector< vector< DVector3 > > &pos, vector< vector< DVector3 > > &norm) const
Definition: DGeometry.cc:1983