How to rename volumes in Geometry - TGeoManager

Dear all,

I have a geometry created with TGeoManager. I add each volume via the AddNode() method with no problems. What I need to do now is to delete an extension in all volumes’ names. I cannot delete the extensions before creating the volumes, as the assemblies wouldn’t be called correctly: I have a lot of parts with the same name, and the references to assemblies would be scrambled.

How do I rename all the parts after they are added to the master volume? I’ve tried to get the parts via gGeo->GetListOfVolumes(), but the size is more than twice the number of volumes I have.

A thing about the names is that I have to delete the same string size for each (in the end of the name), but the strings are different from each other.

Any inputs are more than welcome. Thanks in advance.

_ROOT Version: 6.26
_Platform: GNU/Linux - conda env
_Compiler: GCC 10.4.0

void cut_vol_names()
{
  gROOT->ProcessLine(".x $ROOTSYS/tutorials/geom/rootgeom.C");
  TIter next(gGeoManager->GetListOfVolumes());
  TGeoVolume *vol;
  while ((vol = (TGeoVolume*)next())) {
    std::string s(vol->GetName());
    printf("volume %s -> ", vol->GetName());
    // Remove the last 2 characters from the volume names
    if (s.size() > 2) s.erase(s.size() - 2);
    vol->SetName(s.c_str());
    printf("%s\n", vol->GetName());
  }
}

output:

volume TOP -> T
volume REPLICA -> REPLI
volume ROOT -> RO
volume R -> R
volume bar1 -> ba
volume bar2 -> ba
volume tub1 -> tu
volume bar3 -> ba
volume O -> O
volume bar4 -> ba
volume tub1 -> tu
volume T -> T
volume bar5 -> ba
volume bar6 -> ba
1 Like

Thanks @agheata,

I’ve tried implementing your suggestions like this:

TIter next(gGeoManager->GetListOfVolumes());
TGeoVolume *volume;
while(volume = (TGeoVolume*)next())
{
    std::string name = volume->GetName();
    if(name == "World") continue;
    else
    {
        name.erase(name.size()-13);
    }
    volume->SetName(name.c_str());
}
gGeoManager->CloseGeometry();

But when I open the .root geometry with the TBrowser, all the extensions are still there. I’ve also tried setting the loop after gGeoManager->CloseGeometry(), but I get the same result. I’m skipping “World” as it doesn’t have the extension.

Am I doing something wrong here?

Thanks again for the help.

Hi again,

Well, it actually seems to work.

The funny thing is that in TBrowser the “Master Volume” and “Top Volume” trees (see figure below) show the parts without the extension (great!). But in the “World_1” tree all the parts still have the extension.

ROOTfile_tree

I guess it works :smiley:

Many thanks for the help. Cheers!

Names with copy number extensions like World_1 are not volume names, but node names. Nodes are volumes placed in another volume, so you see the nodes tree in the browser starting from the world. If you want to change those as well in the same while, you would need to add another loop:

for (auto i = 0; i < volume.GetNdaughters(); ++i) {
  auto node = volume->GetNode[i];
  name = node->GetVolume()->GetName();
  // Nodes have the same name as the volume they represent,
  // but appending _copynumber so you can apply the same
  // truncation as the one for volumes, and append after the copy number
 // === apply truncation here to name ===
 name += std::string("_") + std::to_string(node->GetNumber());
 node->SetName(name);
}
1 Like

Super! Many thanks. I needed that last change so the volumes are shown without the extensions in the TEveManager.