Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update hits rotation #99

Merged
merged 4 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 19 additions & 27 deletions inc/TRestDetectorHitsRotateAndTranslateProcess.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,9 @@ class TRestDetectorHitsRotateAndTranslateProcess : public TRestEventProcess {
TRestDetectorHitsEvent* fInputHitsEvent; //!
TRestDetectorHitsEvent* fOutputHitsEvent; //!

Double_t fDeltaX; ///< shift in X-axis
Double_t fDeltaY; ///< shift in X-axis
Double_t fDeltaZ; ///< shift in X-axis

Double_t fAlpha; ///< rotation angle around z-axis
Double_t fBeta; ///< rotation angle around y-axis
Double_t fGamma; ///< rotation angle around x-axis
TVector3 fTranslationVector = {0, 0, 0}; ///< translation vector (x,y,z)
TVector3 fRotationVector = {0, 0, 0}; ///< rotation vector around axis (x,y,z) in radians
TVector3 fRotationCenter = {0, 0, 0}; ///< rotation center (x,y,z)

void InitFromConfigFile() override;
void Initialize() override;
Expand All @@ -49,39 +45,35 @@ class TRestDetectorHitsRotateAndTranslateProcess : public TRestEventProcess {
TRestEvent* ProcessEvent(TRestEvent* inputEvent) override;
void EndProcess() override;

void LoadConfig(std::string configFilename);
void LoadConfig(const std::string& configFilename);

void PrintMetadata() override {
BeginPrintProcess();

RESTMetadata << " delta x : " << fDeltaX << RESTendl;
RESTMetadata << " delta y : " << fDeltaY << RESTendl;
RESTMetadata << " delta z : " << fDeltaZ << RESTendl;
RESTMetadata << " alpha : " << fAlpha << RESTendl;
RESTMetadata << " beta : " << fBeta << RESTendl;
RESTMetadata << " gamma : " << fGamma << RESTendl;
RESTMetadata << "Translation vector (mm): (" << fTranslationVector.X() << ", "
<< fTranslationVector.Y() << ", " << fTranslationVector.Z() << ")" << RESTendl;
TVector3 rotationVectorDegrees = fRotationVector * (180. / TMath::Pi());
RESTMetadata << "Rotation vector (degrees): (" << rotationVectorDegrees.X() << ", "
<< rotationVectorDegrees.Y() << ", " << rotationVectorDegrees.Z() << ")" << RESTendl;

EndPrintProcess();
}

const char* GetProcessName() const override { return "rotateAndTraslate"; }
const char* GetProcessName() const override { return "rotateAndTranslate"; }

inline Double_t GetDeltaX() const { return fDeltaX; }
inline Double_t GetDeltaY() const { return fDeltaY; }
inline Double_t GetDeltaZ() const { return fDeltaZ; }
inline Double_t GetTranslationX() const { return fTranslationVector.X(); }
inline Double_t GetTranslationY() const { return fTranslationVector.Y(); }
inline Double_t GetTranslationZ() const { return fTranslationVector.Z(); }

inline Double_t GetAlpha() const { return fAlpha; }
inline Double_t GetBeta() const { return fBeta; }
inline Double_t GetGamma() const { return fGamma; }
inline Double_t GetRotationX() const { return fRotationVector.X(); }
inline Double_t GetRotationY() const { return fRotationVector.Y(); }
inline Double_t GetRotationZ() const { return fRotationVector.Z(); }

// Constructor
TRestDetectorHitsRotateAndTranslateProcess();
TRestDetectorHitsRotateAndTranslateProcess(const char* configFilename);
// Destructor
explicit TRestDetectorHitsRotateAndTranslateProcess(const char* configFilename);
~TRestDetectorHitsRotateAndTranslateProcess();

ClassDefOverride(TRestDetectorHitsRotateAndTranslateProcess,
1); // Template for a REST "event process" class inherited from
// TRestEventProcess
ClassDefOverride(TRestDetectorHitsRotateAndTranslateProcess, 2);
};

#endif
121 changes: 61 additions & 60 deletions src/TRestDetectorHitsRotateAndTranslateProcess.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,24 @@
/// Javier G. Garza
///_______________________________________________________________________________

//////////////////////////////////////////////////////////////////////////
///
/// This process rotates and translates hits in a TRestDetectorHitsEvent.
/// Hits of type "VETO" are not affected by this process (we typically only want to rotate TPC hits).
///
/// Usage:
///
/// <addProcess type="TRestDetectorHitsRotateAndTranslateProcess">
/// <parameter name="rotationCenter" value="(0,0,0)mm" />
/// <parameter name="translation" value="(0,10,0)mm" />
/// <parameter name="rotationZ" value="45deg" />
/// </addProcess>
///

#include "TRestDetectorHitsRotateAndTranslateProcess.h"

using namespace std;

#include <TRandom3.h>

ClassImp(TRestDetectorHitsRotateAndTranslateProcess);

TRestDetectorHitsRotateAndTranslateProcess::TRestDetectorHitsRotateAndTranslateProcess() { Initialize(); }
Expand All @@ -40,89 +52,78 @@ TRestDetectorHitsRotateAndTranslateProcess::TRestDetectorHitsRotateAndTranslateP
PrintMetadata();
}

TRestDetectorHitsRotateAndTranslateProcess::~TRestDetectorHitsRotateAndTranslateProcess() {
// TRestDetectorHitsRotateAndTranslateProcess destructor
}

void TRestDetectorHitsRotateAndTranslateProcess::LoadDefaultConfig() {
SetTitle("Default config");
TRestDetectorHitsRotateAndTranslateProcess::~TRestDetectorHitsRotateAndTranslateProcess() = default;

fDeltaX = 1.0;
fDeltaY = 1.0;
fDeltaZ = 1.0;
fAlpha = 0.;
fBeta = 0.;
fGamma = 0.;
}
void TRestDetectorHitsRotateAndTranslateProcess::LoadDefaultConfig() { SetTitle("Default config"); }

void TRestDetectorHitsRotateAndTranslateProcess::Initialize() {
SetSectionName(this->ClassName());
SetLibraryVersion(LIBRARY_VERSION);

fDeltaX = 1.0;
fDeltaY = 1.0;
fDeltaZ = 1.0;
fAlpha = 0.;
fBeta = 0.;
fGamma = 0.;

fInputHitsEvent = nullptr;
fOutputHitsEvent = nullptr;

// Get volume name from parameter
}

void TRestDetectorHitsRotateAndTranslateProcess::LoadConfig(string configFilename) {
if (LoadConfigFromFile(configFilename)) LoadDefaultConfig();
void TRestDetectorHitsRotateAndTranslateProcess::LoadConfig(const string& configFilename) {
if (LoadConfigFromFile(configFilename)) {
LoadDefaultConfig();
}

PrintMetadata();
}

void TRestDetectorHitsRotateAndTranslateProcess::InitProcess() {
// Function to be executed once at the beginning of process
// (before starting the process of the events)

// Start by calling the InitProcess function of the abstract class.
// Comment this if you don't want it.
// TRestEventProcess::InitProcess();
}
void TRestDetectorHitsRotateAndTranslateProcess::InitProcess() {}

TRestEvent* TRestDetectorHitsRotateAndTranslateProcess::ProcessEvent(TRestEvent* inputEvent) {
fInputHitsEvent = (TRestDetectorHitsEvent*)inputEvent;

fOutputHitsEvent = fInputHitsEvent;
// fInputHitsEvent->CloneTo(fOutputHitsEvent);

TVector3 meanPosition = fOutputHitsEvent->GetMeanPosition();
for (unsigned int hit = 0; hit < fOutputHitsEvent->GetNumberOfHits(); hit++) {
fOutputHitsEvent->GetHits()->RotateIn3D(hit, fAlpha, fBeta, fGamma, meanPosition);
fOutputHitsEvent->GetHits()->Translate(hit, fDeltaX, fDeltaY, fDeltaZ);
if (fOutputHitsEvent->GetNumberOfHits() == 0) {
return nullptr;
}

if (fOutputHitsEvent->GetNumberOfHits() == 0) return nullptr;
for (unsigned int hit = 0; hit < fOutputHitsEvent->GetNumberOfHits(); hit++) {
const auto& type = fOutputHitsEvent->GetHits()->GetType(hit);
if (type == VETO) {
// Do not rotate VETO hits (we typically only rotate TPC hits)
continue;
}
fOutputHitsEvent->GetHits()->RotateIn3D(hit, fRotationVector.X(), fRotationVector.Y(),
fRotationVector.Z(), fRotationCenter);
fOutputHitsEvent->GetHits()->Translate(hit, fTranslationVector.X(), fTranslationVector.Y(),
fTranslationVector.Z());
}

RESTDebug << "Number of hits rotated: " << fInputHitsEvent->GetNumberOfHits() << RESTendl;
return fOutputHitsEvent;
}

void TRestDetectorHitsRotateAndTranslateProcess::EndProcess() {
// Function to be executed once at the end of the process
// (after all events have been processed)

// Start by calling the EndProcess function of the abstract class.
// Comment this if you don't want it.
// TRestEventProcess::EndProcess();
}
void TRestDetectorHitsRotateAndTranslateProcess::EndProcess() {}

void TRestDetectorHitsRotateAndTranslateProcess::InitFromConfigFile() {
fDeltaX = GetDblParameterWithUnits("deltaX");
fDeltaY = GetDblParameterWithUnits("deltaY");
fDeltaZ = GetDblParameterWithUnits("deltaZ");

fAlpha = StringToDouble(GetParameter("alpha")); // rotation angle around Z
fBeta = StringToDouble(GetParameter("beta")); // rotation angle around Y
fGamma = StringToDouble(GetParameter("gamma")); // rotation angle around X

// Conversion to radians
fAlpha = fAlpha * TMath::Pi() / 180.;
fBeta = fBeta * TMath::Pi() / 180.;
fGamma = fGamma * TMath::Pi() / 180.;
fRotationCenter = Get3DVectorParameterWithUnits("rotationCenter", fRotationCenter);
fTranslationVector = Get3DVectorParameterWithUnits("translation", fTranslationVector);

// rotation units should be specified in the rml (e.g. "90deg")
double rotationX = GetDblParameterWithUnits("rotationX", fRotationVector.X());
double rotationY = GetDblParameterWithUnits("rotationY", fRotationVector.Y());
double rotationZ = GetDblParameterWithUnits("rotationZ", fRotationVector.Z());
fRotationVector = {rotationX, rotationY, rotationZ};

// legacy (maybe deprecated soon)
if (fTranslationVector.Mag2() == 0.0) {
fTranslationVector = {
GetDblParameterWithUnits("deltaX", 0.0), //
GetDblParameterWithUnits("deltaY", 0.0), //
GetDblParameterWithUnits("deltaZ", 0.0), //
};
}
if (fRotationVector.Mag2() == 0.0) {
fRotationVector = {
GetDblParameterWithUnits("alpha", 0.0), //
GetDblParameterWithUnits("beta", 0.0), //
GetDblParameterWithUnits("gamma", 0.0), //
};
}
}