Tree structure with TGraphs

Hi there people of Root,

I’m kinda new here and I’m trying to create a root file that would hold three trees with TGraphs with Datetime x axis. The structure would be like this:

  • Each tree would be data read from different data folder
  • Each tree branch would be data read from a different file from the above folder

The code doesn’t implement the whole structure yet, so no need to bother about that.

Problem 1:
When I write the graphs on the TFile directly, they show correctly but with the “alp” draw option. Print screen: http://prntscr.com/opja8s
Question 1: Is there a way to change this option in the tree structure to “ap”?

Problem 2:
When I store the graphs on the branches, I seem to get an odd structure, but no graph. Print screen: http://prntscr.com/opjbv8
Question 2: How would I go about placing the TGraphs as just leaves under each branch? (Also with the “ap” draw option)

I will try to post as less code as possible:

//Initialization of variables
	string line;
	
	vector<string> datetime;
	vector<double> x_time;
	vector<double> pressure, temperature, humidity;
        //other helpful variables
	
	//File manipulation
    ifstream file;
	file.open("envData.dat", ios::out);
	TFile *treefile = new TFile("dataTree.root", "RECREATE");
	
	//Start parsing the file
	getline(file, line); //skip first line with titles

	while(!file.eof()) {
		// using getline on "line" variables
        // fill the vectors appropriately with pushback
	}
	file.close();

	//TGraphs
	TGraph *envTemperature = new TGraph(n, &x_time[0], &temperature[0]);
	TGraph *envPressure = new TGraph(n, &x_time[0], &pressure[0]);
	TGraph *envHumidity = new TGraph(n, &x_time[0], &humidity[0]);	
	
	//Graph details: axis, divisions, titles etc
	
	//Tree initialization
	TTree envStation("envStation", "Environmental measurements");
	
	envTemperature -> Write("Temperature");
	envPressure -> Write("Pressure");
	envHumidity -> Write("Humidity");
	
	//Branches initialization
	TBranch *branch = envStation.Branch("Temperature", "TGraph", &envTemperature);
    TBranch *branch = envStation.Branch("Pressure", "TGraph", &envPressure);
	TBranch *branch = envStation.Branch("Humidity", "TGraph", &envHumidity);

	envStation.Write();

Sorry if the post is long and thanks in advance

May I ask you why you want to save the TGraphs as tree branches?

Thanks for your reply bellenot

I just want them as leaves and branches did the trick with histograms. They were created in a leaf for each branch. Is it not the same? Can I write them in the tree differently?

I’m not sure I understand what you mean… My question was: Why don’t you simply save the TGraphs in a ROOT file?

The structure I want should be like this:

-> Tree 1: envStation

  • Temperature
  • Pressure
  • Humidity

-> Tree 2:

  • graph1
  • graph2
    .
    .
    .

-> Tree 3:
.
.
.
etc

I just want each graph to be under the appropriate tree, is this not possible?

OK, but still, you could have:

  • tree 1: envStation
    • Temperature
    • Pressure
    • Humidity
  • directory 1: Your graphs
    • graph1
    • graph2

Am I not phrasing it correctly? This what Im trying to achieve.

My question is: How do I store the TGraph in the tree as a leaf instead of writing it on the root file directly?

Well, apparently not, since in my example the directory 1 is not a tree leaf! Anyway, if you really want to store you graph in a Tree leaf (which is quite weird IMHO), I’ll try to come with an example…

There is no advantage to storing a TGraph in a TTree rather than in the ROOT file itself. You should do as bellenot shows you (storing the TGraph in a directory)

Thanks for your replies bellenot. However, I dont understand how it’s weird. I’ve read many examples on the documentation that does the same thing with histograms:

I just want to do it with TGraphs

To clarify here,
I’ve read quite a lot of documentation on trees and histograms but I’m a bit new here. I’m not trying to get you to come up with a solution for me, but I’m really trying to understand

Thanks pamputt, I understand now what you guys mean. I’ll just do it in directories then.

I think you misunderstand what you see. In the screenshot you posted, the histogram is generated from the tree data, and is not stored in the tree. And again: you should not try to save TGraphs in a TTree, but you can easily save then in the file itself

Thanks a ton, I’ll make the needed structure in directories then.

Any idea on the drawing option? Is there some way I can define the drawing option before writing on the file?

When saving the TGraph in the file, you simply save the object, without any graphic attributes. If you want to keep the attributes, you could try to save the TCanvas. Maybe there is another option, but our graphic expert, @couet, is on vacation for the time being…

That’s alright,

thanks for the remarks.

In the browser’s “Draw Option:” field type “ap” then click any graph.

1 Like