#! /usr/local/bin/python3.6

import ROOT
from ROOT import TFile, TTree, TH1D
import os

def RootTutorial2():

  print("[root_tutorial_2] Hello intern!")
  print("[root_tutorial_2] This Macro will walk you through looping a TTree and making your own plot.")

  print("[root_tutorial_2] Reading ROOT file.")
  
  f = TFile("./zjet.root")

  print("[root_tutorial_2] Geting TTree from root file.")
  t =f.Get( "Tdata" )

  print("[root_tutorial_2] Declaring TH1D (your plot object).")
  
  h = TH1D( "h", "Z mass (GeV)", 100, 50, 150 )

  # Now will make a for loop on the TTree entries
  print("[root_tutorial_2]  Looping on the TTree.")

  for entry in t:

    # We now loop on the objects of the event which are encoded in the vectors:
    
    #there are two ways to do this:
    #for iObj in range(entry.id):
      #if(entry.id[iObj] == 0):
        #h.Fill(entry.m[iObj] , 1)
        
    for iObj,idCode in enumerate(entry.id):
      if (idCode == 0 ):
        h.Fill(entry.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
  print("[root_tutorial_2] Drawing your plot.")
  h.Draw()
  ROOT.gPad.Update()
  print("[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.")

  input("Press q to exit program (and say goodbye to your beautiful histogram)...")


if __name__ == "__main__":
    RootTutorial2()
