18 dMinTrackingFOM = 0.0;
24 gPARMS->SetDefaultParameter(
"EVENTRFBUNCH_CAL:USE_BCAL_SHOWERS", USE_BCAL,
"Use BCAL showers for calorimeter-only RF bunch calculation");
25 gPARMS->SetDefaultParameter(
"EVENTRFBUNCH_CAL:USE_CCAL_SHOWERS", USE_CCAL,
"Use CCAL showers for calorimeter-only RF bunch calculation");
26 gPARMS->SetDefaultParameter(
"EVENTRFBUNCH_CAL:USE_FCAL_SHOWERS", USE_FCAL,
"Use FCAL showers for calorimeter-only RF bunch calculation");
39 vector<double> locBeamPeriodVector;
40 locEventLoop->GetCalib(
"PHOTON_BEAM/RF/beam_period", locBeamPeriodVector);
41 dBeamBunchPeriod = locBeamPeriodVector[0];
43 double locTargetCenterZ;
45 dTargetCenter.SetXYZ(0.0, 0.0, locTargetCenterZ);
47 locEventLoop->GetSingle(dParticleID);
61 vector<const DRFTime*> locRFTimes;
62 locEventLoop->Get(locRFTimes);
63 if(!locRFTimes.empty())
64 return Select_RFBunch(locEventLoop, locRFTimes[0]);
66 return Create_NaNRFBunch();
83 locEventLoop->GetSingle(locDetectorMatches);
85 vector<pair<double, const JObject*> > locTimes;
86 int locBestRFBunchShift = 0, locHighestNumVotes = 0;
88 if(Find_NeutralTimes(locEventLoop, locTimes))
89 locBestRFBunchShift = Conduct_Vote(locEventLoop, locRFTime->
dTime, locTimes,
false, locHighestNumVotes);
91 locBestRFBunchShift = 0;
94 locEventRFBunch->
dTime = locRFTime->
dTime + dBeamBunchPeriod*double(locBestRFBunchShift);
98 _data.push_back(locEventRFBunch);
105 map<int, vector<const JObject*> > locNumBeamBucketsShiftedMap;
106 set<int> locBestRFBunchShifts;
108 locHighestNumVotes = Find_BestRFBunchShifts(locRFTime, locTimes, locNumBeamBucketsShiftedMap, locBestRFBunchShifts);
109 if(locBestRFBunchShifts.size() == 1)
110 return *locBestRFBunchShifts.begin();
113 vector<const DBeamPhoton*> locBeamPhotons;
114 locEventLoop->Get(locBeamPhotons);
115 if(Break_TieVote_BeamPhotons(locBeamPhotons, locRFTime, locNumBeamBucketsShiftedMap, locBestRFBunchShifts, locHighestNumVotes))
116 return *locBestRFBunchShifts.begin();
119 return Break_TieVote_Neutrals(locNumBeamBucketsShiftedMap, locBestRFBunchShifts);
129 vector< const DFCALShower* > fcalShowers;
130 locEventLoop->Get( fcalShowers );
132 for(
size_t i = 0; i < fcalShowers.size(); ++i ){
133 DVector3 locHitPoint = fcalShowers[i]->getPosition();
134 DVector3 locPath = locHitPoint - dTargetCenter;
135 double locPathLength = locPath.Mag();
136 if(!(locPathLength > 0.0))
139 double locFlightTime = locPathLength/29.9792458;
140 double locHitTime = fcalShowers[i]->getTime();
141 locTimes.push_back( pair< double, const JObject*>( locHitTime - locFlightTime, fcalShowers[i] ) );
146 vector< const DBCALShower* > bcalShowers;
147 locEventLoop->Get( bcalShowers );
149 for(
size_t i = 0; i < bcalShowers.size(); ++i ){
151 DVector3 locHitPoint( bcalShowers[i]->
x, bcalShowers[i]->
y, bcalShowers[i]->z );
152 DVector3 locPath = locHitPoint - dTargetCenter;
153 double locPathLength = locPath.Mag();
154 if(!(locPathLength > 0.0))
157 double locFlightTime = locPathLength/29.9792458;
158 double locHitTime = bcalShowers[i]->t;
159 locTimes.push_back( pair< double, const JObject*>( locHitTime - locFlightTime, bcalShowers[i] ) );
164 vector< const DCCALShower* > ccalShowers;
165 locEventLoop->Get( ccalShowers );
167 for(
size_t i = 0; i < ccalShowers.size(); ++i ){
168 DVector3 locHitPoint(ccalShowers[i]->
x, ccalShowers[i]->
y, ccalShowers[i]->z);
169 DVector3 locPath = locHitPoint - dTargetCenter;
170 double locPathLength = locPath.Mag();
171 if(!(locPathLength > 0.0))
174 double locFlightTime = locPathLength/29.9792458;
175 double locHitTime = ccalShowers[i]->time;
176 locTimes.push_back( pair< double, const JObject*>( locHitTime - locFlightTime, ccalShowers[i] ) );
181 return (locTimes.size() > 1);
187 int locHighestNumVotes = 0;
188 locNumBeamBucketsShiftedMap.clear();
189 locBestRFBunchShifts.clear();
191 for(
unsigned int loc_i = 0; loc_i < locTimes.size(); ++loc_i)
193 double locDeltaT = locTimes[loc_i].first - locRFHitTime;
194 int locNumBeamBucketsShifted = (locDeltaT > 0.0) ? int(locDeltaT/dBeamBunchPeriod + 0.5) : int(locDeltaT/dBeamBunchPeriod - 0.5);
195 locNumBeamBucketsShiftedMap[locNumBeamBucketsShifted].push_back(locTimes[loc_i].second);
197 int locNumVotesThisShift = locNumBeamBucketsShiftedMap[locNumBeamBucketsShifted].size();
198 if(locNumVotesThisShift > locHighestNumVotes)
200 locHighestNumVotes = locNumVotesThisShift;
201 locBestRFBunchShifts.clear();
202 locBestRFBunchShifts.insert(locNumBeamBucketsShifted);
204 else if(locNumVotesThisShift == locHighestNumVotes)
205 locBestRFBunchShifts.insert(locNumBeamBucketsShifted);
208 return locHighestNumVotes;
216 set<int> locInputRFBunchShifts = locBestRFBunchShifts;
217 for(
size_t loc_i = 0; loc_i < locBeamPhotons.size(); ++loc_i)
219 double locDeltaT = locBeamPhotons[loc_i]->time() - locRFTime;
220 int locNumBeamBucketsShifted = (locDeltaT > 0.0) ? int(locDeltaT/dBeamBunchPeriod + 0.5) : int(locDeltaT/dBeamBunchPeriod - 0.5);
221 if(locInputRFBunchShifts.find(locNumBeamBucketsShifted) == locInputRFBunchShifts.end())
224 locNumBeamBucketsShiftedMap[locNumBeamBucketsShifted].push_back(locBeamPhotons[loc_i]);
226 int locNumVotesThisShift = locNumBeamBucketsShiftedMap[locNumBeamBucketsShifted].size();
227 if(locNumVotesThisShift > locHighestNumVotes)
229 locHighestNumVotes = locNumVotesThisShift;
230 locBestRFBunchShifts.clear();
231 locBestRFBunchShifts.insert(locNumBeamBucketsShifted);
233 else if(locNumVotesThisShift == locHighestNumVotes)
234 locBestRFBunchShifts.insert(locNumBeamBucketsShifted);
237 return (locBestRFBunchShifts.size() == 1);
245 int locBestRFBunchShift = 0;
246 double locHighestTotalEnergy = 0.0;
248 set<int>::const_iterator locSetIterator = locBestRFBunchShifts.begin();
249 for(; locSetIterator != locBestRFBunchShifts.end(); ++locSetIterator)
251 int locRFBunchShift = *locSetIterator;
252 double locTotalEnergy = 0.0;
254 const vector<const JObject*>& locVoters = locNumBeamBucketsShiftedMap[locRFBunchShift];
255 for(
size_t loc_i = 0; loc_i < locVoters.size(); ++loc_i)
261 if( fcalShower != NULL ){
263 locTotalEnergy += fcalShower->
getEnergy();
268 if( bcalShower != NULL ){
270 locTotalEnergy += bcalShower->
E;
275 if( ccalShower != NULL ){
277 locTotalEnergy += ccalShower->
E;
283 if(locTotalEnergy > locHighestTotalEnergy)
285 locHighestTotalEnergy = locTotalEnergy;
286 locBestRFBunchShift = locRFBunchShift;
290 return locBestRFBunchShift;
296 locEventRFBunch->
dTime = numeric_limits<double>::quiet_NaN();
297 locEventRFBunch->
dTimeVariance = numeric_limits<double>::quiet_NaN();
300 _data.push_back(locEventRFBunch);
bool Break_TieVote_BeamPhotons(vector< const DBeamPhoton * > &locBeamPhotons, double locRFTime, map< int, vector< const JObject * > > &locNumBeamBucketsShiftedMap, set< int > &locBestRFBunchShifts, int locHighestNumVotes)
jerror_t Create_NaNRFBunch(void)
jerror_t Select_RFBunch(JEventLoop *locEventLoop, const DRFTime *locRFTime)
int Find_BestRFBunchShifts(double locRFHitTime, const vector< pair< double, const JObject * > > &locTimes, map< int, vector< const JObject * > > &locNumBeamBucketsShiftedMap, set< int > &locBestRFBunchShifts)
int Break_TieVote_Neutrals(map< int, vector< const JObject * > > &locNumBeamBucketsShiftedMap, set< int > &locBestRFBunchShifts)
int Conduct_Vote(JEventLoop *locEventLoop, double locRFTime, vector< pair< double, const JObject * > > &locTimes, bool locUsedTracksFlag, int &locHighestNumVotes)
DGeometry * GetDGeometry(unsigned int run_number)
jerror_t fini(void)
Called after last event of last event source has been processed.
jerror_t brun(jana::JEventLoop *locEventLoop, int32_t runnumber)
Called everytime a new run number is detected.
jerror_t evnt(jana::JEventLoop *locEventLoop, uint64_t eventnumber)
Called every event.
bool Find_NeutralTimes(JEventLoop *locEventLoop, vector< pair< double, const JObject * > > &locTimes)
jerror_t init(void)
Called once at program start.
DetectorSystem_t dTimeSource
unsigned int dNumParticleVotes
bool GetTargetZ(double &z_target) const
z-location of center of target
jerror_t erun(void)
Called everytime run number changes, provided brun has been called.