6 Commits

Author SHA1 Message Date
Paolo Andreetto
b7c775ee35 Error handling 2023-02-22 11:20:56 +01:00
Paolo Andreetto
7bc4932d09 Missing component for VTK 2023-02-20 16:10:10 +01:00
Paolo Andreetto
8832f47e75 Fixed build for VTK on alma9 2023-02-20 16:08:30 +01:00
Paolo Andreetto
043a44150c New writer class for the skin detector 2023-02-17 14:35:31 +01:00
Paolo Andreetto
fce2a39393 Changed version 2023-01-17 10:36:34 +01:00
Paolo Andreetto
d223a3a308 Restored classes for Castor 2023-01-17 10:36:05 +01:00
9 changed files with 279 additions and 140 deletions

View File

@@ -11,7 +11,7 @@ project(uLib)
# The version number.
set(PROJECT_VERSION_MAJOR 0)
set(PROJECT_VERSION_MINOR 4)
set(PROJECT_VERSION_MINOR 5)
set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}")
set(PROJECT_SOVERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}")
@@ -79,8 +79,25 @@ include(${EIGEN3_USE_FILE})
find_package(ROOT CONFIG REQUIRED)
include(${ROOT_USE_FILE})
find_package(VTK CONFIG REQUIRED)
include(${VTK_USE_FILE})
find_package(VTK REQUIRED
COMPONENTS CommonColor
CommonCore
FiltersCore
FiltersModeling
FiltersSources
IOLegacy
IOXML
IOXMLParser
ImagingCore
InteractionStyle
InteractionWidgets
RenderingAnnotation
RenderingContextOpenGL2
RenderingCore
RenderingFreeType
RenderingGL2PSOpenGL2
RenderingOpenGL2
RenderingVolumeOpenGL2)
set(CMAKE_REQUIRED_INCLUDES CMAKE_REQUIRED_INCLUDES math.h)
set(CMAKE_REQUIRED_LIBRARIES CMAKE_REQUIRED_LIBRARIES m)

View File

@@ -25,11 +25,12 @@
#include <iostream>
#include <unordered_map>
#include "VoxRaytracer.h"
#include "Utils.h"
#define unlikely(expr) __builtin_expect(!!(expr), 0)
inline float fast_sign(float f) { return 1 - 2 * (f < 0); }
namespace uLib {
@@ -48,17 +49,16 @@ void VoxRaytracer::RayData::AddElement(Id_t id, float L)
void VoxRaytracer::RayData::AppendRay(const VoxRaytracer::RayData &in)
{
if (!in.m_Data.size())
{
if (unlikely(!in.m_Data.size())) {
std::cout << "Warinig: PoCA on exit border!\n";
return;
}
else if (!m_Data.size())
{
else if (unlikely(!m_Data.size())) {
m_Data = in.m_Data;
std::cout << "Warinig: PoCA on entrance border!\n";
return;
}
else
{
else {
// Opzione 1) un voxel in piu' //
m_Data.reserve(m_Data.size() + in.m_Data.size());
m_Data.insert(m_Data.end(), in.m_Data.begin(), in.m_Data.end());
@@ -82,22 +82,16 @@ void VoxRaytracer::RayData::AppendRay(const VoxRaytracer::RayData &in)
void VoxRaytracer::RayData::PrintSelf(std::ostream &o)
{
o << "Ray: total lenght " << m_TotalLength << "\n";
for(std::vector<Element>::iterator it = m_Data.begin(); it < m_Data.end(); ++it)
{
std::vector<Element>::iterator it;
for(it = m_Data.begin(); it < m_Data.end(); ++it)
o << "[ " << (*it).vox_id << ", " << (*it).L << "] \n";
}
}
////////////////////////////////////////////////////////////////////////////////
//// RAY TRACER ////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
VoxRaytracer::VoxRaytracer(StructuredGrid &image) : m_Image(&image)
{
m_scale << (m_Image->GetWorldMatrix() * Vector4f(1,0,0,0)).norm(),
(m_Image->GetWorldMatrix() * Vector4f(0,1,0,0)).norm(),
(m_Image->GetWorldMatrix() * Vector4f(0,0,1,0)).norm();
}
bool VoxRaytracer::GetEntryPoint(const HLine3f &line, HPoint3f &pt)
{
@@ -156,7 +150,9 @@ bool VoxRaytracer::GetExitPoint(const HLine3f &line, HPoint3f &pt)
}
VoxRaytracer::RayData VoxRaytracer::TraceBetweenPoints(const HPoint3f &in, const HPoint3f &out) const
VoxRaytracer::RayData VoxRaytracer::TraceBetweenPoints(const HPoint3f &in,
const HPoint3f &out)
const
{
RayData ray;
Vector4f pt1 = m_Image->GetLocalPoint(in);
@@ -166,33 +162,39 @@ VoxRaytracer::RayData VoxRaytracer::TraceBetweenPoints(const HPoint3f &in, const
float l = s.head(3).norm();
Vector3f L(l/s(0), l/s(1), l/s(2));
// Vector3f scale; // FIXXX
// scale << (m_Image->GetWorldMatrix() * Vector4f(1,0,0,0)).norm(),
// (m_Image->GetWorldMatrix() * Vector4f(0,1,0,0)).norm(),
// (m_Image->GetWorldMatrix() * Vector4f(0,0,1,0)).norm();
Vector3f offset;
for(int i=0;i<3;++i)
{
offset(i) = (s(i)>=0) - (pt1(i)-floor(pt1(i)));
}
for(int i=0;i<3;++i) offset(i) = (s(i)>=0) - (pt1(i)-floor(pt1(i))) ;
offset = offset.cwiseProduct(L).cwiseAbs();
L = L.cwiseAbs();
//---- Check if the ray only crosses one voxel
Vector3i vid = m_Image->Find(in);
if (vid == m_Image->Find(out))
{
if(vid == m_Image->Find(out)){
ray.AddElement(m_Image->Map(vid),s.norm());
return ray;
}
//---- Otherwise, loop until ray is finished
int id; float d;
while (l>0)
{
while(l>0){
d = offset.minCoeff(&id);
if (m_Image->IsInsideGrid(vid))
{
if(m_Image->IsInsideGrid(vid)){
ray.AddElement(m_Image->Map(vid), d * m_scale(id) );
}
// nan check //
// if(unlikely(!isFinite(d * scale(id)))) {
// std:: cout << "NAN in raytracer\n";
// exit(1);
// }
vid(id) += (int)fast_sign(s(id));
l -= d;
@@ -202,102 +204,11 @@ VoxRaytracer::RayData VoxRaytracer::TraceBetweenPoints(const HPoint3f &in, const
return ray;
}
static int encode_v(Vector3i in)
{
return ((in[0] + 1) << 4) + ((in[1] + 1) << 2) + in[2] + 1;
}
static Vector3i decode_v(int in)
{
Vector3i result {
((in & 48) >> 4) - 1,
((in & 12) >> 2) - 1,
(in & 3) - 1
};
return result;
}
VoxRaytracer::RayData VoxRaytracer::BeamBetweenPoints(const HPoint3f &in, const HPoint3f &out, Vector3i thickness) const
{
if (thickness[0] < 0) thickness[0] = 0;
if (thickness[1] < 0) thickness[1] = 0;
if (thickness[2] < 0) thickness[2] = 0;
Vector3i zero_v { 0, 0, 0 };
RayData ray = TraceBetweenPoints(in, out);
if (thickness == zero_v || ray.Data().size() == 0) return ray;
/*
* Calculate the forbidden relocations
*/
std::unordered_map<int, int> ban_points(26);
Vector3i prevPos = m_Image->UnMap(ray.Data()[0].vox_id);
Vector3i currDir = zero_v;
int currLen = 1;
for (int k = 1; k < ray.Data().size(); k++)
{
Vector3i currPos = m_Image->UnMap(ray.Data()[k].vox_id);
Vector3i offset = currPos - prevPos;
prevPos = currPos;
if (k == 1) currDir = offset;
if (offset == currDir)
{
currLen++;
continue;
}
int enc_v = encode_v(currDir);
if (ban_points.find(enc_v) == ban_points.end())
{
ban_points.emplace(enc_v, currLen);
}
else if (currLen > ban_points[enc_v])
{
ban_points[enc_v] = currLen;
}
currDir = offset;
currLen = 2;
}
/*
* Calculate the beam section
*/
std::vector<Vector3i> relocs;
relocs.push_back(zero_v);
/*
* Compose the beam
*/
RayData beam;
for (auto iter : ray.Data())
{
Vector3i rPos = m_Image->UnMap(iter.vox_id);
for (Vector3i reloc : relocs)
{
Vector3i cPos = rPos + reloc;
if (!m_Image->IsInsideGrid(cPos)) continue;
beam.AddElement(m_Image->Map(cPos), iter.L);
}
}
return beam;
}
// 20150528 SV for absorbed muons
VoxRaytracer::RayData VoxRaytracer::TraceLine(const HLine3f &line) const
{
RayData ray;
Vector4f pt = m_Image->GetLocalPoint(line.origin);
Vector4f s = m_Image->GetLocalPoint(line.direction);
@@ -339,4 +250,4 @@ VoxRaytracer::RayData VoxRaytracer::TraceLine(const HLine3f &line) const
return ray;
}
} //end of namespace uLib
}

View File

@@ -38,8 +38,7 @@ namespace uLib {
class VoxRaytracer {
public:
class RayData
{
class RayData {
public:
RayData() : m_TotalLength(0) {}
@@ -63,8 +62,14 @@ public:
Scalarf m_TotalLength;
};
public:
VoxRaytracer(StructuredGrid &image);
VoxRaytracer(StructuredGrid &image) : m_Image(&image) {
m_scale <<
(m_Image->GetWorldMatrix() * Vector4f(1,0,0,0)).norm(),
(m_Image->GetWorldMatrix() * Vector4f(0,1,0,0)).norm(),
(m_Image->GetWorldMatrix() * Vector4f(0,0,1,0)).norm();
}
bool GetEntryPoint(const HLine3f &line, HPoint3f &pt);
@@ -72,8 +77,6 @@ public:
RayData TraceBetweenPoints(const HPoint3f &in, const HPoint3f &out) const;
RayData BeamBetweenPoints(const HPoint3f &in, const HPoint3f &out, Vector3i thickness) const;
RayData TraceLine(const HLine3f &line) const;
inline StructuredGrid* GetImage() const { return this->m_Image; }

View File

@@ -1,12 +1,43 @@
set(HEADERS RootMathDense.h
RootMuonScatter.h
RootHitRaw.h)
RootHitRaw.h
muCastorMCTrack.h
muCastorHit.h
muCastorInfo.h
muCastorSkinHit.h
SkinDetectorWriter.h)
set(SOURCES ${HEADERS} RootMuonScatter.cpp)
set(SOURCES ${HEADERS} RootMuonScatter.cpp
muCastorMCTrack.cpp
muCastorHit.cpp
muCastorInfo.cpp
muCastorSkinHit.cpp
SkinDetectorWriter.cpp)
set(DICTIONARY_HEADERS muCastorMCTrack.h
muCastorHit.h
muCastorInfo.h
muCastorSkinHit.h
SkinDetectorWriter.h)
set(LIBRARIES ${ROOT_LIBRARIES}
${PACKAGE_LIBPREFIX}Math)
set(rDictName ${PACKAGE_LIBPREFIX}RootDict)
root_generate_dictionary(${rDictName} ${DICTIONARY_HEADERS}
LINKDEF Linkdef.h)
set_source_files_properties(${rDictName}.cxx
PROPERTIES GENERATED TRUE)
set_source_files_properties(${rDictName}.h
PROPERTIES GENERATED TRUE)
list(APPEND SOURCES ${rDictName}.cxx)
# TODO use a custom target linked to root_generate_dictionary
set(R_ARTIFACTS ${CMAKE_CURRENT_BINARY_DIR}/lib${rDictName}_rdict.pcm
${CMAKE_CURRENT_BINARY_DIR}/lib${rDictName}.rootmap)
install(FILES ${R_ARTIFACTS}
DESTINATION ${PACKAGE_INSTALL_LIB_DIR})
set(libname ${PACKAGE_LIBPREFIX}Root)
set(ULIB_SHARED_LIBRARIES ${ULIB_SHARED_LIBRARIES} ${libname} PARENT_SCOPE)
set(ULIB_SELECTED_MODULES ${ULIB_SELECTED_MODULES} Root PARENT_SCOPE)

View File

@@ -79,6 +79,9 @@ using namespace ROOT::Mutom;
#pragma link C++ class muCastorMCTrack+;
#pragma link C++ class muCastorHit+;
#pragma link C++ class muCastorInfo+;
#pragma link C++ class muCastorSkinHit+;
#pragma link C++ class SkinDetectorWriter+;
#endif // __CINT__

View File

@@ -0,0 +1,47 @@
#include "SkinDetectorWriter.h"
#include "muCastorSkinHit.h"
#include "TVector3.h"
SkinDetectorWriter::SkinDetectorWriter(string filename) :
t_file(nullptr),
t_tree(nullptr),
t_buffer(nullptr),
i_status(0)
{
t_file = new TFile(filename.c_str(), "RECREATE");
t_tree = new TTree("muCastorMC", "muCastorMC");
t_buffer = new TClonesArray("muCastorSkinHit");
t_tree->Branch("CastorSkinHits", "TClonesArray", t_buffer, 32000, 99);
if (t_file->IsZombie()) i_status = 1;
}
SkinDetectorWriter::~SkinDetectorWriter()
{}
void SkinDetectorWriter::add(int detID, float p_x, float p_y, float p_z,
float m_x, float m_y, float m_z)
{
TClonesArray& ref = *t_buffer;
int size = ref.GetEntriesFast();
muCastorSkinHit* new_hit = new(ref[size]) muCastorSkinHit();
new_hit->SetDetID(detID);
new_hit->SetPdgCode(13);
new_hit->SetMotherID(-1);
new_hit->SetPos (TVector3(p_x, p_y, p_z));
new_hit->SetMom (TVector3(m_x, m_y, m_z));
}
void SkinDetectorWriter::write()
{
if (t_tree->Fill() < 0) i_status = 2;
t_buffer->Delete(); // or t_buffer->Clear() ??
}
void SkinDetectorWriter::close()
{
if (t_tree->Write() == 0) i_status = 3;
t_file->Close();
}

View File

@@ -0,0 +1,32 @@
#ifndef SkinDetectorWriter_h
#define SkinDetectorWriter_h
#include <string>
#include "TFile.h"
#include "TTree.h"
#include "TClonesArray.h"
using std::string;
class SkinDetectorWriter
{
public:
SkinDetectorWriter(string filename);
virtual ~SkinDetectorWriter();
void add(int detID, float p_x, float p_y, float p_z, float m_x, float m_y, float m_z);
int status() { return i_status; }
void write();
void close();
private:
TFile* t_file;
TTree* t_tree;
TClonesArray* t_buffer;
int i_status;
};
#endif //SkinDetectorWriter_h

View File

@@ -0,0 +1,43 @@
//----------------------------------------------------------
// Class : CastorSkinHit
// Date: October 2020
// Author: Germano Bonomi germano.bonomi@unibs.it
//----------------------------------------------------------
#include <iostream>
#include "muCastorSkinHit.h"
/// \cond CLASSIMP
ClassImp(muCastorSkinHit)
/// \endcond
using namespace std;
//_____________________________________________________________________________
muCastorSkinHit::muCastorSkinHit() :
fDetID(-1),
fPdgCode(-1),
fMotherID(-1),
fMomX(0.),
fMomY(0.),
fMomZ(0.),
fPosX(0.),
fPosY(0.),
fPosZ(0.)
{}
//_____________________________________________________________________________
muCastorSkinHit::~muCastorSkinHit()
{}
//_____________________________________________________________________________
void muCastorSkinHit::Print(const Option_t* /*opt*/) const
{
cout << " detID: " << fDetID
<< " position (cm): ("
<< fPosX << ", " << fPosY << ", " << fPosZ << ")"
<< " momentum (MeV/c): ("
<< fMomX << ", " << fMomY << ", " << fMomZ << ")"
<< endl;
}

View File

@@ -0,0 +1,52 @@
//----------------------------------------------------------
// Class : CastorSkinHit
// Date: October 2020
// Author: Germano Bonomi germano.bonomi@unibs.it
//----------------------------------------------------------
#ifndef muCastor_SKINHIT_H
#define muCastor_SKINHIT_H
#include <TObject.h>
#include <TVector3.h>
class muCastorSkinHit : public TObject
{
public:
muCastorSkinHit();
virtual ~muCastorSkinHit();
// methods
virtual void Print(const Option_t* option = "") const;
// set methods
void SetDetID(Int_t id) { fDetID = id; };
void SetPdgCode(Int_t pdg) { fPdgCode = pdg; };
void SetMotherID(Int_t mid) { fMotherID = mid; };
void SetMom(TVector3 xyz) { fMomX = xyz.X(); fMomY = xyz.Y(); fMomZ = xyz.Z(); };
void SetPos(TVector3 xyz) { fPosX = xyz.X(); fPosY = xyz.Y(); fPosZ = xyz.Z(); };
Int_t GetDetID() { return fDetID; }
Int_t GetPdgCode() { return fPdgCode; }
Int_t GetMotherID() { return fMotherID; }
TVector3 GetMom() { return TVector3(fMomX, fMomY, fMomZ); }
TVector3 GetPos() { return TVector3(fPosX, fPosY, fPosZ); }
private:
Int_t fDetID; // Detector module ID
Int_t fPdgCode; // Particle PDG Code
Int_t fMotherID; // Particle mother ID (-1 = primary, 0 = secondary, etc..)
Double_t fMomX; // Track momentum when releasing the hit (X)
Double_t fMomY; // Track momentum when releasing the hit (Y)
Double_t fMomZ; // Track momentum when releasing the hit (Z)
Double_t fPosX; // Hit coordinates (at the entrance of the detector) (X)
Double_t fPosY; // Hit coordinates (at the entrance of the detector) (Y)
Double_t fPosZ; // Hit coordinates (at the entrance of the detector) (Z)
ClassDef(muCastorSkinHit,1) //muCastorSkinHit
};
#endif //muCastort_SKINHIT_H