63#include "TRestTrackLinearizationProcess.h"
65#include "TRestTrackReductionProcess.h"
89 fTrackEvent =
nullptr;
104 for (
int t = 0; t < fTrackEvent->GetNumberOfTracks(); t++)
105 fOutTrackEvent->AddTrack(fTrackEvent->GetTrack(t));
107 for (
int t = 0; t < fTrackEvent->GetNumberOfTracks(); t++) {
108 if (fTrackEvent->GetLevel(t) > 1)
continue;
114 if (vHits.GetNumberOfHits() == 0)
continue;
116 RESTDebug <<
"Adding track " <<
RESTendl;
119 newTrack.SetTrackID(fOutTrackEvent->GetNumberOfTracks() + 1);
120 newTrack.SetParentID(track->GetTrackID());
121 newTrack.SetVolumeHits(vHits);
123 RESTDebug <<
"Is XZ " << newTrack.isXZ() <<
" Is YZ " << newTrack.isYZ() <<
RESTendl;
125 fOutTrackEvent->AddTrack(&newTrack);
128 RESTDebug <<
"NTracks X " << fOutTrackEvent->GetNumberOfTracks(
"X") <<
" Y "
129 << fOutTrackEvent->GetNumberOfTracks(
"Y") <<
" " << fOutTrackEvent->GetNumberOfTracks(
"X")
132 fOutTrackEvent->SetLevels();
133 return fOutTrackEvent;
144 const int nHits = hits->GetNumberOfHits();
145 const REST_HitType hType = hits->GetType(0);
148 RESTDebug <<
"NHits " << nHits <<
" hits Type " << hType <<
RESTendl;
151 auto cl =
GetBestNodes(hits->GetX(), hits->GetZ(), hits->GetEnergyVector(), nodes);
152 for (
const auto& [xy, z] : cl) {
153 vHits.AddHit(xy, hits->GetY(0), z, 0, 0, hType, 0, 0, 0);
155 }
else if (hType == YZ) {
156 auto cl =
GetBestNodes(hits->GetY(), hits->GetZ(), hits->GetEnergyVector(), nodes);
157 for (
const auto& [xy, z] : cl) {
158 vHits.AddHit(hits->GetX(0), xy, z, 0, 0, hType, 0, 0, 0);
160 }
else if (hType == XY) {
161 auto cl =
GetBestNodes(hits->GetX(), hits->GetY(), hits->GetEnergyVector(), nodes);
162 for (
const auto& [xy, z] : cl) {
163 vHits.AddHit(xy, z, hits->GetZ(0), 0, 0, hType, 0, 0, 0);
166 RESTWarning <<
"Track linearization not implemented for XYZ tracks" <<
RESTendl;
170 TRestVolumeHits::kMeansClustering(hits, vHits, 1);
173 for (
unsigned int i = 0; i < vHits.GetNumberOfHits(); i++)
174 RESTDebug << i <<
" " << vHits.GetX(i) <<
" " << vHits.GetY(i) <<
" " << vHits.GetZ(i) <<
" "
185 const std::vector<Float_t>& fXY,
const std::vector<Float_t>& fZ,
const std::vector<Float_t>& fEn,
187 const int nHits = fXY.size();
189 const double max[2] = {TMath::MaxElement(fXY.size(), fXY.data()),
190 TMath::MaxElement(fZ.size(), fZ.data())};
191 const double min[2] = {TMath::MinElement(fXY.size(), fXY.data()),
192 TMath::MinElement(fZ.size(), fZ.data())};
195 for (
const auto& en : fEn) totEn += en;
197 RESTDebug <<
"Max " << max[0] <<
" " << max[1] <<
RESTendl;
198 RESTDebug <<
"Min " << min[0] <<
" " << min[1] <<
RESTendl;
202 const int hitAvg = std::round(totEn / (
double)nHits / 10.);
204 for (
int h = 0; h < nHits; h++) {
205 double en = fEn.at(h);
206 for (
int e = 0; e < en; e += hitAvg) {
207 gr[0].SetPoint(c, fXY.at(h), fZ.at(h));
208 gr[1].SetPoint(c, fZ.at(h), fXY.at(h));
213 RESTDebug <<
"Points graph " << c <<
" Hits " << nHits <<
RESTendl;
220 std::string fitOpt =
"";
223 for (
int l = 0; l < 2; l++) {
224 TF1 f1(
"f1",
"[0] * x + [1]", min[l], max[l]);
225 gr[l].Fit(&f1, fitOpt.c_str());
226 double fitGoodness = f1.GetChisquare();
229 bestFit = fitGoodness;
230 slope = f1.GetParameter(0);
231 intercept = f1.GetParameter(1);
233 }
else if (fitGoodness < bestFit) {
234 bestFit = fitGoodness;
236 slope = f1.GetParameter(0);
237 intercept = f1.GetParameter(1);
241 RESTDebug <<
"Best fit " << bestFit <<
" " << bestIndex <<
RESTendl;
242 RESTDebug <<
"Slope " << slope <<
" Intercept " << intercept <<
RESTendl;
244 std::vector<std::pair<double, double>> cluster(nodes);
245 for (
int i = 0; i < nodes; i++) {
246 double first = min[bestIndex] + ((double)i) * (max[bestIndex] - min[bestIndex]) / (
double)(nodes - 1);
247 double second = first * slope + intercept;
249 cluster[i] = std::make_pair(first, second);
251 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.