diff --git a/core/base/utils/CbmGeometryUtils.cxx b/core/base/utils/CbmGeometryUtils.cxx index 5701b38c22cdba9787a8479e091bfdbd931fecfc..caa113c604a066cf23d0fc0d823ba9f5c6217ddf 100644 --- a/core/base/utils/CbmGeometryUtils.cxx +++ b/core/base/utils/CbmGeometryUtils.cxx @@ -27,6 +27,7 @@ #include <TObject.h> // for TObject #include <TString.h> // for TString, operator<, operator<< +#include <climits> #include <map> // for map #include <string.h> // for strcmp @@ -58,6 +59,61 @@ namespace Cbm LOG(info) << "****"; } + + void UniqueId() + { + /* Uniqueness is not strictly true but should be in practice. + The names are converted to Int_t with 2 billion positives followed by same negative + after overflow. If each ascii character were uniquely converted guarantee uniqueness to the + first 4 characters (256^4). I account for all characters in the name, allow overflow, + uppercase and lowercase treated same. I treat all special characters the same. + + Very unlikely names like "Rich_C02_gas+" would have same id as "rich-CO3-Gas-" which + could throw up errors in future. + */ + + TList* media = gGeoManager->GetListOfMedia(); + + TGeoMedium* med; + TGeoMaterial* mat; + + Int_t base = 27; // 26 alphabet plus 1 for all special characters + Int_t j = 0; + while (j < media->GetSize()) { // cycle through med but mat name sets id + + med = (TGeoMedium*) media->At(j); + mat = med->GetMaterial(); + + Int_t UniqueID = 0; // INT_MAX 2147483647 overflow to negative ID not so undesireable. + Int_t i = 0; + + while (i <= strlen(mat->GetName())) { + if (mat->GetName()[i] >= 'A' && mat->GetName()[i] <= 'Z') { + UniqueID += + ((Int_t) pow((double) base, (double) i) % INT_MAX) * ((Int_t) mat->GetName()[i] - 'B'); // So A=1 + } + else if (med->GetMaterial()->GetName()[i] >= 'a' && mat->GetName()[i] <= 'z') { + UniqueID += + ((Int_t) pow((double) base, (double) i) % INT_MAX) * ((Int_t) mat->GetName()[i] - 'b'); // so a=1 + } + else { + UniqueID += + ((Int_t) pow((double) base, (double) i + 1) % INT_MAX) * ((Int_t) mat->GetName()[i]); // i+1 equiv +base + }; + i++; + } + + med->SetId((UniqueID > 0) ? UniqueID : -1 * UniqueID); + mat->SetIndex((UniqueID > 0) ? UniqueID : -1 * UniqueID); // ? negative number caused issue for mat but not med + + LOG(debug) << "Reset ID number of Medium " << med->GetName() << " to " << med->GetId(); + LOG(debug) << "Reset ID number of Material " << mat->GetName() << " to " << mat->GetIndex(); + + j++; + } + } + + void RemoveDuplicateMaterials() { // Revove duplicate materials @@ -166,8 +222,11 @@ namespace Cbm } } + Cbm::GeometryUtils::RemoveDuplicateMaterials(); Cbm::GeometryUtils::RemoveDuplicateMedia(); + Cbm::GeometryUtils::UniqueId(); + if (mat) { gGeoManager->GetTopVolume()->AddNode(module1, 0, mat); } else { diff --git a/core/base/utils/CbmGeometryUtils.h b/core/base/utils/CbmGeometryUtils.h index 72b304c9c3152524df29ab1e3dca7b75167d0116..28dbe964f627aa2ba332f9c556f15b5249c240cc 100644 --- a/core/base/utils/CbmGeometryUtils.h +++ b/core/base/utils/CbmGeometryUtils.h @@ -13,6 +13,7 @@ namespace Cbm { namespace GeometryUtils { + void UniqueId(); void PrintMedia(); void PrintMaterials(); void ReAssignMediaId();