Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DBeamPhoton_factory.cc
Go to the documentation of this file.
1 // $Id$
2 //
3 // File: DBeamPhoton_factory.cc
4 // Created: Thu Dec 3 17:27:55 EST 2009
5 // Creator: staylor (on Linux ifarml6 2.6.18-128.el5 x86_64)
6 //
7 
8 #include <iostream>
9 #include <iomanip>
10 #include <cmath>
11 using namespace std;
12 
13 #include "DBeamPhoton_factory.h"
14 using namespace jana;
15 
16 inline bool DBeamPhoton_SortByTime(const DBeamPhoton* locBeamPhoton1, const DBeamPhoton* locBeamPhoton2)
17 {
18  // pseudo-randomly sort beam photons by time, using only sub-picosecond digits
19  // do this by converting to picoseconds, then stripping all digits above the decimal place
20 
21  //guard against NaN
22  if(std::isnan(locBeamPhoton1->time()))
23  return false;
24  if(std::isnan(locBeamPhoton2->time()))
25  return true;
26  double locT1_Picoseconds_Stripped = 1000.0*locBeamPhoton1->time() - floor(1000.0*locBeamPhoton1->time());
27  double locT2_Picoseconds_Stripped = 1000.0*locBeamPhoton2->time() - floor(1000.0*locBeamPhoton2->time());
28  return (locT1_Picoseconds_Stripped < locT2_Picoseconds_Stripped);
29 }
30 
31 //------------------
32 // init
33 //------------------
35 {
36  DELTA_T_DOUBLES_MAX = 1.5; // ns
37  gPARMS->SetDefaultParameter("BeamPhoton:DELTA_T_DOUBLES_MAX", DELTA_T_DOUBLES_MAX,
38  "Maximum time difference in ns between a TAGM-TAGH pair of beam photons"
39  " for them to be merged into a single photon");
40  DELTA_E_DOUBLES_MAX = 0.05; // GeV
41  gPARMS->SetDefaultParameter("BeamPhoton:DELTA_E_DOUBLES_MAX", DELTA_E_DOUBLES_MAX,
42  "Maximum energy difference in GeV between a TAGM-TAGH pair of beam photons"
43  " for them to be merged into a single photon");
44 
45  //Setting this flag makes it so that JANA does not delete the objects in _data. This factory will manage this memory.
46  SetFactoryFlag(NOT_OBJECT_OWNER);
47  return NOERROR;
48 }
49 
50 //------------------
51 // brun
52 //------------------
53 jerror_t DBeamPhoton_factory::brun(jana::JEventLoop *locEventLoop, int32_t runnumber)
54 {
55  DApplication* dapp = dynamic_cast<DApplication*>(locEventLoop->GetJApplication());
56  DGeometry* locGeometry = dapp->GetDGeometry(locEventLoop->GetJEvent().GetRunNumber());
57  dTargetCenterZ = 0.0;
58  locGeometry->GetTargetZ(dTargetCenterZ);
59 
60  return NOERROR;
61 }
62 
63 //------------------
64 // evnt
65 //------------------
66 jerror_t DBeamPhoton_factory::evnt(jana::JEventLoop *locEventLoop, uint64_t locEventNumber)
67 {
68  dResourcePool_BeamPhotons->Recycle(dCreated);
69  dCreated.clear();
70  _data.clear();
71 
72  vector<const DTAGMHit*> tagm_hits;
73  locEventLoop->Get(tagm_hits);
74 
75  for (unsigned int ih=0; ih < tagm_hits.size(); ++ih)
76  {
77  if (!tagm_hits[ih]->has_fADC) continue; // Skip TDC-only hits (i.e. hits with no ADC info.)
78  if (tagm_hits[ih]->row > 0) continue; // Skip individual fiber readouts
79  DBeamPhoton* gamma = Get_Resource();
80 
81  Set_BeamPhoton(gamma, tagm_hits[ih], locEventNumber);
82  _data.push_back(gamma);
83  }
84 
85  vector<const DTAGHHit*> tagh_hits;
86  locEventLoop->Get(tagh_hits);
87 
88  for (unsigned int ih=0; ih < tagh_hits.size(); ++ih)
89  {
90  if (!tagh_hits[ih]->has_fADC) continue; // Skip TDC-only hits (i.e. hits with no ADC info.)
91  DBeamPhoton *gamma = nullptr;
92  for (unsigned int jh=0; jh < _data.size(); ++jh)
93  {
94  if (fabs(_data[jh]->momentum().Mag() - tagh_hits[ih]->E) < DELTA_E_DOUBLES_MAX
95  && fabs(_data[jh]->time() - tagh_hits[ih]->t) < DELTA_T_DOUBLES_MAX)
96  {
97  gamma = _data[jh];
98  if (_data[jh]->momentum().Mag() < tagh_hits[ih]->E)
99  {
100  gamma->Reset();
101  Set_BeamPhoton(gamma, tagh_hits[ih], locEventNumber);
102  }
103  }
104  }
105  if (gamma == nullptr)
106  {
107  gamma = Get_Resource();
108  Set_BeamPhoton(gamma, tagh_hits[ih], locEventNumber);
109  _data.push_back(gamma);
110  }
111  }
112 
113  sort(_data.begin(), _data.end(), DBeamPhoton_SortByTime);
114  dCreated = _data;
115 
116  return NOERROR;
117 }
118 
119 void DBeamPhoton_factory::Set_BeamPhoton(DBeamPhoton* gamma, const DTAGMHit* hit, uint64_t locEventNumber)
120 {
121  DVector3 pos(0.0, 0.0, dTargetCenterZ);
122  DVector3 mom(0.0, 0.0, hit->E);
123  gamma->setPID(Gamma);
124  gamma->setMomentum(mom);
125  gamma->setPosition(pos);
126  gamma->setTime(hit->t);
127  gamma->dCounter = hit->column;
128  gamma->dSystem = SYS_TAGM;
129  gamma->AddAssociatedObject(hit);
130 
131  auto locCovarianceMatrix = dResourcePool_TMatrixFSym->Get_SharedResource();
132  locCovarianceMatrix->ResizeTo(7, 7);
133  locCovarianceMatrix->Zero();
134  gamma->setErrorMatrix(locCovarianceMatrix);
135 }
136 
137 void DBeamPhoton_factory::Set_BeamPhoton(DBeamPhoton* gamma, const DTAGHHit* hit, uint64_t locEventNumber)
138 {
139  DVector3 pos(0.0, 0.0, dTargetCenterZ);
140  DVector3 mom(0.0, 0.0, hit->E);
141  gamma->setPID(Gamma);
142  gamma->setMomentum(mom);
143  gamma->setPosition(pos);
144  gamma->setTime(hit->t);
145  gamma->dCounter = hit->counter_id;
146  gamma->dSystem = SYS_TAGH;
147  gamma->AddAssociatedObject(hit);
148 
149  auto locCovarianceMatrix = dResourcePool_TMatrixFSym->Get_SharedResource();
150  locCovarianceMatrix->ResizeTo(7, 7);
151  locCovarianceMatrix->Zero();
152  gamma->setErrorMatrix(locCovarianceMatrix);
153 }
unsigned int dCounter
Definition: DBeamPhoton.h:18
double t
Definition: DTAGHHit.h:19
void setMomentum(const DVector3 &aMomentum)
double E
Definition: DTAGHHit.h:18
void setTime(double locTime)
DApplication * dapp
double E
Definition: DTAGMHit.h:18
jerror_t brun(jana::JEventLoop *locEventLoop, int32_t runnumber)
Called everytime a new run number is detected.
TVector3 DVector3
Definition: DVector3.h:14
bool DBeamPhoton_SortByTime(const DBeamPhoton *locBeamPhoton1, const DBeamPhoton *locBeamPhoton2)
Definition: GlueX.h:29
Definition: GlueX.h:24
double t
Definition: DTAGMHit.h:19
double time(void) const
int counter_id
Definition: DTAGHHit.h:20
void setErrorMatrix(const shared_ptr< const TMatrixFSym > &aMatrix)
DGeometry * GetDGeometry(unsigned int run_number)
void setPID(Particle_t locPID)
DetectorSystem_t dSystem
Definition: DBeamPhoton.h:19
void Set_BeamPhoton(DBeamPhoton *gamma, const DTAGHHit *hit, uint64_t locEventNumber)
virtual void Reset(void)
int column
Definition: DTAGMHit.h:21
void setPosition(const DVector3 &aPosition)
jerror_t init(void)
Called once at program start.
jerror_t evnt(jana::JEventLoop *locEventLoop, uint64_t locEventNumber)
Called every event.
bool GetTargetZ(double &z_target) const
z-location of center of target
Definition: DGeometry.cc:1933