How to store histograms in different folders in a root file?

Hello,

I have a different set of histograms and I want to store them in separate folders. I am new to ROOT, so I don’t know how to do this.

The following code is the pseudo-code for what I want to do. Please help me with this.

void test()
{
    TFile *fout = new TFile("test.root","RECREATE");
    TTree *tree = new TTree("tree","tree");
	
	Double_t X;
	Double_t Y;
	tree->Branch("X", &X, "X/D");
	tree->Branch("Y", &X, "Y/D");
	
	// I want these histograms in different folders
	TH1F *hist_X = new TH1F("hist_X", "X", 20, 0, 10);	// folder name: Many_hist_X
	TH1F *hist_Y = new TH1F("hist_Y", "Y", 20, 0, 10);	// folder name: Many_hist_Y
	
	for (int i=0; i<10; i++)
	{
		X = i;
		Y = i;
		
		hist_X->Fill(X);
		hist_Y->Fill(Y);
		
		tree->Fill();
	}
	
	fout->Write();
    fout->Close();
}

Thank you,
Divyang.

Use mkdir to create a directtory n a TFile.

https://root.cern/doc/v608/dirs_8C_source.html