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

Added implementation for hexagonal segmentation and also staggered square segmentations #1161

Merged
merged 40 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
7754e37
Added hex and staggered square segmentations src
sebouh137 Aug 15, 2023
83eb406
Added .h for hex and staggered square segmentation
sebouh137 Aug 15, 2023
8be52c6
Update HexGrid.h
sebouh137 Aug 15, 2023
8ec6ab6
Update SquareGrid.h
sebouh137 Aug 15, 2023
9012472
Update DDCore/src/segmentations/HexGrid.cpp
sebouh137 Aug 25, 2023
195024a
Update DDCore/src/segmentations/HexGrid.cpp
sebouh137 Aug 25, 2023
b1f76eb
Update DDCore/src/segmentations/SquareGrid.cpp
sebouh137 Aug 25, 2023
22576d0
Update DDCore/src/segmentations/SquareGrid.cpp
sebouh137 Aug 25, 2023
84373a2
Update HexGrid.cpp
sebouh137 Aug 25, 2023
ade020d
Update HexGrid.cpp
sebouh137 Aug 25, 2023
9b7833c
Update SquareGrid.cpp
sebouh137 Aug 25, 2023
2ef97dd
Update CartesianGridXY.cpp
sebouh137 Aug 31, 2023
18151d1
Update CartesianGridXY.h
sebouh137 Sep 5, 2023
7d9268e
Update CartesianGridXY.cpp
sebouh137 Sep 5, 2023
ae359af
Update CartesianGridXY.cpp
sebouh137 Sep 5, 2023
8ca1c92
Update HexGrid.cpp
sebouh137 Sep 5, 2023
1375ffa
Update CartesianGridXY.cpp
sebouh137 Sep 5, 2023
d128b9a
Update CartesianGridXY.cpp
sebouh137 Sep 5, 2023
f2dc23a
Update HexGrid.cpp
sebouh137 Sep 5, 2023
6546b6b
Update HexGrid.h
sebouh137 Sep 5, 2023
bcefcd1
Update CartesianGridXY.h
sebouh137 Sep 5, 2023
9851acd
Update HexGrid.h
sebouh137 Sep 5, 2023
1b8a78f
Update HexGrid.cpp
sebouh137 Sep 5, 2023
edc80c1
Update CartesianGridXY.cpp
sebouh137 Sep 5, 2023
d1e90a2
removed SquareGrid.h and SquareGrid.cpp, which became redundant when …
sebouh137 Sep 5, 2023
c90594c
Update DDCore/include/DDSegmentation/HexGrid.h
sebouh137 Sep 5, 2023
45e4385
Update HexGrid.cpp
sebouh137 Sep 5, 2023
9816e48
Update CartesianGridXY.cpp
sebouh137 Sep 5, 2023
9ce45fb
Update CartesianGridXY.cpp
sebouh137 Sep 5, 2023
e2ed050
...
sebouh137 Sep 6, 2023
c07dfd4
Update CartesianGridXY.cpp
sebouh137 Sep 12, 2023
c16d729
Update HexGrid.cpp
sebouh137 Sep 12, 2023
551fe45
Update HexGrid.cpp
sebouh137 Sep 12, 2023
898186b
CartesianGridXY: add missing parenthesis
andresailer Sep 12, 2023
18146de
added CartesianGridXYStaggered.cpp and .h
sebouh137 Sep 15, 2023
1138508
reverted CartesianGridXY.h and CartesianGridXY.cpp to the upstream ma…
sebouh137 Sep 15, 2023
385d753
merged
sebouh137 Sep 15, 2023
4aec387
fixed merge
sebouh137 Sep 15, 2023
dccd89f
CartesianGridXYStaggered: fix cellDimensions function declaration
andresailer Sep 19, 2023
87689b9
Update CartesianGridXYStaggered.cpp
sebouh137 Sep 19, 2023
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
121 changes: 121 additions & 0 deletions DDCore/include/DDSegmentation/HexGrid.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
//==========================================================================
// AIDA Detector description implementation
//--------------------------------------------------------------------------
// Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
// All rights reserved.
//
// For the licensing terms see $DD4hepINSTALL/LICENSE.
// For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
//
//==========================================================================

/*
* HexGrid.h
*
* Created on: August 9, 2023
* Author: Sebouh J. Paul, UC Riverside
*/

#ifndef DDSEGMENTATION_HEXGRID_H
#define DDSEGMENTATION_HEXGRID_H

#include "DDSegmentation/Segmentation.h"

namespace dd4hep {
namespace DDSegmentation {

/// Segmentation base class describing hexagonal grid segmentation, with or without staggering
class HexGrid: public Segmentation {
public:
/// Destructor
virtual ~HexGrid();
//protected:
/// Default constructor used by derived classes passing the encoding string
HexGrid(const std::string& cellEncoding = "");
/// Default constructor used by derived classes passing an existing decoder
HexGrid(const BitFieldCoder* decoder);

/// determine the position based on the cell ID
virtual Vector3D position(const CellID& cellID) const;
/// determine the cell ID based on the position
virtual CellID cellID(const Vector3D& localPosition, const Vector3D& globalPosition, const VolumeID& volumeID) const;
// access the stagger mode: 0=no stagger; 1=stagger cycling through 3 offsets
int stagger() const {
return _stagger;
}

/// access the grid size
double sideLength() const {
return _sideLength;
}
/// access the coordinate offset in X
double offsetX() const {
return _offsetX;
}
/// access the coordinate offset in Y
double offsetY() const {
return _offsetY;
}
/// access the field name used for X
const std::string& fieldNameX() const {
return _xId;
}
/// access the field name used for Y
const std::string& fieldNameY() const {
return _yId;
}

/// set the stagger mode: 0=no stagger; 1=stagger cycling through 3 offsets
void setStagger(int stagger) {
_stagger= stagger;
}
/// set the grid size in X
void setSideLength(double cellSize) {
_sideLength = cellSize;
}
/// set the coordinate offset in X
void setOffsetX(double offset) {
_offsetX = offset;
}
/// set the coordinate offset in Y
void setOffsetY(double offset) {
_offsetY = offset;
}
/// set the field name used for X
void setFieldNameX(const std::string& fieldName) {
_xId = fieldName;
}
/// set the field name used for Y
void setFieldNameY(const std::string& fieldName) {
_yId = fieldName;
}
/** \brief Returns a vector<double> of the cellDimensions of the given cell ID
in natural order of dimensions, e.g., dx/dy/dz, or dr/r*dPhi

Returns a vector of the cellDimensions of the given cell ID
\param cellID is ignored as all cells have the same dimension
\return std::vector<double> size 2:
-# size in x
-# size in y
*/
virtual std::vector<double> cellDimensions(const CellID& cellID) const;

protected:
/// the stagger mode: 0=off ; 1=cycle through 3 different offsets (H3)
// 2=cycle through 4 differnt offsets (H4)
int _stagger;
/// the length of one side of a hexagon
double _sideLength;
/// the coordinate offset in X
double _offsetX;
/// the coordinate offset in Y
double _offsetY;
/// the field name used for X
std::string _xId;
/// the field name used for Y
std::string _yId;
};

} /* namespace DDSegmentation */
} /* namespace dd4hep */
#endif // DDSEGMENTATION_HEXGRID_H
121 changes: 121 additions & 0 deletions DDCore/include/DDSegmentation/SquareGrid.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
//==========================================================================
// AIDA Detector description implementation
//--------------------------------------------------------------------------
// Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
// All rights reserved.
//
// For the licensing terms see $DD4hepINSTALL/LICENSE.
// For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
//
//==========================================================================

/*
* SquareGrid.h
*
* Created on: August 9, 2023
* Author: Sebouh J. Paul, UC Riverside
*/

#ifndef DDSEGMENTATION_SQUAREGRID_H
#define DDSEGMENTATION_SQUAREGRID_H

#include "DDSegmentation/Segmentation.h"

namespace dd4hep {
namespace DDSegmentation {

/// Segmentation base class describing a cartesian grid segmentation with square
/// cells, with or without staggering
class SquareGrid: public Segmentation {
public:
/// Destructor
virtual ~SquareGrid();
//protected:
/// Default constructor used by derived classes passing the encoding string
SquareGrid(const std::string& cellEncoding = "");
/// Default constructor used by derived classes passing an existing decoder
SquareGrid(const BitFieldCoder* decoder);

/// determine the position based on the cell ID
virtual Vector3D position(const CellID& cellID) const;
/// determine the cell ID based on the position
virtual CellID cellID(const Vector3D& localPosition, const Vector3D& globalPosition, const VolumeID& volumeID) const;
// access the stagger mode: 0=no stagger; 1=stagger cycling through 3 offsets
int stagger() const {
return _stagger;
}

/// access the grid size
double sideLength() const {
return _sideLength;
}
/// access the coordinate offset in X
double offsetX() const {
return _offsetX;
}
/// access the coordinate offset in Y
double offsetY() const {
return _offsetY;
}
/// access the field name used for X
const std::string& fieldNameX() const {
return _xId;
}
/// access the field name used for Y
const std::string& fieldNameY() const {
return _yId;
}

/// set the stagger mode: 0=no stagger; 1=stagger cycling through 3 offsets
void setStagger(int stagger) {
_stagger= stagger;
}
/// set the grid size in X
void setSideLength(double cellSize) {
_sideLength = cellSize;
}
/// set the coordinate offset in X
void setOffsetX(double offset) {
_offsetX = offset;
}
/// set the coordinate offset in Y
void setOffsetY(double offset) {
_offsetY = offset;
}
/// set the field name used for X
void setFieldNameX(const std::string& fieldName) {
_xId = fieldName;
}
/// set the field name used for Y
void setFieldNameY(const std::string& fieldName) {
_yId = fieldName;
}
/** \brief Returns a vector<double> of the cellDimensions of the given cell ID
in natural order of dimensions, e.g., dx/dy/dz, or dr/r*dPhi

Returns a vector of the cellDimensions of the given cell ID
\param cellID is ignored as all cells have the same dimension
\return std::vector<double> size 2:
-# size in x
-# size in y
*/
virtual std::vector<double> cellDimensions(const CellID& cellID) const;

protected:
/// the stagger mode: 0=off ; 1=cycle through 2 differnt offsets
int _stagger;
/// the length of one side of a square
double _sideLength;
/// the coordinate offset in X
double _offsetX;
/// the coordinate offset in Y
double _offsetY;
/// the field name used for X
std::string _xId;
/// the field name used for Y
std::string _yId;
};

} /* namespace DDSegmentation */
} /* namespace dd4hep */
#endif // DDSEGMENTATION_SQUAREGRID_H
150 changes: 150 additions & 0 deletions DDCore/src/segmentations/HexGrid.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
andresailer marked this conversation as resolved.
Show resolved Hide resolved
* HexGrid.cpp
*
* Created on: August 9, 2023
* Author: Sebouh J. Paul, UC Riverside
*/
#include "DD4hep/Factories.h"
#include "HexGrid.h"
sebouh137 marked this conversation as resolved.
Show resolved Hide resolved

namespace dd4hep {
namespace DDSegmentation {

/// Default constructor used by derived classes passing the encoding string
HexGrid::HexGrid(const std::string& cellEncoding) :
Segmentation(cellEncoding) {
_type = "HexGridXY";
_description = "Hexagonal segmentation in the local XY-plane";
andresailer marked this conversation as resolved.
Show resolved Hide resolved

// register all necessary parameters
registerParameter("stagger", "stagger mode", _stagger, 1);
registerParameter("side_length", "Cell size", _sideLength, 1., SegmentationParameter::LengthUnit);
registerParameter("offset_x", "Cell offset in X", _offsetX, 0., SegmentationParameter::LengthUnit, true);
registerParameter("offset_y", "Cell offset in Y", _offsetY, 0., SegmentationParameter::LengthUnit, true);
registerIdentifier("identifier_x", "Cell ID identifier for X", _xId, "x");
registerIdentifier("identifier_y", "Cell ID identifier for Y", _yId, "y");
}

/// Default constructor used by derived classes passing an existing decoder
HexGrid::HexGrid(const BitFieldCoder* decode) : Segmentation(decode) {
// define type and description
_type = "HexGridXY";
_description = "Hexagonal segmentation in the local XY-plane";

// register all necessary parameters
registerParameter("stagger", "stagger mode", _stagger, 1);
registerParameter("side_length", "Cell size", _sideLength, 1., SegmentationParameter::LengthUnit);
registerParameter("offset_x", "Cell offset in X", _offsetX, 0., SegmentationParameter::LengthUnit, true);
registerParameter("offset_y", "Cell offset in Y", _offsetY, 0., SegmentationParameter::LengthUnit, true);
registerIdentifier("identifier_x", "Cell ID identifier for X", _xId, "x");
registerIdentifier("identifier_y", "Cell ID identifier for Y", _yId, "y");

}

/// Destructor
HexGrid::~HexGrid() {
}

/// determine the position based on the cell ID
Vector3D HexGrid::position(const CellID& cID) const {
int layer= _decoder->get(cID,"layer");
Vector3D cellPosition;
cellPosition.X = _decoder->get(cID,_xId )*1.5*_sideLength+_offsetX+_sideLength/2.;
cellPosition.Y = _decoder->get(cID,_yId )*std::sqrt(3)/2.*_sideLength+ _offsetY+_sideLength*std::sqrt(3)/2.;
if (_stagger==0)
cellPosition.X+=_sideLength;
else if (_stagger==1)
cellPosition.X+=(layer%3)*_sideLength;
else if (_stagger==2){
switch (layer%4){
case 0:
cellPosition.X-=0.75*_sideLength;
break;
case 1:
//cellPosition.X+=0.75*_sideLength;
cellPosition.Y+=std::sqrt(3)/4*_sideLength;
break;
case 2:
//cellPosition.X-=0.75*_sideLength;
cellPosition.Y-=std::sqrt(3)/4*_sideLength;
break;
case 3:
cellPosition.X+=0.75*_sideLength;
break;
}
}
return cellPosition;
}

inline double positive_modulo(double i, double n) {
return std::fmod(std::fmod(i,n) + n,n);
}

//inline double positive_floor(
sebouh137 marked this conversation as resolved.
Show resolved Hide resolved

/// determine the cell ID based on the position
CellID HexGrid::cellID(const Vector3D& localPosition, const Vector3D& /* globalPosition */, const VolumeID& vID) const {
CellID cID = vID ;
int layer= _decoder->get(cID,"layer");
double _gridSizeY=std::sqrt(3)*_sideLength/2.;
sebouh137 marked this conversation as resolved.
Show resolved Hide resolved
double _gridSizeX=3*_sideLength/2;

double x=localPosition.X-_offsetX;
double y=localPosition.Y-_offsetY;
if (_stagger==0)
x-=_sideLength;
else if (_stagger==1)
x-=(layer%3)*_sideLength;
else if (_stagger==2){
switch (layer%4){
case 0:
x+=0.75*_sideLength;
break;
case 1:
//x-=0.75*_sideLength;
y-=std::sqrt(3)/4*_sideLength;
break;
case 2:
//x+=0.75*_sideLength;
y+=std::sqrt(3)/4*_sideLength;
break;
case 3:
x-=0.75*_sideLength;
break;
}
}

double a=positive_modulo(y/(std::sqrt(3)*_sideLength),1);
double b=positive_modulo(x/(3*_sideLength),1);
int ix = std::floor(x/(3*_sideLength/2.))+
(b<0.5)*(-abs(a-.5)<(b-.5)*3)+(b>0.5)*(abs(a-.5)-.5<(b-1)*3);
int iy=std::floor(y/(std::sqrt(3)*_sideLength/2.));
iy-=(ix+iy)&1;


//int ix=int(floor((localPosition.X - _offsetX) / (3/2*_sideLength)));
andresailer marked this conversation as resolved.
Show resolved Hide resolved
//int iy=int(floor((localPosition.Y - _offsetY) / (std::sqrt(3)/2*_sideLength)));
//if((ix+iy)%2 &&
// ((localPosition.X-offset_X)/(2*_sideLength)+(localPosition.Y-_offsetX)/(std::sqrt(3)*_sideLength))
//iy-=1;

_decoder->set( cID,_xId, ix );
_decoder->set( cID,_yId, iy );
return cID ;
}

std::vector<double> HexGrid::cellDimensions(const CellID&) const {
#if __cplusplus >= 201103L
return {2*_sideLength, std::sqrt(3)*_sideLength};
#else
std::vector<double> cellDims(2,0.0);
cellDims[0] = 2*_sideLength;
cellDims[1] = std::sqrt(3)*_sideLength;
return cellDims;
#endif
}

} /* namespace DDSegmentation */
} /* namespace dd4hep */

DECLARE_SEGMENTATION(HexGrid, create_segmentation<dd4hep::DDSegmentation::HexGrid>)
Loading