Branch storing a array of strings, std::vector<std::stri

Hello,

I am developing a GEANT4 simulations that gives a ROOT tree as an output, with the data I need. Inside of this tree I am trying to fill a Branch with an array of strings, that should contain the name of the processes involved in each particle interaction. I saw in some previous topic of the ROOT forum that it is possible to fill the branch with std::vectorstd::string.

So I wrote, putting a maximum limit of 1000 elements on the array:

[code]std::vectorstd::string RProcess(1000);
RProcess[i] = fStep->GetPostStepPoint()->GetProcessDefinedStep()->GetProcessName();
G4cout << "RProcess = " << RProcess[i] << G4endl;
i++;

TTree *t1 = new TTree(“t1”,“t1”);
t1 -> Branch(“RProcess”, &RProcess);
t1->Fill()
t1->Write()[/code]

The “G4cout” prints in the screen the correct name, but when I finish the simulation and access the output tree from ROOT, I get always

So it seems that the filling procedure doesn’t work.

I tried as well defining the variable as a pointer, as said in some of the forum topics, but I got the same result.

[code]std::vectorstd::string *RProcess; //Process involved in the interaction
RProcess = new std::vectorstd::string;
RProcess->push_back(fStep->GetPostStepPoint()->GetProcessDefinedStep()->GetProcessName());
G4cout << "RProcess = " << (*RProcess)[i] << G4endl;

TTree *t1 = new TTree(“t1”,“t1”);
t1 -> Branch(“RProcess”, &RProcess);
t1->Fill()
t1->Write()
delete RProcess;[/code]

Any idea of what is going wrong?

Thanks,
Estela

Hi,

With ROOT v5.20, I have no problem storing vector.
See root.cern.ch/viewvc/trunk/root/t … t&view=log for a simple example.

[quote] RProcess = NULL
So it seems that the filling procedure doesn’t work. [/quote]Nope :slight_smile:. This actually only means that TTree::Show does not properly print vector (of string).

[quote]So I wrote, putting a maximum limit of 1000 elements on the array:
std::vectorstd::string RProcess(1000);
[/quote]Actually, you create exactly 1000 strings which will each be stored in the file (whether you set them or not).

[quote]I tried as well defining the variable as a pointer,
[/quote]Which version of ROOT are you using? The non-pointer interface works for ROOT v5.20 and newer.

Cheers,
Philippe.

Hi,

thanks a lot for the reply. (I am using root v20.0.) You were right. The data was actually in my ROOT branch, but it is not printed when I put t1->Show() Only if I write as you said in your example:

then I get the information. So it was actually working all the time, what was wrong was the way to extract the information out of this branch.

I just have discovered a different problem related to it. Storing those arrays of strings not only produces larger output files, but also requires 25 times longer simulation time.

Therefore I have decided to change to a different approach: I give a different number to each possible physical process, and in this way I substitute the array of characters by a simple array of integers.

I define, for example:

polarLowEnCompt = 1 LowEnPhotoElec = 2 eIoni = 3 ...... etc.

Knowing the conversion between my integer quantities to the GEANT4 physical process I can always identify which process has been involved in each step of my simulation.

It is maybe a not very elegant way to do it, but it saves a very large amount of time.

Just for curiosity, is it normal that writing an array of characters consumes so much time?

Thanks a lot for your help,
Estela.