Basic Data input question

I am currently using the input

but this means when i am wanting to look at different ROOT files i need to change it. How is the easiest way to a) use the root file that i load with root and B) is it easy to ask the user for the file that he wants to be analysed?

Thanks Gareth

root > TString filename
root > cin >> filename
root > TChain.Add(filename)

Rene

unfortunately that didnt seem to work. i think this may have been because of how i implamented it.

the code i use originally is below, can you see why any error might occur?

[code]{
TChain chain(“100”);
chain.Add("/net/unixfsrv/hep/gbrown/100Events.ntup.root");

int particles;
int id[100];
float pt[100];
int child_of[100];
int count=0;
int sec;

int nentries = (Int_t) chain->GetEntries();

chain->SetBranchAddress(“particles”,&particles);
chain->SetBranchAddress(“id”,id);
chain->SetBranchAddress(“pt”,pt);
chain->SetBranchAddress(“child_of”,child_of);

TH1F *h_ptsecond = new TH1F(“h_ptsecond”,“pt of first particle in chain”,100,0.,50000.);

for (int i=0; i<nentries ; i++) {
chain->GetEntry(i);
cout << " Entry: " << i << " number of particles " << particles << endl;
for (int ipar=0; ipar<(particles) ; ++ipar) {
if (id[ipar]==443/&&id[child_of[ipar]-1]==531/){
++count;
}
if(child_of[ipar]==0){
cout << "particle " << ipar << " id : " << id[ipar] << " child of: " << child_of[ipar]-1 <<endl;
}
else{
cout << “particle " << ipar << " id : " << id[ipar] << " child of: " << child_of[ipar]-1 <<” "<< id[child_of[ipar]-1] <<endl;
}

  }
  }

  h_ptsecond->Fill(pt[0]);

cout << "Phi Psi: " << count <<endl;
}

};
[/code]

I do not understand the relationship between this post and your original post.
What is the problem?
Could you provide a short data file such that we can run your script?

Rene

Hi,

I might have an idea what Gareth is looking for. Put your code into a file called e.g. LoadTree.C, and replace your code with void LoadTree(const char* filename){ TChain chain("100"); chain.Add(filename); Now users can call .x LoadTree.C+("somefile.root")
Is that what you need?

Cheers, Axel.

Axel, that is indeed the sort of thing i wanted. When i tried to replace my code with yours there seemed to be a few errors coming up (see below)

[quote]

root [5] .x LoadTree.C+(“500Eventsb.ntup.root”)
Info in : creating shared library /net/unixfsrv/hep/gbrown/./LoadTree_C.so
In file included from /net/unixfsrv/hep/gbrown/filevDYB64.h:32,
from /net/unixfsrv/hep/gbrown/filevDYB64.cxx:16:
/net/unixfsrv/hep/gbrown/LoadTree.C: In function void LoadTree(const char*)': /net/unixfsrv/hep/gbrown/LoadTree.C:5:TChain’ undeclared (first use this
function)
/net/unixfsrv/hep/gbrown/LoadTree.C:5: (Each undeclared identifier is reported
only once for each function it appears in.)
/net/unixfsrv/hep/gbrown/LoadTree.C:5: syntax error before (' token /net/unixfsrv/hep/gbrown/LoadTree.C:8:chain’ undeclared (first use this
function)
/net/unixfsrv/hep/gbrown/LoadTree.C:24: TH1F' undeclared (first use this function) /net/unixfsrv/hep/gbrown/LoadTree.C:24:h_ptsecond’ undeclared (first use this
function)
/net/unixfsrv/hep/gbrown/LoadTree.C:24: syntax error before (' token /net/unixfsrv/hep/gbrown/LoadTree.C:28:cout’ undeclared (first use this
function)
/net/unixfsrv/hep/gbrown/LoadTree.C:28: endl' undeclared (first use this function) /net/unixfsrv/hep/gbrown/LoadTree.C:15: warning: unused variableint sec’
g++: /net/unixfsrv/hep/gbrown/./filevDYB64.o: No such file or directory
Error in : Compilation failed!
Error: Function LoadTree(“500Eventsb.ntup.root”) is not defined in current scope :0:
Possible candidates are…
filename line:size busy function type and name
*** Interpreter error recovered ***[/quote]

i dont know if it means much too you. it might just be me making a mistake.

Thanks, Gareth

Sorry same message

This errors are real C++ errors reported by the compiler. To fix those you need to add the proper include (TClass.h, Riostream.h, TH1F.h, etc.)>

Cheers,
Philipe

which includes will i need??

i have added

#include <iostream.h> #include <vector.h> #include <TClass.h> #include <Riostream.h> #include <TH1F.h>

but still have the errors

[quote]root [2] .x LoadTree.C+(“500Events.ntup.root”)
Info in TUnixSystem::ACLiC: creating shared library /net/unixfsrv/hep/gbrown/./LoadTree_C.so
In file included from /usr/include/c++/3.2.3/backward/iostream.h:31,
from /net/unixfsrv/hep/gbrown/LoadTree.C:1,
from /net/unixfsrv/hep/gbrown/file9pDntH.h:32,
from /net/unixfsrv/hep/gbrown/file9pDntH.cxx:16:
/usr/include/c++/3.2.3/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the header for the <X.h> header for C++ includes, or instead of the deprecated header <strstream.h>. To disable this warning use -Wno-deprecated.
In file included from /net/unixfsrv/hep/gbrown/file9pDntH.h:32,
from /net/unixfsrv/hep/gbrown/file9pDntH.cxx:16:
/net/unixfsrv/hep/gbrown/LoadTree.C: In function void LoadTree(const char*)': /net/unixfsrv/hep/gbrown/LoadTree.C:11:TChain’ undeclared (first use this
function)
/net/unixfsrv/hep/gbrown/LoadTree.C:11: (Each undeclared identifier is reported
only once for each function it appears in.)
/net/unixfsrv/hep/gbrown/LoadTree.C:11: syntax error before (' token /net/unixfsrv/hep/gbrown/LoadTree.C:14:chain’ undeclared (first use this
function)
/net/unixfsrv/hep/gbrown/LoadTree.C:21: warning: unused variable `int sec’
g++: /net/unixfsrv/hep/gbrown/./file9pDntH.o: No such file or directory
Error in : Compilation failed!
Error: Function LoadTree(“500Events.ntup.root”) is not defined in current scope :0:
Possible candidates are…
filename line:size busy function type and name
*** Interpreter error recovered ***[/quote]

Looking at the error reported by the compiler, you should at least
#include “TChain.h”

Rene

that has got rid of a few more. i now have

[quote]In file included from /net/unixfsrv/hep/gbrown/fileXjLmkQ.h:32,
from /net/unixfsrv/hep/gbrown/fileXjLmkQ.cxx:16:
/net/unixfsrv/hep/gbrown/LoadTree.C: In function void LoadTree(const char*)': /net/unixfsrv/hep/gbrown/LoadTree.C:23: base operand of->’ has non-pointer
type TChain' /net/unixfsrv/hep/gbrown/LoadTree.C:25: base operand of->’ has non-pointer
type TChain' /net/unixfsrv/hep/gbrown/LoadTree.C:26: base operand of->’ has non-pointer
type TChain' /net/unixfsrv/hep/gbrown/LoadTree.C:27: base operand of->’ has non-pointer
type TChain' /net/unixfsrv/hep/gbrown/LoadTree.C:28: base operand of->’ has non-pointer
type TChain' /net/unixfsrv/hep/gbrown/LoadTree.C:33: base operand of->’ has non-pointer
type TChain' /net/unixfsrv/hep/gbrown/LoadTree.C:21: warning: unused variableint sec’
g++: /net/unixfsrv/hep/gbrown/./fileXjLmkQ.o: No such file or directory
Error in : Compilation failed!
Error: Function LoadTree(“500Events.ntup.root”) is not defined in current scope :0:
Possible candidates are…
filename line:size busy function type and name
*** Interpreter error recovered ***
[/quote]

which most seem to be similar. Any ideas??

Thanks, gareth

Replace
chain->
by
chain.

Rene

thank you very much, all of you whom helped. that will save me a lot of time.

A futher question slightly away from the original would be if it would be possible to save all the cout to a .txt file so i can view all of the data, because at the moment there is too much data and some is lost.

so how would i go about printing to a .txt file.

Thanks,
Gareth

It all depends on which output you would like to redirect. You may (or may not) be looking for TSystem::RedirectOutput.

Cheers,
Philippe

what i would like it to do is copy all the text that i have outputed into a file.

[quote]what i would like it to do is copy all the text that i have outputed into a file.[/quote]I am sorry but this is too vague of a description for me to be able to give any help that might be remotely useful :frowning:.
Where does ‘all the text’ comes from? How is it ‘outputed’ and by which part of your application?

Cheers,
Philippe.

PS. Still the closest answer is to use TSystem::RedirectOutput, did you try it out?

[quote=“pcanal”][quote]what i would like it to do is copy all the text that i have outputed into a file.[/quote]I am sorry but this is too vague of a description for me to be able to give any help that might be remotely useful :frowning:.
Where does ‘all the text’ comes from? How is it ‘outputed’ and by which part of your application?

Cheers,
Philippe.

PS. Still the closest answer is to use TSystem::RedirectOutput, did you try it out?[/quote]

using TSystem::RedirectOutput do i just need to add that to the top of my code.

Sorry about the vague description, maybe this will be better

in my code i read from an ntup. i read through the event in a loop and Cout certain bits of information like “id”. now all text i have “cout”'ed to the screen i want to put into a text file.

dont know if that is better. I will try your earlier suggestion

i managed to do it by adding.

ofstream myfile; myfile.open ("example.txt"); myfile << "Writing this to a file.\n"; myfile.close();

i was wondering how can i have instead of calling it example.txt, can i have it as filename.txt where i inputed filename as earlier