# Initializing Shapes in a 'For' Loop

Hi Rooters,

I would like to produce a set of 10 cones, all with different rmax1, rmax2, and dz. Whilst I realise the easiest, and most direct, way to do this would be to simply write out 10 different lines, I will eventually want to extend to 499 cones. Thus I wish to know if it is possible to initialize a shape and then create a node to position the shape in a ‘FOR’ loop such that every time round the loop a new shape and node is created?

Something that effectively does:

For(Int_t i= 0; i<10; i++) {
dz = some function of i;
rmax1 = some function of i;
rmax2= some function of i;
TCONE *cone_i = new TCONE(“cone_i”, “cone_i”, “cone_material”, dz, rmax1, rmax2);
TNode *node_i = new TNode(“node_i”, “node_i”, cone_i);
}

Do I need to convert the int to a string first … like:

TCONE *Form(“cone_%d”,i) = … ?

Please excuse me for not including the actual code, but I havn’t written it yet! I just wish to know the feasibility of this before starting. If anyone needs more info just say.

Thanks,

Rich

Rich,

You have several ways for solving this simple problem
1- create a char array to support, eg the TNode name, with
char nodeName[20;
sprintf(nodeName,“node_%d”,i);
TNode *node = new TNode(nodeName,…

2-a shorter version using the ROOT function Form (in TString)
TNode *node = new TNode(Form(“node_%d”,i),…

Rene

Rene,

Thanks for the help. Will this not create a problem with several shapes and nodes having the same pointer? I can see that this does not matter so much for the nodes, but for shapes wont I effectively be initializiing several shapes by the same name?

I’m probably completely wrong so I’ll go away and give it a try.

Cheers,

Rich

Ok, I implemented as you described and all went well in root. However, when I moved into LITRANI, and replaced TCONE with the LITRANI shape TSCONE (which inherits from TTUBE) I get a “twice same name for shapes” error and then a segmentation break… so this is clearly a LITRANI problem.