Character array

I am trying to create and and access the elements of a character array of fixed length ([8], that is in a text file together with other data of type float. The created tree has branches of the proper type corresponding to the variables I create. However, it would appear that the variables so created are continuous and can not be accessed by index ([i]) in order to be able to get the individual elements of the array. I browsed the examples in Root tutorials but I failed to find one that I could use. Does somebody know of any?. Is it possible to access the individual elements of a TTree array by index [i] number?
Thanks in advance

I’m not sure i understood your problem

you stored elements of the char array in one branch and you want to retrieve the elements from it?

Hi,

This is currently a known defect of TTree::Draw that you can not access the strings as individual values. You ‘can’ access the first element by using it in an arithmetic expression (index[0]+1).
You will need to use MakeSelector to do the analysis.

Cheers,
Philippe

Thank you all for the help and sorry for the delay. I’ve been out of the office for some weeks now.

Hello,

I have a filenames.txt file in which I have stored the filenames of other 120 .txt files. These other 120 .txt files are themselves containing integer value readings of ADC frequencies (some integer numbers, in short) in a column. There is only one such column in all these 120 .txt files.

I want now to open filenames.txt file and read the names of those 120 .txt files one after the other and want to draw a histogram for each, storing all these 120 histograms in a single .root file.

I wrote a macro for this:

{

	ifstream in1("filenames.txt"); //this file contains names of other 120 .txt files
	char name[20]; //variable for the entries in filenames.txt
	Int_t i; //counter for the while loop
	while (!in1.eof())
	{
		in1>name;  //pass the entry in the filename.txt to character array
		ifstream in2 (in1); //I dont know whether this is acceptable, help needed
		TH1F *h = new TH1F ("h1","histo",4096,0,4095);
		Int_t freq; 
		Int_t j;
		while(!in2.eof())
		{
			in2>>freq;
			for (Int_t k=0;k<freq;k++)
			{
				h->Fill(j);
			}
		j++;
		}
		h->Draw();
		TFile *f = new TFile ("here.root","RECREATE");
		h->Fit("gaus");
		h->Write();
		f->Close();
	}
	i++;
}

When I run this error:

Error: operator> not defined for basic_ifstream<char,char_traits > collective.cpp:8:

I will be grateful for you help. Thank you.

Hi,

I think you meant:in1 >> name; //pass the entry in the filename.txt to character array ifstream in2 (name);

Cheers,
Philippe.

Oh yes… That was so stupid of me…
Anyway, I think there is fault in the logic itself… Trying to figure out how I can do it…

Thanks…

[code]#include “TFile.h”
#include “TH1.h”

#include
#include

void read_many_files(const char *filenames = “filenames.txt”,
const char *outfile = “here.root”)
{
if ((!filenames) || (!(*filenames))) return; // just a precaution
std::ifstream in1(filenames); // names of another 120 .txt files

TFile *f = ((TFile *)0);
if (outfile && (*outfile)) f = TFile::Open(“here.root”, “RECREATE”);

Int_t i = 0; // counter for the while loop
while (1) {
std::string name;
#if 0 /* 0 or 1 /
in1 >> name; // pass the entry in the filename.txt
if ( ! in1.good() ) break;
#else /
0 or 1 /
std::getline(in1, name); // pass the entry in the filename.txt
if ( ! in1.good() ) break;
if (!name.size() || name.find_first_not_of("\t\n\v\f\r #")) continue;
#endif /
0 or 1 */

std::ifstream in2(name.c_str());

TH1F *h = new TH1F(TString::Format("h1_%03d", i),
                   TString::Format("histo %03d", i),
                   4096, 0, 4096);

while(1) {
  Int_t freq;
  in2 >> freq;
  if ( ! in2.good() ) break;
  
  h->Fill(freq);
}

if (h->GetEntries() > 0) {
  h->Draw();
  h->Fit("gaus");
}

h->Write();
i++; // "next" histogram

}

if (f) delete f; // automatically deletes all histograms, too
}[/code]

Thank you for the solution…
I will go through it…

I tried it my way…
I did this…

{ TFile *f=new TFile("big.root","RECREATE"); ifstream in1("filenames.txt");//open the file which has names of other files char name[200]; while(!in1.eof()){ in1>>name; cout<<name<<endl; ifstream in2(name); Int_t freq; Int_t i=0; TH1F *h=new TH1F(name,name,4096,0,4095); while(!in2.eof()){ in2>>freq; for (Int_t k=0;k<freq;k++){ h->Fill(i); } i++; } h->Write(); in2.close(); } in1.close(); f->Close(); }

I know this is very naive but it works, just one problem, it shows a potential memory leak at the end… I get the .root file in which I have 120 histograms… But this memory leak I dont understand… How can I avoid it by modifying the same program?

Thanks.

Hi,

I guessing it is warning about potential leak of histograms … if this is the case, you need to make sure your histogram have unique names.

Cheers,
Philippe.