Geant4 int to string problem

Please fill out the following information to help in answering your question, and also see tips for posting codeghsnippets. If you don’t provide this information it will take more time to help with your problem!

Geant4 Version: geant4_10_07_p03
Operating System: mint
Compiler/Version: gcc version 7.5.0
CMake Version: cmake version 3.10.2


Hello, dear all!

I need everybody very well!

I have a problem:

i need convertation from int-variable to string-variable in geant4. i have know so do it in c++: using tostring and itoa command, but i do not understand do it using geant4.

help me, please!

There are several ways. I don’t know about tostring and itoa - they look like C, not C++, to me. But OK, they should still work.

The G4 command source code offers some static functions - see G4UIcommand.hh.

I always use std::ostringstream (you need #include <sstream>):

std::ostringstream oss;
oss << i;

Then the result is in oss.str() as an std::string. A C-string is in oss.str().c_str(). Google “C++ string” for all properties.

This is useful if you want to make a string such as:

oss << “The number of events is “ << i;

Note: this is just C++. Not special about Geant4.

You’re writing C++ code anyway, so you can certainly just use std::tostring, or itoa from old-style C. You could also use stringstream, and format the output of your numbers, as John Allison pointed out.

Solved. yes, i did this:

 G4String beltName [100];
        for( G4int kl; kl<100; kl++ ) { 
        std::ostringstream mystr;
        mystr<<kl<<std::endl;
        beltName[kl]= "solid_inner_tyvek" + G4String(mystr.str());
        // G4cout<<"\n "<<beltName[kl]<<" i= "<<kl<<std::endl;
        }

Thank for your attantion and answeres!

But i have new prodlem:

How i can declaretion this object in geant4 - G4Tube with my string variable massive(beltIn[i][j])?

G4Tubs beltIn[0][1] = new G4Tubs(“inner_tyvek11”, 0.51254mm, 0.51254.4mm, 0.5200mm, startAngle, spanningAngle);

and i have this error from compilator:

error: conflicting declaration ‘G4Tubs beltIn[0][1]’
G4Tubs beltIn[0][1] = new G4Tubs(“inner_tyvek11”, 0.51254mm, 0.51254.4mm, 0.5200mm, startAngle, spanningAngle);

How i can massive-name for declaration G4Tube?

Mmm.

I take it you have many G4Tubs to which you want to give a distinctive name, dimensions and position.

I don’t think there’s need to keep the pointers to the G4Tubs yourself. Geant4 will do it for you.

I assume this is in a loop of some sort.

When you create your G4Tubsobjects with your distinctive name and dimensions and positions in a loop, just new a G4Tubs, then new a G4LogicalVolume, then a G4PVPlacement, all using local (discardable) variable names. The Geant4 geometry system stores them.

Does this help?

Ok, but i need make G4Tubsobjects as G4LogicalVolume and G4PVPlacement very more too,

It doesn’t matter where you create it - there will be this error everywhere.

  1. You code is declaring an array (in fact, a 2D array), and then assigning a single object to that whole array. You declare the array outside a loop, then you do nested loops over i and j, and then you assign a new object to a single array element inside the loop.

  2. You have declared an array of objects, when what you want is an array of pointers. Then you assign each element of the array via a new G4Tubs(...)call.

No, this is my code:

G4String beltIn [3][100], beltOut [3][100];
for( G4int kl; kl<100; kl++ ) {
std::ostringstream mystr;
mystr<<kl<<std::endl;

beltIn[0][kl]= "solid_inner_tyvek" + G4String(mystr.str()); 
beltIn[1][kl]= "logic_inner_tyvek" + G4String(mystr.str()); 
beltIn[2][kl]= "phys_inner_tyvek" + G4String(mystr.str()); 

beltOut[0][kl]= "solid_out_tyvek" + G4String(mystr.str()); 
beltOut[1][kl]= "logic_out_tyvek" + G4String(mystr.str()); 
beltOut[2][kl]= "phys_out_tyvek" + G4String(mystr.str()); 

// G4cout<<"\n "<<beltIn[kl]<<" i= "<<kl<<std::endl;
}

std::vector<G4Tubs*> asd(100);

   for { ...
   asd[1]=beltIn[0][1];
    //asd=beltIn[0][1];
   
         
    asd[0] = new G4Tubs(beltIn[0][1], 0.5*1254*mm, 0.5*1254.4*mm, 0.5*200*mm, startAngle, spanningAngle);
   G4LogicalVolume* logic_inner_tyvek11 = new G4LogicalVolume(solid_inner_tyvek11, tyvek, "inner_tyvek11"); }

and new problem:
error: cannot convert ‘G4String’ to ‘__gnu_cxx::__alloc_traits<std::allocator<G4Tubs*> >::value_type {aka G4Tubs*}’ in assignment
asd[1]=beltIn[0][1];

Well, because beltIn[0][1] is a G4String and ‘asd[1] is a ‘G4Tubs*.

I think you need to take a course on C and C++ programming.

I have known about it, that beltIn and g4Tubs are different variables . But my question is in follow: how i can convert string-variable to G4Tube-variable? there is not G4Tubs-class in C++ language.

may be using vector?

I’m a bit lost on what the goal here, is, so let me try and summarise what I think it is, even if only partially.

First, arrays are created to hold G4Strings for the names of the solids, logical and physical volumes:

G4String beltIn [3][100], beltOut [3][100];
for( G4int kl; kl<100; kl++ ) {
  std::ostringstream mystr;
  mystr<<kl<<std::endl;

  beltIn[0][kl]= "solid_inner_tyvek" + G4String(mystr.str()); 
  beltIn[1][kl]= "logic_inner_tyvek" + G4String(mystr.str()); 
  beltIn[2][kl]= "phys_inner_tyvek" + G4String(mystr.str()); 

  beltOut[0][kl]= "solid_out_tyvek" + G4String(mystr.str()); 
  beltOut[1][kl]= "logic_out_tyvek" + G4String(mystr.str()); 
  beltOut[2][kl]= "phys_out_tyvek" + G4String(mystr.str()); 
}

The second bit of code posted isn’t valid C++, but what it looks like is the creation of the G4Tubs instances like this:

std::vector<G4Tubs*> asd(100);

for (G4int index=0; index<100; ++index)
{
  asd[index] = new G4Tubs( beltIn[0][index], 
                           0.5*1254*mm, 0.5*1254.4*mm, 0.5*200*mm, startAngle, spanningAngle);
}

As @Allison already noted, you can then also used the now stored pointers to G4Tubs* to create the LVs, and similarly for the PVs.

There’s basically no direct way to convert G4String to G4Tubs*. You have to go via the G4Tubs constructor and fill out all the arguments.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.