Trouble between TTree::SetAutoSave() and TTree::Draw()

Testing the AutoSave feature to writing TTrees, I wrote the following little macro:

void checkpointTest() {

	//  Declare file variables.
	TTree * countTree = 0;
	unsigned int count;

	//  Create/update file from which to write entries.
	TFile outFile("checkpointTest.root");
	if (outFile.IsZombie()) {

		outFile.Open("checkpointTest.root", "recreate");

		countTree = new TTree("countTree", "TTree in which to contain the count.");
//		countTree -> SetAutoSave(10);
		countTree -> Branch("count", & count);
		count = 0;

	} else {

		countTree = (TTree *) outFile.Get("countTree");
		countTree -> SetBranchAddress("count", & count);
		count = countTree -> GetEntries();

		outFile.ReOpen("update");
	}

	while (count < 1e3 + 7) {  //  Chose to add an integer indivisible by 10 to confirm the importance of additional "Write()" at end.

		countTree -> Fill();
		++count;

//		if (rand() % 100 == 0) return;
	}

	outFile.Write();
	outFile.Close();
}

When I uncomment the above two commented-out lines, I can run the macro like root -l -q checkpointTest.C until while(count < 1e3 + 7) is satisfied. Although this eventually ends up a “count” leaf containing consecutive integers ranging from 0 to 1006, the number of entries in the histogram bins appear to increase with each cycle when I do countTree -> Draw("count"). After the first run of checkpointTest.C, I get

After the second run I get

It doesn’t appear that prior cycles are saved in the TTree, but these histograms make it look like the values from the last cycle prior to the file closing are added? To be clear, I expect the bins in the above histograms to retain values of 1 after each cycle, not increase.

Also, when I leave the two lines commented out above and no file “checkpointTest.root” is already present, I expect the entire file to run fully until count == 1006. However, when the first line is commented out I get the warning

Warning in <TFile::Write>: file checkpointTest.root not opened in write mode

and nothing is written to the file although outFile.Open("checkpointTest.root", "recreate") is declared. Why does countTree -> SetAutoSave(10) suddenly make outFile suddenly open in write mode?


ROOT Version: 6.13/01
Platform: macosx64
Compiler: Not Provided


I wouldn’t expect the histograms to behave as above. Is it supposed to? If not, is there a way to “disable” so that the sum of the bins in the histogram equals the number of entries, when there is supposed to be only one value per entry?

Well, dang, I should have checked the behavior of the “vanilla” case:

void checkpointTest() {

	//  Declare file variables.
	TTree * countTree = 0;
	unsigned int count;

	//  Create/update file from which to write entries.
	TFile outFile("checkpointTest.root", "recreate");
//	TFile outFile("checkpointTest.root");
//	if (outFile.IsZombie()) {
//
//		outFile.Open("checkpointTest.root", "recreate");

		countTree = new TTree("countTree", "TTree in which to contain the count.");
//		countTree -> SetAutoSave(10);
		countTree -> Branch("count", & count);
		count = 0;

//	} else {
//
//		countTree = (TTree *) outFile.Get("countTree");
//		countTree -> SetBranchAddress("count", & count);
//		count = countTree -> GetEntries();
//
//		outFile.ReOpen("update");
//	}

	while (count < 1e3 + 7) {  //  Chose to add an integer indivisible by 10 to confirm the importance of additional "Write()" at end.

		countTree -> Fill();
		++count;

//		if (rand() % 100 == 0) return;
	}

	outFile.Write();
	outFile.Close();
}

I then still get the same problem, regardless of AutoSave being enabled or not:


This may then have to do with implementation of bin width.

Hi,
sorry for the late reply, maybe @moneta or @pcanal can help :smile:

Cheers,
Enrico

Actually it does. When calling the TTree::Draw method root attempts to create a new histogram with bin ranges that match the range of the drawn variable. Therefore always around 100 bins are used (In your last case 105 with a width of 10) and because of the bin width of 10 every bin has 10 entries except for the last one.

If you don’t want root to select the bin ranges itself, you can select them on your own by giving root a histogram to fill and draw. Then the histogram can be filled by using the TTree::Project method and afterwards can be drawn:

TH1D* h = new TH1D("h", "", 1007, 0., 1007.);
countTree -> Project("h", "count");
h->Draw();

If you look at the sourcecode of the TTree::Project method, you find out that it actually uses the TTree::Draw method with turning of the graphics. Therefore this code has the same behavior like the one above:

TH1D* h = new TH1D("h", "", 1007, 0., 1007.);
countTree -> Draw("count>>h");

For more information on which parameters are possible please refer to: ROOT: TTree Class Reference

With your example this produces the following histogram which what I think is what you expected:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.