hpstr
The Heavy Photon Search Toolkit for Reconstruction (hpstr) provides an interface to physics data from the HPS experiment saved in the LCIO format and converts it into an ROOT based format.
Loading...
Searching...
No Matches
TrackingProcessor.cxx
Go to the documentation of this file.
1
2#include "TrackingProcessor.h"
3#include "utilities.h"
4
6 : Processor(name, process) {
7 }
8
11
13
14 std::cout << "Configuring TrackingProcessor" << std::endl;
15 try
16 {
17 debug_ = parameters.getInteger("debug", debug_);
18 trkCollLcio_ = parameters.getString("trkCollLcio", trkCollLcio_);
19 trkCollRoot_ = parameters.getString("trkCollRoot", trkCollRoot_);
20 kinkRelCollLcio_ = parameters.getString("kinkRelCollLcio", kinkRelCollLcio_);
21 trkRelCollLcio_ = parameters.getString("trkRelCollLcio", trkRelCollLcio_);
22 trkhitCollRoot_ = parameters.getString("trkhitCollRoot", trkhitCollRoot_);
23 hitFitsCollLcio_ = parameters.getString("hitFitsCollLcio", hitFitsCollLcio_);
24 rawhitCollRoot_ = parameters.getString("rawhitCollRoot", rawhitCollRoot_);
25 truthTracksCollLcio_ = parameters.getString("truthTrackCollLcio",truthTracksCollLcio_);
26 truthTracksCollRoot_ = parameters.getString("truthTrackCollRoot",truthTracksCollRoot_);
27 trackStateLocation_ = parameters.getString("trackStateLocation",trackStateLocation_);
28 useTrackerHits_ = parameters.getInteger("useTrackerHits",useTrackerHits_);
29 bfield_ = parameters.getDouble("bfield",bfield_);//this will overwrite the momentum; only use for pre-v3 slcio
30
31 //Residual plotting is done in this processor for the moment.
32 doResiduals_ = parameters.getInteger("doResiduals",doResiduals_);
33 trackResDataLcio_ = parameters.getString("trackResDataLcio",trackResDataLcio_);
34 resCfgFilename_ = parameters.getString("resPlots",resCfgFilename_);
35 resoutname_ = parameters.getString("resoutname",resoutname_);
36
37 }
38 catch (std::runtime_error& error)
39 {
40 std::cout << error.what() << std::endl;
41 }
42
43}
44
46 tree->Branch(trkCollRoot_.c_str(), &tracks_);
47
48 if (!trkhitCollRoot_.empty())
49 tree->Branch(trkhitCollRoot_.c_str(), &hits_);
50
51 if (!rawhitCollRoot_.empty())
52 tree->Branch(rawhitCollRoot_.c_str(), &rawhits_);
53
54 if (!truthTracksCollRoot_.empty())
55 tree->Branch(truthTracksCollRoot_.c_str(),&truthTracks_);
56
57
58 //Residual plotting
59 if (doResiduals_) {
61 trkResHistos_->debugMode(debug_);
62 trkResHistos_->loadHistoConfig(resCfgFilename_);
63 trkResHistos_->doTrackComparisonPlots(false);
64 trkResHistos_->DefineHistos();
65 }
66
67}
68
70
71
72 //Clean up
73 if (tracks_.size() > 0 ) {
74 for (std::vector<Track *>::iterator it = tracks_.begin(); it != tracks_.end(); ++it) {
75 delete *it;
76 }
77 tracks_.clear();
78 }
79
80 if (hits_.size() > 0) {
81 for (std::vector<TrackerHit *>::iterator it = hits_.begin(); it != hits_.end(); ++it) {
82 delete *it;
83 }
84 hits_.clear();
85 }
86
87 if (rawhits_.size() > 0) {
88 for (std::vector<RawSvtHit *>::iterator it = rawhits_.begin(); it != rawhits_.end(); ++it) {
89 delete *it;
90 }
91 rawhits_.clear();
92 }
93
94 if (truthTracks_.size() > 0) {
95 for (std::vector<Track *>::iterator it = truthTracks_.begin(); it != truthTracks_.end(); ++it) {
96 delete *it;
97 }
98 truthTracks_.clear();
99 }
100
101 Event* event = static_cast<Event*> (ievent);
102 // Get the collection of 3D hits from the LCIO event. If no such collection
103 // exist, a DataNotAvailableException is thrown
104
105 // Get decoders to read cellids
106 UTIL::BitField64 decoder("system:6,barrel:3,layer:4,module:12,sensor:1,side:32:-2,strip:12");
107
108 UTIL::LCRelationNavigator* rawTracker_hit_fits_nav = nullptr;
109 EVENT::LCCollection* raw_svt_hit_fits = nullptr;
110 //Check to see if fits are in the file
111 auto evColls = event->getLCEvent()->getCollectionNames();
112 auto it = std::find (evColls->begin(), evColls->end(), hitFitsCollLcio_.c_str());
113 bool hasFits = true;
114 if(it == evColls->end()) hasFits = false;
115 if(hasFits)
116 {
117 raw_svt_hit_fits = event->getLCCollection(hitFitsCollLcio_.c_str());
118 // Heap an LCRelation navigator which will allow faster access
119 rawTracker_hit_fits_nav = new UTIL::LCRelationNavigator(raw_svt_hit_fits);
120 }
121
122 EVENT::LCCollection* tracks{nullptr};
123 try
124 {
125 // Get all track collections from the event
126 tracks = event->getLCCollection(trkCollLcio_.c_str());
127 }
128 catch (EVENT::DataNotAvailableException e)
129 {
130 std::cout << e.what() << std::endl;
131 return false;
132 }
133
134 //Initialize map of shared hits
135 std::map <int, std::vector<int> > SharedHits;
136 //TODO: can we do better? (innermost)
137 std::map <int, bool> SharedHitsLy0;
138 std::map <int, bool> SharedHitsLy1;
139
140 for (int itrack = 0; itrack < tracks->getNumberOfElements();++itrack) {
141 SharedHits[itrack] = {};
142 SharedHitsLy0[itrack] = false;
143 SharedHitsLy1[itrack] = false;
144 }
145
146 // Loop over all the LCIO Tracks and add them to the HPS event.
147 for (int itrack = 0; itrack < tracks->getNumberOfElements(); ++itrack) {
148
149 // Get a LCIO Track from the LCIO event
150 EVENT::Track* lc_track = static_cast<EVENT::Track*>(tracks->getElementAt(itrack));
151
152 // Get the collection of LCRelations between GBL kink data and track data variables
153 // and the corresponding track.
154 EVENT::LCCollection* gbl_kink_data{nullptr};
155 EVENT::LCCollection* track_data{nullptr};
156 try
157 {
158 if (!kinkRelCollLcio_.empty())
159 gbl_kink_data = static_cast<EVENT::LCCollection*>(event->getLCCollection(kinkRelCollLcio_.c_str()));
160 if (!trkRelCollLcio_.empty())
161 track_data = static_cast<EVENT::LCCollection*>(event->getLCCollection(trkRelCollLcio_.c_str()));
162 }
163 catch (EVENT::DataNotAvailableException e)
164 {
165 std::cout << e.what() << std::endl;
166 if (!gbl_kink_data)
167 std::cout<<"TrackingProcessor::Failed retrieving " << kinkRelCollLcio_ <<std::endl;
168 if (!track_data)
169 std::cout<<"TrackingProcessor::Failed retrieving " << trkRelCollLcio_ <<std::endl;
170
171 }
172
173 // Add a track to the event
174 if (debug_ > 0)
175 std::cout << "[TrackingProcessor] calling buildTrack (reco): trackStateLocation=" << trackStateLocation_
176 << " has_gbl_kink=" << (gbl_kink_data ? "yes" : "no")
177 << " has_track_data=" << (track_data ? "yes" : "no") << std::endl;
178 Track* track = utils::buildTrack(lc_track,trackStateLocation_, gbl_kink_data,track_data);
179
180 if (!track) {
181 continue;
182 }
183
184 //Override the momentum of the track if the bfield_ > 0
185 if (bfield_>0)
186 track->setMomentum(bfield_); //this will overwrite the momentum; only use for pre-v3 slcio
187
188
189 int nHits = 0;
190
191 if(!useTrackerHits_){
192 auto hitPattern = lc_track->getSubdetectorHitNumbers();
193 for( int h=0; h<hitPattern.size(); h++){
194 if( hitPattern[h]>0 ){
195 track->addHitLayer(h);
196 nHits++;
197 }
198 }
199 if(nHits==0){
200 std::cout << "[TrackingProcessor::process] WARNING Track with no hits -- likely getSubdetectorHitNumbers isn't filled!" << std::endl;
201 std::cout << "[TrackingProcessor::process] WARNING If hit collections are saved, you may want to turn on useTrackerHits" << std::endl;
202 }
203 track->setTrackerHitCount(nHits);
204 }
205 else{
206
207 // Get the collection of hits associated with a LCIO Track
208 EVENT::TrackerHitVec lc_tracker_hits = lc_track->getTrackerHits();
209
210 // Iterate through the collection of 3D hits (TrackerHit objects)
211 // associated with a track, find the corresponding hits in the HPS
212 // event and add references to the track
213 bool rotateHits = true;
214 int hitType = 0;
215 if (track->isKalmanTrack())
216 hitType=1; //SiClusters
217
218 for (auto lc_tracker_hit : lc_tracker_hits) {
219
220 TrackerHit* tracker_hit = utils::buildTrackerHit(static_cast<IMPL::TrackerHitImpl*>(lc_tracker_hit),rotateHits,hitType);
221
222 std::vector<RawSvtHit*> rawSvthitsOn3d;
223 utils::addRawInfoTo3dHit(tracker_hit,static_cast<IMPL::TrackerHitImpl*>(lc_tracker_hit),
224 raw_svt_hit_fits,&rawSvthitsOn3d,hitType);
225
226 for (auto rhit : rawSvthitsOn3d)
227 rawhits_.push_back(rhit);
228
229 rawSvthitsOn3d.clear();
230
231 if (debug_)
232 std::cout<<tracker_hit->getRawHits().GetEntries()<<std::endl;
233 // Add a reference to the hit
234 track->addHit(tracker_hit);
235 track->addHitLayer(tracker_hit->getLayer());
236 hits_.push_back(tracker_hit);
237
238 //Get shared Hits information
239 for (int jtrack = itrack+1; jtrack < tracks->getNumberOfElements(); ++jtrack) {
240
241 EVENT::Track* j_lc_track = static_cast<EVENT::Track*>(tracks->getElementAt(jtrack));
242 if (utils::isUsedByTrack(tracker_hit,j_lc_track)) {
243 //The hit is not already in the shared list
244 if (std::find(SharedHits[itrack].begin(), SharedHits[itrack].end(),tracker_hit->getID()) == SharedHits[itrack].end()) {
245 SharedHits[itrack].push_back(tracker_hit->getID());
246 if (tracker_hit->getLayer() == 0 )
247 SharedHitsLy0[itrack] = true;
248 if (tracker_hit->getLayer() == 1 )
249 SharedHitsLy1[itrack] = true;
250 }
251 if (std::find(SharedHits[jtrack].begin(), SharedHits[jtrack].end(),tracker_hit->getID()) == SharedHits[jtrack].end()) {
252 SharedHits[jtrack].push_back(tracker_hit->getID());
253 if (tracker_hit->getLayer() == 0 )
254 SharedHitsLy0[jtrack] = true;
255 if (tracker_hit->getLayer() == 1 )
256 SharedHitsLy1[jtrack] = true;
257 }
258 } // found shared hit
259 } // loop on j>i tracks
260 }//tracker hits
261
262 track->setNShared(SharedHits[itrack].size());
263 track->setSharedLy0(SharedHitsLy0[itrack]);
264 track->setSharedLy1(SharedHitsLy1[itrack]);
265 }
266
267 //Get the truth tracks relations:
268
269 // Get the collection of LCRelations between GBL kink data and track data variables
270 // and the corresponding track.
271 EVENT::LCCollection* truth_tracks_rel{nullptr};
272
273 try
274 {
275 if (!truthTracksCollLcio_.empty())
276 truth_tracks_rel = static_cast<EVENT::LCCollection*>(event->getLCCollection(truthTracksCollLcio_.c_str()));
277 }
278 catch (EVENT::DataNotAvailableException e)
279 {
280 std::cout << e.what() << std::endl;
281 if (!truth_tracks_rel)
282 std::cout<<"Failed retrieving " << truthTracksCollLcio_ <<std::endl;
283 }
284
285
286 if (truth_tracks_rel) {
287
288 std::shared_ptr<UTIL::LCRelationNavigator> truth_tracks_nav = std::make_shared<UTIL::LCRelationNavigator>(truth_tracks_rel);
289 //Get the truth_track associated with the lcio_track
290 EVENT::LCObjectVec lc_truth_tracks = truth_tracks_nav->getRelatedToObjects(lc_track);
291 if (lc_truth_tracks.size() < 1) {
292 // No truth matched track for this track - skip silently
293 }
294 else {
295 EVENT::Track* lc_truth_track = static_cast<EVENT::Track*> (lc_truth_tracks.at(0));
296 if (!lc_truth_track) {
297 tracks_.push_back(track);
298 continue;
299 }
300 if (debug_ > 0)
301 std::cout << "[TrackingProcessor] calling buildTrack (truth): trackStateLocation=" << trackStateLocation_ << std::endl;
302 Track* truth_track = utils::buildTrack(lc_truth_track,trackStateLocation_,nullptr,nullptr);
303 if (!truth_track) {
304 tracks_.push_back(track);
305 continue;
306 }
307 track->setTruthLink(truth_track);
308 if (bfield_>0)
309 truth_track->setMomentum(bfield_);//this will overwrite the momentum; only use for pre-v3 slcio
310 //truth tracks phi needs to be corrected
311 if (truth_track->getPhi() > TMath::Pi())
312 truth_track->setPhi(truth_track->getPhi() - (TMath::Pi()) * 2.);
313
314 truthTracks_.push_back(truth_track);
315 }
316
317 }
318 tracks_.push_back(track);
319
320
321
322 //Do the residual plots -- should be in another function
323 if (doResiduals_) {
324 EVENT::LCCollection* trackRes_data_rel{nullptr};
325 try {
326 if (!trackResDataLcio_.empty())
327 trackRes_data_rel = static_cast<EVENT::LCCollection*>(event->getLCCollection(trackResDataLcio_.c_str()));
328 }
329 catch (EVENT::DataNotAvailableException e)
330 {
331 std::cout<<e.what()<<std::endl;
332 }
333
334 if (trackRes_data_rel) {
335 std::shared_ptr<UTIL::LCRelationNavigator> trackRes_data_nav = std::make_shared<UTIL::LCRelationNavigator>(trackRes_data_rel);
336 EVENT::LCObjectVec trackRes_data_vec = trackRes_data_nav->getRelatedFromObjects(lc_track);
337 IMPL::LCGenericObjectImpl* trackRes_data = static_cast<IMPL::LCGenericObjectImpl*>(trackRes_data_vec.at(0));
338
339 /*
340 //Some of the residuals do not get saved because sigma is negative. Will be fixed.
341 if (track->getTrackerHitCount() != trackRes_data->getNDouble()) {
342 std::cout<<"WARNING-Different number of hit on track residuals wrt measurements"<<std::endl;
343 std::cout<<"Hits::"<<track->getTrackerHitCount()<<" Residuals:"<<trackRes_data->getNDouble()<<std::endl;
344 }
345 */
346
347 //Last int is the volume
348 for (int i_res = 0; i_res < trackRes_data->getNInt()-1;i_res++) {
349 //std::cout<<"Residual ly " << trackRes_data->getIntVal(i_res)<<" res="<< trackRes_data->getDoubleVal(i_res)<<" sigma="<<trackRes_data->getFloatVal(i_res)<<std::endl;
350 int ly = trackRes_data->getIntVal(i_res);
351 double res = trackRes_data->getDoubleVal(i_res);
352 double sigma = trackRes_data->getFloatVal(i_res);
353 trkResHistos_->FillResidualHistograms(track,ly,res,sigma);
354 }
355 }//trackResData exists
356 }//doResiduals
357
358
359 }// tracks
360
361 //delete
362 if (rawTracker_hit_fits_nav) {
363 delete rawTracker_hit_fits_nav; rawTracker_hit_fits_nav = nullptr;}
364
365 //event->addCollection("TracksInfo", &tracks_);
366 //event->addCollection("TrackerHitsInfo", &hits_);
367 //event->addCollection("TrackerHitsRawInfo", &rawhits_);
368
369 return true;
370}
371
373
374 if (doResiduals_) {
375 TFile* outfile = new TFile(resoutname_.c_str(),"RECREATE");
376 trkResHistos_->saveHistos(outfile,trkCollLcio_);
377 if (trkResHistos_) delete trkResHistos_;
378 trkResHistos_=nullptr;
379 }
380}
381
#define DECLARE_PROCESSOR(CLASS)
Macro which allows the framework to construct a producer given its name during configuration.
Definition Processor.h:139
Definition Event.h:35
Definition IEvent.h:7
description
Processor(const std::string &name, Process &process)
Class constructor.
Definition Processor.cxx:10
virtual bool process()
Process the histograms and generate analysis output.
Definition Processor.h:95
description
Definition TrackHistos.h:17
Definition Track.h:32
double getPhi() const
Definition Track.h:88
void setMomentum(double bfield=0.52)
Definition Track.cxx:63
void setPhi(const double phi0)
Definition Track.h:89
int getLayer() const
Definition TrackerHit.h:109
int getID() const
Definition TrackerHit.h:124
TRefArray getRawHits() const
Definition TrackerHit.h:36
Insert description here. more details.
std::string truthTracksCollRoot_
description
virtual bool process(IEvent *ievent)
Process the event and put new data products into it.
std::string trkRelCollLcio_
collection name
std::string trackStateLocation_
Specify track state used for track collection DEFAULT AtIP.
int debug_
Debug Level.
std::string trkhitCollRoot_
description
double bfield_
magnetic field
std::string trkCollRoot_
collection name
int useTrackerHits_
Load hit collections, otherwise get from getSubdetectorHitNumbers.
virtual void configure(const ParameterSet &parameters)
Configure the Processor.
virtual void finalize()
Callback for the Processor to take any necessary action when the processing of events finishes.
std::string trackResDataLcio_
description
std::vector< Track * > tracks_
virtual void initialize(TTree *tree)
Callback for the Processor to take any necessary action when the processing of events starts.
std::string hitFitsCollLcio_
collection name
std::string resoutname_
description
std::vector< RawSvtHit * > rawhits_
std::vector< TrackerHit * > hits_
std::vector< Track * > truthTracks_
std::string truthTracksCollLcio_
description
std::string resCfgFilename_
description
std::string trkCollLcio_
collection name
int doResiduals_
do Residuals
std::string kinkRelCollLcio_
collection name
TrackHistos * trkResHistos_
description
std::string rawhitCollRoot_
collection name
TrackingProcessor(const std::string &name, Process &process)
Class constructor.
bool addRawInfoTo3dHit(TrackerHit *tracker_hit, IMPL::TrackerHitImpl *lc_tracker_hit, EVENT::LCCollection *raw_svt_fits, std::vector< RawSvtHit * > *rawHits=nullptr, int type=0, bool storeRawHit=true)
description
Track * buildTrack(EVENT::Track *lc_track, std::string trackstate_location, EVENT::LCCollection *gbl_kink_data, EVENT::LCCollection *track_data)
description
bool isUsedByTrack(IMPL::TrackerHitImpl *lc_tracker_hit, EVENT::Track *lc_track)
description
TrackerHit * buildTrackerHit(IMPL::TrackerHitImpl *lc_trackerHit, bool rotate=true, int type=0)
description