From 7f0f7b1eb60b2f1ec4e2ed74b9ce7fe5a57c58d7 Mon Sep 17 00:00:00 2001
From: Christian Grefe <Christian.Grefe@cern.ch>
Date: Mon, 20 Jan 2014 16:00:22 +0000
Subject: [PATCH] Fixed calculation of position to bin (-1.5 to 0.5 used to
 evaluate to bin 0)

---
 .../include/DDSegmentation/Segmentation.h          |  2 ++
 DDSegmentation/src/Segmentation.cpp                | 14 +++++++++++++-
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/DDSegmentation/include/DDSegmentation/Segmentation.h b/DDSegmentation/include/DDSegmentation/Segmentation.h
index 37562a672..84f477bb1 100644
--- a/DDSegmentation/include/DDSegmentation/Segmentation.h
+++ b/DDSegmentation/include/DDSegmentation/Segmentation.h
@@ -71,6 +71,8 @@ public:
 	/// Determine the cell ID based on the position
 	virtual CellID cellID(const Position& localPosition, const Position& globalPosition,
 			const VolumeID& volumeID) const = 0;
+	/// Determine the volume ID from the full cell ID by removing all local fields
+	virtual VolumeID volumeID(const CellID& cellID) const;
 	/// Calculates the neighbours of the given cell ID and adds them to the list of neighbours
 	virtual void neighbours(const CellID& cellID, std::set<CellID>& neighbours) const;
 	/// Access the encoding string
diff --git a/DDSegmentation/src/Segmentation.cpp b/DDSegmentation/src/Segmentation.cpp
index 8b12a4c9e..2995828cd 100644
--- a/DDSegmentation/src/Segmentation.cpp
+++ b/DDSegmentation/src/Segmentation.cpp
@@ -10,6 +10,7 @@
 #include <iostream>
 #include <sstream>
 #include <stdexcept>
+#include <cmath>
 
 namespace DD4hep {
 namespace DDSegmentation {
@@ -47,6 +48,17 @@ Segmentation::~Segmentation() {
 	}
 }
 
+/// Determine the volume ID from the full cell ID by removing all local fields
+VolumeID Segmentation::volumeID(const CellID& cellID) const {
+	map<string, StringParameter>::const_iterator it;
+	_decoder->setValue(cellID);
+	for (it = _indexIdentifiers.begin(); it != _indexIdentifiers.end(); ++it) {
+		string identifier = it->second->typedValue();
+		(*_decoder)[identifier] = 0;
+	}
+	return _decoder->getValue();
+}
+
 /// Calculates the neighbours of the given cell ID and adds them to the list of neighbours
 void Segmentation::neighbours(const CellID& cellID, std::set<CellID>& neighbours) const {
 	map<string, StringParameter>::const_iterator it;
@@ -129,7 +141,7 @@ int Segmentation::positionToBin(double position, double cellSize, double offset)
 	if (cellSize == 0.) {
 		throw runtime_error("Invalid cell size: 0.0");
 	}
-	return int((position + 0.5 * cellSize - offset) / cellSize);
+	return int(floor((position + 0.5 * cellSize - offset) / cellSize));
 }
 
 } /* namespace DDSegmentation */
-- 
GitLab