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.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
TrackHitAnaProcessor.cxx
Go to the documentation of this file.
2#include <iomanip>
3#include "utilities.h"
4
5TrackHitAnaProcessor::TrackHitAnaProcessor(const std::string& name, Process& process)
6 : Processor(name, process) {
7 }
8
11
13
14 std::cout << "Configuring TrackHitAnaProcessor" << std::endl;
15 try
16 {
17 debug_ = parameters.getInteger("debug",debug_);
18 trkCollName_ = parameters.getString("trkCollName",trkCollName_);
19 histCfgFilename_ = parameters.getString("histCfg",histCfgFilename_);
20 doTruth_ = (bool) parameters.getInteger("doTruth",doTruth_);
21 truthHistCfgFilename_ = parameters.getString("truthHistCfg",truthHistCfgFilename_);
22 selectionCfg_ = parameters.getString("selectionjson",selectionCfg_);
23 regionSelections_ = parameters.getVString("regionDefinitions",regionSelections_);
24 }
25 catch (std::runtime_error& error)
26 {
27 std::cout << error.what() << std::endl;
28 }
29
30}
31
33
34 //Init histos
35 trkHistos_ = new TrackHistos("preselTrks");
39 // Init tree
40 tree->SetBranchAddress(trkCollName_.c_str(), &tracks_, &btracks_);
41
42 if (!selectionCfg_.empty()) {
43 trkSelector_ = std::make_shared<BaseSelector>(name_+"_trkSelector",selectionCfg_);
44 trkSelector_->setDebug(debug_);
45 trkSelector_->LoadSelection();
46 std::cout << "Track Selection Loaded" << std::endl;
47 }
48
49 if (doTruth_) {
50 truthHistos_ = new TrackHistos(trkCollName_+"_truthComparison");
56
57 //tree->SetBranchAddress(truthCollName_.c_str(),&truth_tracks_,&btruth_tracks_);
58 }
59
60 //Setup regions
61 for (unsigned int i_reg = 0; i_reg < regionSelections_.size(); i_reg++)
62 {
63 std::string regname = AnaHelpers::getFileName(regionSelections_[i_reg],false);
64 std::cout << "Setting up region:: " << regname << std::endl;
65 reg_selectors_[regname] = std::make_shared<BaseSelector>(regname, regionSelections_[i_reg]);
66 reg_selectors_[regname]->setDebug(debug_);
67 reg_selectors_[regname]->LoadSelection();
68
69 reg_histos_[regname] = std::make_shared<TrackHistos>(regname);
70 reg_histos_[regname]->loadHistoConfig(histCfgFilename_);
71 reg_histos_[regname]->doTrackComparisonPlots(false);
72 reg_histos_[regname]->DefineTrkHitHistos();
73
74 regions_.push_back(regname);
75
76 }
77
78}
79
81
82 double weight = 1.;
83 // Loop over all the LCIO Tracks and add them to the HPS event.
84 int n_sel_tracks = 0;
85 for (int itrack = 0; itrack < tracks_->size(); ++itrack) {
86
87 if (trkSelector_) trkSelector_->getCutFlowHisto()->Fill(0.,weight);
88
89 // Get a track
90 Track* track = tracks_->at(itrack);
91 bool isTop = (track->getTanLambda() > 0.0);
92 bool isPos = (track->getOmega() < 0.0);
93 bool isKF = track->isKalmanTrack();
94 int trkType = (int)isTop*2 + (int)isPos;
95 int n2dhits_onTrack = !track->isKalmanTrack() ? track->getTrackerHitCount() * 2 : track->getTrackerHitCount();
96 //if (n2dhits_onTrack != track->getSvtHits()->GetEntries()) continue;
97
98 //Track Selection
99 if (trkSelector_ && !trkSelector_->passCutGt("n_hits_gt",n2dhits_onTrack,weight))
100 continue;
101
102 if (trkSelector_ && !trkSelector_->passCutLt("chi2ndf_lt",track->getChi2Ndf(),weight))
103 continue;
104
105 if (trkSelector_ && !trkSelector_->passCutGt("p_gt",fabs(track->getP()),weight))
106 continue;
107
108 if (trkSelector_ && !trkSelector_->passCutLt("p_lt",fabs(track->getP()),weight))
109 continue;
110
111 std::vector<int> hit_layers;
112 int hitCode = 0;
113 int n12hits = 0;
114 for (int ihit = 0; ihit<track->getSvtHits().GetEntries(); ++ihit) {
115 TrackerHit* hit = (TrackerHit*) track->getSvtHits().At(ihit);
116 int layer = hit->getLayer();
117 hit_layers.push_back(layer);
118 if (isKF)
119 {
120 if (layer < 4)
121 {
122 hitCode = hitCode | (0x1 << layer);
123 n12hits++;
124 }
125 }
126 else
127 {
128 if (layer < 2)
129 {
130 hitCode = hitCode | (0x1 << (2*layer));
131 hitCode = hitCode | (0x1 << (2*layer+1));
132 }
133 }
134 }
135
136 trkHistos_->Fill1DHisto("hitCode_h", hitCode);
137 trkHistos_->Fill2DHisto("hitCode_trkType_hh", hitCode, trkType);
138 trkHistos_->Fill1DTrack(track, weight, "");
139 trkHistos_->Fill2DTrack(track, weight, "");
140
141 n_sel_tracks++;
142
143 for (auto region : regions_ )
144 {
145
146 reg_selectors_[region]->getCutFlowHisto()->Fill(0.,weight);
147 if(debug_) std::cout<<"Check for region "<<region
148 <<" hc "<<hitCode
149 <<" lt:"<< !reg_selectors_[region]->passCutLt("hitCode_lt", ((double)hitCode)-0.5, weight)
150 <<" gt:"<< !reg_selectors_[region]->passCutGt("hitCode_gt", ((double)hitCode)+0.5, weight)
151 << std::endl;
152 //Hit code req
153 if ( !reg_selectors_[region]->passCutLt("hitCode_lt", ((double)hitCode)-0.5, weight) ) continue;
154
155 if(debug_) std::cout<<"Pass Lt cut"<<std::endl;
156 if ( !reg_selectors_[region]->passCutGt("hitCode_gt", ((double)hitCode)+0.5, weight) ) continue;
157
158 if(debug_) std::cout<<"Pass Gt cut"<<std::endl;
159
160 if(debug_) std::cout<<"Pass region "<<region<<std::endl;
161 reg_histos_[region]->Fill1DHisto("hitCode_h", hitCode,weight);
162 if(isTop&&isPos)
163 {
164 reg_histos_[region]->Fill1DTrack(track, weight, "topPos_");
165 reg_histos_[region]->Fill2DTrack(track, weight, "topPos_");
166 }
167 if(isTop&&!isPos)
168 {
169 reg_histos_[region]->Fill1DTrack(track, weight, "topEle_");
170 reg_histos_[region]->Fill2DTrack(track, weight, "topEle_");
171 }
172 if(!isTop&&isPos)
173 {
174 reg_histos_[region]->Fill1DTrack(track, weight, "botPos_");
175 reg_histos_[region]->Fill2DTrack(track, weight, "botPos_");
176 }
177 if(!isTop&&!isPos)
178 {
179 reg_histos_[region]->Fill1DTrack(track, weight, "botEle_");
180 reg_histos_[region]->Fill2DTrack(track, weight, "botEle_");
181 }
182 }
183 }//Loop on tracks
184
185 trkHistos_->Fill1DHisto("n_tracks_h",n_sel_tracks);
186
187
188
189 return true;
190}
191
193
194 outF_->cd();
195 trkHistos_->saveHistos(outF_,"preselTrks");
196 delete trkHistos_;
197 trkHistos_ = nullptr;
198 if (trkSelector_)
199 trkSelector_->getCutFlowHisto()->Write();
200
201 for (reg_it it = reg_histos_.begin(); it!=reg_histos_.end(); ++it) {
202 std::string dirName = it->first;
203 (it->second)->saveHistos(outF_,dirName);
204 outF_->cd(dirName.c_str());
205 reg_selectors_[it->first]->getCutFlowHisto()->Scale(0.5);
206 reg_selectors_[it->first]->getCutFlowHisto()->Write();
207 }
208
209
210 if (truthHistos_) {
212 delete truthHistos_;
213 truthHistos_ = nullptr;
214 }
215 //trkHistos_->Clear();
216}
217
#define DECLARE_PROCESSOR(CLASS)
Macro which allows the framework to construct a producer given its name during configuration.
Definition Processor.h:139
static std::string getFileName(std::string filePath, bool withExtension)
Definition AnaHelpers.cxx:5
void Fill2DHisto(const std::string &histoName, float valuex, float valuey, float weight=1.)
description
virtual void DefineHistos()
Definition of histograms from json config.
void Fill1DHisto(const std::string &histoName, float value, float weight=1.)
description
virtual void loadHistoConfig(const std::string histoConfigFile)
load histogram config
virtual void saveHistos(TFile *outF=nullptr, std::string folder="")
save histograms
Definition IEvent.h:7
description
Base class for all event processing components.
Definition Processor.h:34
TFile * outF_
Definition Processor.h:125
std::string name_
Definition Processor.h:128
virtual bool process()
Process the histograms and generate analysis output.
Definition Processor.h:95
description
Definition TrackHistos.h:17
void Fill2DTrack(Track *track, float weight=1., const std::string &trkname="")
Fill 2D track.
void doTrackComparisonPlots(bool doplots)
Compare tracks.
void Fill1DTrack(Track *track, float weight=1., const std::string &trkname="")
Fill 1D track.
Insert description here. more details.
TrackHitAnaProcessor(const std::string &name, Process &process)
Class constructor.
std::string trkCollName_
Track Collection name.
std::string truthHistCfgFilename_
description
virtual void configure(const ParameterSet &parameters)
Configure the Ana Processor.
virtual void finalize()
Callback for the Processor to take any necessary action when the processing of events finishes.
std::map< std::string, std::shared_ptr< TrackHistos > > reg_histos_
description
std::vector< std::string > regionSelections_
description
virtual void initialize(TTree *tree)
Callback for the Processor to take any necessary action when the processing of events starts.
std::map< std::string, std::shared_ptr< TrackHistos > >::iterator reg_it
description
std::string histCfgFilename_
description
TrackHistos * trkHistos_
description
std::map< std::string, std::shared_ptr< BaseSelector > > reg_selectors_
description
TBranch * btracks_
description
TrackHistos * truthHistos_
description
std::vector< Track * > * tracks_
std::vector< std::string > regions_
description
std::shared_ptr< BaseSelector > trkSelector_
description
Definition Track.h:32
int getLayer() const
Definition TrackerHit.h:109