68#include "TRestTrackLinearizationProcess.h"
70#include "TRestTrackReductionProcess.h"
93 fTrackEvent =
nullptr;
108 for (
int t = 0; t < fTrackEvent->GetNumberOfTracks(); t++)
109 fOutTrackEvent->AddTrack(fTrackEvent->GetTrack(t));
111 for (
int t = 0; t < fTrackEvent->GetNumberOfTracks(); t++) {
112 if (fTrackEvent->GetLevel(t) > 1)
continue;
118 if (vHits.GetNumberOfHits() == 0)
continue;
120 RESTDebug <<
"Adding track " <<
RESTendl;
123 newTrack.SetTrackID(fOutTrackEvent->GetNumberOfTracks() + 1);
124 newTrack.SetParentID(track->GetTrackID());
125 newTrack.SetVolumeHits(vHits);
127 RESTDebug <<
"Is XZ " << newTrack.isXZ() <<
" Is YZ " << newTrack.isYZ() <<
RESTendl;
129 fOutTrackEvent->AddTrack(&newTrack);
132 RESTDebug <<
"NTracks X " << fOutTrackEvent->GetNumberOfTracks(
"X") <<
" Y "
133 << fOutTrackEvent->GetNumberOfTracks(
"Y") <<
" " << fOutTrackEvent->GetNumberOfTracks(
"X")
136 fOutTrackEvent->SetLevels();
137 return fOutTrackEvent;
148 const int nHits = hits->GetNumberOfHits();
149 const REST_HitType hType = hits->GetType(0);
152 RESTDebug <<
"NHits " << nHits <<
" hits Type " << hType <<
RESTendl;
155 auto cl =
GetBestNodes(hits->GetX(), hits->GetZ(), hits->GetEnergyVector(), nodes);
156 for (
const auto& [xy, z] : cl) {
157 vHits.AddHit(xy, hits->GetY(0), z, 0, 0, hType, 0, 0, 0);
159 }
else if (hType == YZ) {
160 auto cl =
GetBestNodes(hits->GetY(), hits->GetZ(), hits->GetEnergyVector(), nodes);
161 for (
const auto& [xy, z] : cl) {
162 vHits.AddHit(hits->GetX(0), xy, z, 0, 0, hType, 0, 0, 0);
164 }
else if (hType == XY) {
165 auto cl =
GetBestNodes(hits->GetX(), hits->GetY(), hits->GetEnergyVector(), nodes);
166 for (
const auto& [xy, z] : cl) {
167 vHits.AddHit(xy, z, hits->GetZ(0), 0, 0, hType, 0, 0, 0);
170 RESTWarning <<
"Track linearization not implemented for XYZ tracks" <<
RESTendl;
174 TRestVolumeHits::kMeansClustering(hits, vHits, 1, fFixBoundaries);
177 for (
unsigned int i = 0; i < vHits.GetNumberOfHits(); i++)
178 RESTDebug << i <<
" " << vHits.GetX(i) <<
" " << vHits.GetY(i) <<
" " << vHits.GetZ(i) <<
" "
189 const std::vector<Float_t>& fXY,
const std::vector<Float_t>& fZ,
const std::vector<Float_t>& fEn,
191 const int nHits = fXY.size();
193 const double max[2] = {TMath::MaxElement(fXY.size(), fXY.data()),
194 TMath::MaxElement(fZ.size(), fZ.data())};
195 const double min[2] = {TMath::MinElement(fXY.size(), fXY.data()),
196 TMath::MinElement(fZ.size(), fZ.data())};
199 for (
const auto& en : fEn) totEn += en;
201 RESTDebug <<
"Max " << max[0] <<
" " << max[1] <<
RESTendl;
202 RESTDebug <<
"Min " << min[0] <<
" " << min[1] <<
RESTendl;
206 const int hitAvg = std::round(totEn / (
double)nHits / 10.);
208 for (
int h = 0; h < nHits; h++) {
209 double en = fEn.at(h);
210 for (
int e = 0; e < en; e += hitAvg) {
211 gr[0].SetPoint(c, fXY.at(h), fZ.at(h));
212 gr[1].SetPoint(c, fZ.at(h), fXY.at(h));
217 RESTDebug <<
"Points graph " << c <<
" Hits " << nHits <<
RESTendl;
224 std::string fitOpt =
"";
227 for (
int l = 0; l < 2; l++) {
228 TF1 f1(
"f1",
"[0] * x + [1]", min[l], max[l]);
229 gr[l].Fit(&f1, fitOpt.c_str());
230 double fitGoodness = f1.GetChisquare();
233 bestFit = fitGoodness;
234 slope = f1.GetParameter(0);
235 intercept = f1.GetParameter(1);
237 }
else if (fitGoodness < bestFit) {
238 bestFit = fitGoodness;
240 slope = f1.GetParameter(0);
241 intercept = f1.GetParameter(1);
245 RESTDebug <<
"Best fit " << bestFit <<
" " << bestIndex <<
RESTendl;
246 RESTDebug <<
"Slope " << slope <<
" Intercept " << intercept <<
RESTendl;
248 std::vector<std::pair<double, double>> cluster(nodes);
249 for (
int i = 0; i < nodes; i++) {
250 double first = min[bestIndex] + ((double)i) * (max[bestIndex] - min[bestIndex]) / (
double)(nodes - 1);
251 double second = first * slope + intercept;
253 cluster[i] = std::make_pair(first, second);
255 cluster[i] = std::make_pair(second, first);
A base class for any REST event.
@ REST_Debug
+show the defined debug messages
A process to perform track linearization.
std::vector< std::pair< double, double > > GetBestNodes(const std::vector< Float_t > &fXY, const std::vector< Float_t > &fZ, const std::vector< Float_t > &fEn, const int &nodes)
This function performs a linear fit to the volumehits of the track weighthed by the energy of the hit...
void EndProcess() override
Function to include required actions after all events have been processed.
~TRestTrackLinearizationProcess()
Default destructor.
void Initialize() override
Function to initialize input/output event members and define the section name.
void GetHitsProjection(TRestVolumeHits *hits, const int &nodes, TRestVolumeHits &vHits)
This function performs the track linearization towards a given number of nodes the nodes are extracte...
TRestTrackLinearizationProcess()
Default constructor.
void InitProcess() override
Process initialization. Nothing to do here.
TRestEvent * ProcessEvent(TRestEvent *inputEvent) override
The main processing event function.
void RemoveHits()
It removes all hits inside the class.