REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
Loading...
Searching...
No Matches
TRestDetectorHitsReadoutAnalysisProcess.cxx
1//
2// Created by lobis on 03-Sep-23.
3//
4
5#include "TRestDetectorHitsReadoutAnalysisProcess.h"
6
7#include <numeric>
8
9using namespace std;
10
12 fInputHitsEvent = (TRestDetectorHitsEvent*)inputEvent;
13
14 vector<TVector3> hitPosition;
15 vector<double> hitEnergy;
16 double energyInFiducial = 0;
17
18 for (size_t hitIndex = 0; hitIndex < fInputHitsEvent->GetNumberOfHits(); hitIndex++) {
19 const auto position = fInputHitsEvent->GetPosition(hitIndex);
20 const auto energy = fInputHitsEvent->GetEnergy(hitIndex);
21 const auto time = fInputHitsEvent->GetTime(hitIndex);
22 const auto type = fInputHitsEvent->GetType(hitIndex);
23 fOutputHitsEvent->AddHit(position, energy, time, type);
24
25 if (energy <= 0) {
26 // this should never happen
27 cerr << "TRestDetectorHitsReadoutAnalysisProcess::ProcessEvent() : "
28 << "Negative or zero energy found in hit " << hitIndex << endl;
29 exit(1);
30 }
31
32 const auto daqId = fReadout->GetDaqId(position, false);
33 const auto channelType = fReadout->GetTypeForChannelDaqId(daqId);
34
35 if (channelType != fChannelType) {
36 continue;
37 }
38
39 hitPosition.push_back(position);
40 hitEnergy.push_back(energy);
41
42 if (fFiducialDiameter > 0) {
43 TVector3 relativePosition = position - fFiducialPosition;
44 relativePosition.SetZ(0); // TODO: this should be relative to readout normal
45 const double distance = relativePosition.Mag();
46 if (distance > fFiducialDiameter / 2) {
47 continue;
48 }
49 energyInFiducial += energy;
50 }
51 }
52
53 const double readoutEnergy = accumulate(hitEnergy.begin(), hitEnergy.end(), 0.0);
54
55 if (fRemoveZeroEnergyEvents && readoutEnergy <= 0) {
56 return nullptr;
57 }
58
59 TVector3 positionAverage;
60 for (size_t i = 0; i < hitPosition.size(); i++) {
61 positionAverage += hitPosition[i] * hitEnergy[i];
62 }
63 positionAverage *= 1.0 / readoutEnergy;
64
65 // standard deviation weighted with energy
66 TVector3 positionSigma;
67 for (size_t i = 0; i < hitPosition.size(); i++) {
68 TVector3 diff2 = hitPosition[i] - positionAverage;
69 diff2.SetX(diff2.X() * diff2.X());
70 diff2.SetY(diff2.Y() * diff2.Y());
71 diff2.SetZ(diff2.Z() * diff2.Z());
72 positionSigma += diff2 * hitEnergy[i];
73 }
74 positionSigma *= 1.0 / readoutEnergy;
75 positionSigma.SetX(sqrt(positionSigma.X()));
76 positionSigma.SetY(sqrt(positionSigma.Y()));
77 positionSigma.SetZ(sqrt(positionSigma.Z()));
78
79 SetObservableValue("readoutEnergy", readoutEnergy);
80 SetObservableValue("readoutNumberOfHits", hitEnergy.size());
81
82 SetObservableValue("readoutAveragePositionX", positionAverage.X());
83 SetObservableValue("readoutAveragePositionY", positionAverage.Y());
84 SetObservableValue("readoutAveragePositionZ", positionAverage.Z());
85
86 SetObservableValue("readoutSigmaPositionX", positionSigma.X());
87 SetObservableValue("readoutSigmaPositionY", positionSigma.Y());
88 SetObservableValue("readoutSigmaPositionZ", positionSigma.Z());
89
90 SetObservableValue("readoutEnergyInFiducial", energyInFiducial);
91
92 return fOutputHitsEvent;
93}
94
96 fReadout = dynamic_cast<TRestDetectorReadout*>(fRunInfo->GetMetadataClass("TRestDetectorReadout"));
97 if (!fReadout) {
98 cerr << "TRestDetectorHitsReadoutAnalysisProcess::InitProcess() : "
99 << "TRestDetectorReadout not found" << endl;
100 exit(1);
101 }
102
103 if (fChannelType == "") {
104 cerr << "TRestDetectorHitsReadoutAnalysisProcess::InitProcess() : "
105 << "Channel type not defined" << endl;
106 exit(1);
107 }
108}
109
111
114
115 RESTMetadata << "Channel type : " << fChannelType << RESTendl;
116 RESTMetadata << "Fiducial position : (" << fFiducialPosition.X() << " , " << fFiducialPosition.Y()
117 << " , " << fFiducialPosition.Z() << ")" << RESTendl;
118 RESTMetadata << "Fiducial diameter : " << fFiducialDiameter << RESTendl;
119
120 EndPrintProcess();
121}
122
124 fRemoveZeroEnergyEvents = StringToBool(GetParameter("removeZeroEnergyEvents", "false"));
125 fChannelType = GetParameter("channelType", fChannelType);
126 fFiducialPosition = Get3DVectorParameterWithUnits("fiducialPosition", fFiducialPosition);
127 fFiducialDiameter = GetDblParameterWithUnits("fiducialDiameter", fFiducialDiameter);
128}
129
131 SetSectionName(this->ClassName());
132 SetLibraryVersion(LIBRARY_VERSION);
133
134 fInputHitsEvent = nullptr;
135 fOutputHitsEvent = new TRestDetectorHitsEvent();
136}
void AddHit(Double_t x, Double_t y, Double_t z, Double_t en, Double_t t=0, REST_HitType type=XYZ)
Adds a new hit to this event.
void InitProcess() override
To be executed at the beginning of the run (outside event loop)
TRestEvent * ProcessEvent(TRestEvent *inputEvent) override
Process one event.
std::string fChannelType
This process will only work on hits corresponding to this channel type (using readout)
void PrintMetadata() override
Implemented it in the derived metadata class to print out specific metadata information.
void InitFromConfigFile() override
To make settings from rml file. This method must be implemented in the derived class.
void EndProcess() override
To be executed at the end of the run (outside event loop)
A metadata class to generate/store a readout description.
Int_t GetDaqId(const TVector3 &position, bool check=true)
Returns the DaqID of the channel for position. If no channel is found returns -1.
void BeginPrintProcess()
[name, cut range]
TRestRun * fRunInfo
< Pointer to TRestRun object where to find metadata.
void SetObservableValue(const std::string &name, const T &value)
Set observable value for AnalysisTree.
A base class for any REST event.
Definition TRestEvent.h:38
endl_t RESTendl
Termination flag object for TRestStringOutput.
void SetLibraryVersion(TString version)
Set the library version of this metadata class.
void SetSectionName(std::string sName)
set the section name, clear the section content
std::string GetParameter(std::string parName, TiXmlElement *e, TString defaultValue=PARAMETER_NOT_FOUND_STR)
Returns the value for the parameter named parName in the given section.