Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
JEventProcessor_SC_Eff.cc
Go to the documentation of this file.
1 // $Id$
2 //
3 // File: JEventProcessor_SC_Eff.cc
4 //
5 
7 
8 // Routine used to create our JEventProcessor
9 extern "C"
10 {
11  void InitPlugin(JApplication *locApplication)
12  {
13  InitJANAPlugin(locApplication);
14  locApplication->AddProcessor(new JEventProcessor_SC_Eff()); //register this plugin
15  }
16 } // "C"
17 
18 //define static local variable //declared in header file
20 
21 //------------------
22 // init
23 //------------------
25 {
26  //TRACK REQUIREMENTS
27  dMinTrackingFOM = 5.73303E-7; // +/- 5 sigma
28  dMinNumTrackHits = 14; //e.g. 6 in CDC, 8 in
31  dMaxVertexR = 1.0;
33  //action initialize not necessary: is empty
36 
37  TDirectory* locOriginalDir = gDirectory;
38  gDirectory->mkdir("SC_Eff")->cd();
39 
40  //Histograms
41  dHist_HitFound = new TH2I("HitFound", "Hit Found;Projected Hit-Z (cm);Sector", 120, 40.0, 100.0, 30, 0.5, 30.5);
42  dHist_HitTotal = new TH2I("HitTotal", "Hit Total;Projected Hit-Z (cm);Sector", 120, 40.0, 100.0, 30, 0.5, 30.5);
43 
44  // back to original dir
45  locOriginalDir->cd();
46 
47  //TTREE INTERFACE
48  //MUST DELETE WHEN FINISHED: OR ELSE DATA WON'T BE SAVED!!!
49  dTreeInterface = DTreeInterface::Create_DTreeInterface("sc_eff", "tree_sc_eff.root");
50 
51  //TTREE BRANCHES
52  DTreeBranchRegister locTreeBranchRegister;
53 
54  //TRACK
55  locTreeBranchRegister.Register_Single<Int_t>("PID_PDG"); //gives charge, mass, beta
56  locTreeBranchRegister.Register_Single<Float_t>("TrackVertexZ");
57  locTreeBranchRegister.Register_Single<TVector3>("TrackP3");
58  locTreeBranchRegister.Register_Single<UInt_t>("TrackCDCRings"); //rings correspond to bits (1 -> 28)
59  locTreeBranchRegister.Register_Single<UInt_t>("TrackFDCPlanes"); //planes correspond to bits (1 -> 24)
60 
61  //PROJECTED HIT
62  locTreeBranchRegister.Register_Single<Float_t>("ProjectedSCHitPhi"); //degrees
63  locTreeBranchRegister.Register_Single<Float_t>("ProjectedSCHitZ");
64  locTreeBranchRegister.Register_Single<UChar_t>("ProjectedSCHitSector");
65 
66  //SEARCH
67  locTreeBranchRegister.Register_Single<UChar_t>("NumMatchingSCHits");
68  locTreeBranchRegister.Register_FundamentalArray<UChar_t>("SCHitSector", "NumMatchingSCHits"); //0 if none in time: PID:OUT_OF_TIME_CUT
69  locTreeBranchRegister.Register_FundamentalArray<Float_t>("TrackHitDeltaPhi", "NumMatchingSCHits"); //is signed: SC - Track
70  locTreeBranchRegister.Register_FundamentalArray<Float_t>("TrackHitDeltaT", "NumMatchingSCHits"); //is signed: SC - Track
71  locTreeBranchRegister.Register_FundamentalArray<Bool_t>("IsMatchedToTrack", "NumMatchingSCHits"); //false if not registered in DDetectorMatches
72 
73  //REGISTER BRANCHES
74  dTreeInterface->Create_Branches(locTreeBranchRegister);
75 
76  return NOERROR;
77 }
78 
79 //------------------
80 // brun
81 //------------------
82 jerror_t JEventProcessor_SC_Eff::brun(jana::JEventLoop* locEventLoop, int locRunNumber)
83 {
84  // This is called whenever the run number changes
85 
86  return NOERROR;
87 }
88 
89 //------------------
90 // evnt
91 //------------------
92 
93 jerror_t JEventProcessor_SC_Eff::evnt(jana::JEventLoop* locEventLoop, uint64_t locEventNumber)
94 {
95  // This is called for every event. Use of common resources like writing
96  // to a file or filling a histogram should be mutex protected. Using
97  // locEventLoop->Get(...) to get reconstructed objects (and thereby activating the
98  // reconstruction algorithm) should be done outside of any mutex lock
99  // since multiple threads may call this method at the same time.
100 
101  // This plugin is used to determine the reconstruction efficiency of hits in the BCAL
102  // Note, this is hit-level, not shower-level. Hits: By sector/layer/module/end
103 
104  //CUT ON TRIGGER TYPE
105  const DTrigger* locTrigger = NULL;
106  locEventLoop->GetSingle(locTrigger);
107  if(locTrigger->Get_L1FrontPanelTriggerBits() != 0)
108  return NOERROR;
109 
110  //SEE IF SC REQUIRED TO TRIGGER
111  uint32_t locTriggerBits = locTrigger->Get_L1TriggerBits();
112  if(locTriggerBits >= 32)
113  return NOERROR;
114 
115  const DEventRFBunch* locEventRFBunch = NULL;
116  locEventLoop->GetSingle(locEventRFBunch);
117  if(locEventRFBunch->dNumParticleVotes <= 1)
118  return NOERROR; //don't trust PID: beta-dependence
119 
120  vector<const DChargedTrack*> locChargedTracks;
121  locEventLoop->Get(locChargedTracks);
122 
123  vector<const DSCHit*> locSCHits;
124  locEventLoop->Get(locSCHits);
125 
126  const DParticleID* locParticleID = NULL;
127  locEventLoop->GetSingle(locParticleID);
128 
129  const DDetectorMatches* locDetectorMatches = NULL;
130  locEventLoop->GetSingle(locDetectorMatches);
131 
132  //Try to select the most-pure sample of tracks possible
133  set<const DChargedTrackHypothesis*> locBestTracks;
134  for(auto& locChargedTrack : locChargedTracks)
135  {
136  //loop over PID hypotheses and find the best (if any good enough)
137  const DChargedTrackHypothesis* locBestChargedTrackHypothesis = nullptr;
138  double locBestTrackingFOM = -1.0;
139  for(auto& locChargedTrackHypothesis : locChargedTrack->dChargedTrackHypotheses)
140  {
141  if((locChargedTrackHypothesis->PID() == Electron) || (locChargedTrackHypothesis->PID() == Positron))
142  continue; //don't evaluate for these
143 
144  if(locChargedTrackHypothesis->position().Perp() > dMaxVertexR)
145  continue; //don't trust reconstruction if not close to target
146 
147  //Need PID for beta-dependence
148  if(!Cut_PIDDeltaT(locChargedTrackHypothesis))
149  continue; //also requires match to BCAL or TOF: no need for separate check
150 
151  auto locTrackTimeBased = locChargedTrackHypothesis->Get_TrackTimeBased();
152  if(locTrackTimeBased->FOM < dMinTrackingFOM)
153  continue; //don't trust tracking results: bad tracking FOM
154 
155  if(!dCutAction_TrackHitPattern->Cut_TrackHitPattern(locParticleID, locTrackTimeBased))
156  continue; //don't trust tracking results: not many grouped hits
157 
158  unsigned int locNumTrackHits = locTrackTimeBased->Ndof + 5;
159  if(locNumTrackHits < dMinNumTrackHits)
160  continue; //don't trust tracking results: not many hits
161 
162  if(locTrackTimeBased->FOM < locBestTrackingFOM)
163  continue; //not the best mass hypothesis
164 
165  locBestTrackingFOM = locTrackTimeBased->FOM;
166  locBestChargedTrackHypothesis = locChargedTrackHypothesis;
167  }
168 
169  //if passed all cuts, save the best
170  if(locBestChargedTrackHypothesis != nullptr)
171  locBestTracks.insert(locBestChargedTrackHypothesis);
172  }
173 
174  //for histograms, keep running total of what to fill
175  //will fill at end: only one lock
176  vector<pair<int, double> > locHitMap_HitFound, locHitMap_HitTotal; //int: sector, double: projected-z
177 
178  // Loop over the good tracks, using the best DTrackTimeBased object for each
179  for(auto& locChargedTrackHypothesis : locBestTracks)
180  {
181  auto locTrackTimeBased = locChargedTrackHypothesis->Get_TrackTimeBased();
182 
183  //Predict ST Surface Hit Location
184  DVector3 locPredictedSurfacePosition;
185  bool locProjBarrelRegion = false;
186  vector<DTrackFitter::Extrapolation_t>extrapolations=locTrackTimeBased->extrapolations.at(SYS_START);
187  unsigned int locPredictedSCSector = locParticleID->PredictSCSector(extrapolations, &locPredictedSurfacePosition, &locProjBarrelRegion);
188 
189  //Save for hist if is matched
190  pair<int, double> locHitPair(locPredictedSCSector, locPredictedSurfacePosition.Z());
191  locHitMap_HitTotal.push_back(locHitPair);
192  if(locDetectorMatches->Get_IsMatchedToDetector(locTrackTimeBased, SYS_START))
193  locHitMap_HitFound.push_back(locHitPair);
194 
195  //TRACK
196  dTreeFillData.Fill_Single<Int_t>("PID_PDG", PDGtype(locChargedTrackHypothesis->PID()));
197  dTreeFillData.Fill_Single<Float_t>("TrackVertexZ", locChargedTrackHypothesis->position().Z());
198  dTreeFillData.Fill_Single<UInt_t>("TrackCDCRings", locTrackTimeBased->dCDCRings);
199  dTreeFillData.Fill_Single<UInt_t>("TrackFDCPlanes", locTrackTimeBased->dFDCPlanes);
200  DVector3 locDP3 = locChargedTrackHypothesis->momentum();
201  TVector3 locP3(locDP3.X(), locDP3.Y(), locDP3.Z());
202  dTreeFillData.Fill_Single<TVector3>("TrackP3", locP3);
203 
204  //PROJECTED
205  dTreeFillData.Fill_Single<Float_t>("ProjectedSCHitPhi", locPredictedSurfacePosition.Phi()*180.0/TMath::Pi());
206  dTreeFillData.Fill_Single<Float_t>("ProjectedSCHitZ", locPredictedSurfacePosition.Z());
207  dTreeFillData.Fill_Single<UChar_t>("ProjectedSCHitSector", locPredictedSCSector);
208 
209  vector<shared_ptr<const DSCHitMatchParams>> locAllSCMatchParams;
210  locDetectorMatches->Get_SCMatchParams(locTrackTimeBased, locAllSCMatchParams);
211 
212  //pre-sort
213  set<const DSCHit*> locMatchedSCHits;
214  for(auto locSCMatchParams : locAllSCMatchParams)
215  locMatchedSCHits.insert(locSCMatchParams->dSCHit);
216 
217  //FILL ARRAYS
218  size_t locArrayIndex = 0;
219  for(size_t loc_i = 0; loc_i < locSCHits.size(); ++loc_i)
220  {
221  const DSCHit* locSCHit = locSCHits[loc_i];
222 
223  shared_ptr<DSCHitMatchParams> locSCHitMatchParams;
224  if(!locParticleID->Distance_ToTrack(extrapolations, locSCHit, locTrackTimeBased->t0(), locSCHitMatchParams))
225  continue;
226 
227  double locDeltaT = locSCHitMatchParams->dHitTime - locSCHitMatchParams->dFlightTime - locChargedTrackHypothesis->time();
228  bool locIsMatchedToTrack = (locMatchedSCHits.find(locSCHit) != locMatchedSCHits.end());
229 
230  dTreeFillData.Fill_Array<UChar_t>("SCHitSector", locSCHit->sector, locArrayIndex);
231  dTreeFillData.Fill_Array<Float_t>("TrackHitDeltaPhi", locSCHitMatchParams->dDeltaPhiToHit, locArrayIndex); //is signed: SC - Track
232  dTreeFillData.Fill_Array<Float_t>("TrackHitDeltaT", locDeltaT, locArrayIndex); //is signed: SC - Track
233  dTreeFillData.Fill_Array<Bool_t>("IsMatchedToTrack", locIsMatchedToTrack, locArrayIndex);
234 
235  ++locArrayIndex;
236  }
237 
238  //FILL ARRAY SIZE
239  dTreeFillData.Fill_Single<UChar_t>("NumMatchingSCHits", locArrayIndex);
240 
241  //FILL TTREE
243  }
244 
245  // FILL HISTOGRAMS
246  // Since we are filling histograms local to this plugin, it will not interfere with other ROOT operations: can use plugin-wide ROOT fill lock
247  japp->RootFillLock(this); //ACQUIRE ROOT FILL LOCK
248  {
249  //Fill Found
250  for(auto& locHitPair : locHitMap_HitFound)
251  dHist_HitFound->Fill(locHitPair.second, locHitPair.first);
252 
253  //Fill Total
254  for(auto& locHitPair : locHitMap_HitTotal)
255  dHist_HitTotal->Fill(locHitPair.second, locHitPair.first);
256  }
257  japp->RootFillUnLock(this); //RELEASE ROOT FILL LOCK
258 
259  return NOERROR;
260 }
261 
263 {
264  DetectorSystem_t locSystem = locChargedTrackHypothesis->t1_detector();
265  if(dMaxPIDDeltaTMap.find(locSystem) == dMaxPIDDeltaTMap.end())
266  return false;
267 
268  double locDeltaT = locChargedTrackHypothesis->time() - locChargedTrackHypothesis->t0();
269  return (fabs(locDeltaT) <= dMaxPIDDeltaTMap[locSystem]);
270 }
271 
272 //------------------
273 // erun
274 //------------------
276 {
277  // This is called whenever the run number changes, before it is
278  // changed to give you a chance to clean up before processing
279  // events from the next run number.
280 
281  return NOERROR;
282 }
283 
284 //------------------
285 // fini
286 //------------------
288 {
289  // Called before program exit after event processing is finished.
290 
292  delete dTreeInterface; //saves trees to file, closes file
293 
294  return NOERROR;
295 }
296 
unsigned int PredictSCSector(const DReferenceTrajectory *rt, DVector3 *locOutputProjPos=nullptr, bool *locProjBarrelRegion=nullptr, double *locMinDPhi=nullptr) const
jerror_t brun(jana::JEventLoop *locEventLoop, int locRunNumber)
Called every time a new run number is detected.
jerror_t erun(void)
Called every time run number changes, provided brun has been called.
bool Get_SCMatchParams(const DTrackingData *locTrack, vector< shared_ptr< const DSCHitMatchParams > > &locMatchParams) const
TVector3 DVector3
Definition: DVector3.h:14
jerror_t init(void)
Called once at program start.
uint32_t Get_L1TriggerBits(void) const
jerror_t fini(void)
Called after last event of last event source has been processed.
void Register_Single(string locBranchName)
void Fill_Array(string locBranchName, const DType &locData, size_t locArrayIndex)
const DTrackTimeBased * Get_TrackTimeBased(void) const
bool Create_Branches(const DTreeBranchRegister &locTreeBranchRegister)
uint32_t Get_L1FrontPanelTriggerBits(void) const
DetectorSystem_t
Definition: GlueX.h:15
static int PDGtype(Particle_t p)
Definition: GlueX.h:19
JApplication * japp
int sector
Definition: DSCHit.h:18
double time(void) const
Definition: DSCHit.h:14
void Fill(DTreeFillData &locTreeFillData)
static DTreeInterface * Create_DTreeInterface(string locTreeName, string locFileName)
InitPlugin_t InitPlugin
Definition: GlueX.h:20
static thread_local DTreeFillData dTreeFillData
DCutAction_TrackHitPattern * dCutAction_TrackHitPattern
void Register_FundamentalArray(string locBranchName, string locArraySizeName, size_t locInitialArraySize=10)
int Ndof
Number of degrees of freedom in the fit.
bool Cut_TrackHitPattern(const DParticleID *locParticleID, const DKinematicData *locTrack) const
Definition: DCutActions.cc:809
bool Cut_PIDDeltaT(const DChargedTrackHypothesis *locChargedTrackHypothesis)
unsigned int dNumParticleVotes
Definition: DEventRFBunch.h:32
bool Distance_ToTrack(const DReferenceTrajectory *rt, const DFCALShower *locFCALShower, double locInputStartTime, shared_ptr< DFCALShowerMatchParams > &locShowerMatchParams, DVector3 *locOutputProjPos=nullptr, DVector3 *locOutputProjMom=nullptr) const
Definition: DParticleID.cc:737
DetectorSystem_t t1_detector(void) const
jerror_t evnt(jana::JEventLoop *locEventLoop, uint64_t locEventNumber)
Called every event.
map< DetectorSystem_t, double > dMaxPIDDeltaTMap
bool Get_IsMatchedToDetector(const DTrackingData *locTrack, DetectorSystem_t locDetectorSystem) const
void Fill_Single(string locBranchName, const DType &locData)