void RootTutorial2_sol() { cout << "[root_tutorial_2] Hello intern!" << endl; cout << "[root_tutorial_2] This Macro will walk you through looping a TTree and making your own plot." << endl; cout << "[root_tutorial_2] Reading ROOT file." << endl; TFile *f = new TFile("../zjet.root"); cout << "[root_tutorial_2] Geting TTree from root file." << endl; TTree *t = (TTree*) f -> Get( "Tdata" ); cout << "[root_tutorial_2] Declaring TH1D (your plot object)." << endl; TH1D *h = new TH1D( "h", "Z mass (GeV)", 100, 50, 150 ); TH1D *h1 = new TH1D( "h1", "Other particle", 100, 0, 0.5 ); // These are the variables that will receive the data in the TTree vector *id = 0; vector *m = 0; // This is how you say to the TTree that these are the variables taking in the information t->SetBranchAddress("id",&id); t->SetBranchAddress("m",&m); // Now will make a for loop on the TTree entries cout << "[root_tutorial_2] Looping on the TTree." << endl; for (int iEnt = 0; iEnt < t->GetEntries(); ++iEnt) { // This is how you get an entry from the TTree, i.e. this is how you load the values from a given entry to your variables t->GetEntry(iEnt); // We now loop on the objects of the event which are encoded in the vectors: for (int iObj = 0; iObj < (*id).size(); ++iObj) { // We want the Z boson, which in this case as id 0 (these id's are different from Pythia's) if ((*id)[iObj] == 0 ) h->Fill((*m)[iObj] , 1); //cout << (*m)[iObj] << endl; if ((*id)[iObj] == 10 ) h1->Fill((*m)[iObj] , 1); } } // Now we wanna draw our object. You can create a TCanvas for this where you can control the width and the height, but for this demonstration it's not necessary cout << "[root_tutorial_2] Drawing your two plots." << endl; //h->Draw(); //h1->Draw("SAME"); TCanvas *cnew = new TCanvas("cnew","The Title",800,600); cnew->Divide(2, 1); cnew->cd(1); h->GetXaxis()->SetTitle("Mass (GeV)"); h->GetYaxis()->SetTitle("Events/bin"); h->Draw(); //h->Fit("gaus"); //h->Fit("gaus", 89, 93); // this fails because we didn't read the docs h->Fit("gaus", "", "", 89, 93); cnew->cd(2); h1->Draw(); h1->GetXaxis()->SetTitle("Mass (GeV)"); h1->GetYaxis()->SetTitle("Events/bin"); h1->Fit("expo","","", 0.005, 0.3); cnew->SaveAs("out.pdf"); cout << "[root_tutorial_2] Next macro you'll reconstruct the Z and the jet from particles a detector could find and try to write the objects to a file this macro can read." << endl; }