import json
# ROOT imports
from ROOT import TChain
# custom ROOT classes 
from ROOT import alp, ComposableSelector, CounterOperator, JetFilterOperator, BTagFilterOperator, JetPairingOperator, DiJetPlotterOperator
from ROOT import BaseOperator, FolderOperator, EventWriterOperator


config = {"jets_branch_name": "Jets",
          "hlt_names":[]}

selector = ComposableSelector(alp.Event)(0, json.dumps(config))
selector.addOperator(BaseOperator(alp.Event)())
selector.addOperator(CounterOperator(alp.Event)())
selector.addOperator(JetFilterOperator(alp.Event)(2.5, 30., 4))
selector.addOperator(CounterOperator(alp.Event)())
selector.addOperator(BTagFilterOperator(alp.Event)("pfCombinedInclusiveSecondaryVertexV2BJetTags", 0.800, 4))
selector.addOperator(CounterOperator(alp.Event)())
selector.addOperator(FolderOperator(alp.Event)("empty_one"))
selector.addOperator(JetPairingOperator(alp.Event)(4))
selector.addOperator(FolderOperator(alp.Event)("4CSVM"))
selector.addOperator(DiJetPlotterOperator(alp.Event)())
selector.addOperator(CounterOperator(alp.Event)())
selector.addOperator(EventWriterOperator(alp.Event)())
selector.addOperator(FolderOperator(alp.Event)("empty_two"))

tchain = TChain("ntuple/tree")
tchain.Add("/lustre/cmswork/hh/alpha_ntuples/v0_20161004/GluGluToHHTo4B_node_SM_13TeV-madgraph_v14-v1/0000/output.root")

tchain.Process(selector, "", 10000)
        print "- applying JER Up -"
        selector.addOperator(JERShifterOperator(alp.Event)(+1))
    elif args.jetCorr == 3:
        print "- applying JER Down -"
        selector.addOperator(JERShifterOperator(alp.Event)(-1))
#    else:
#       print "- default JEC-JER applied -"

    selector.addOperator(FolderOperator(alp.Event)("base"))
    selector.addOperator(WeightSumOperator(alp.Event)(w_nobTag_v))
    selector.addOperator(
        CounterOperator(alp.Event)(config["n_gen_events"], w_nobTag_v))
    selector.addOperator(GenJetPlotterOperator(alp.Event)(btagAlgo))

    selector.addOperator(FolderOperator(alp.Event)("acc"))
    selector.addOperator(JetFilterOperator(alp.Event)(args.eta, args.pt, 4))
    selector.addOperator(
        CounterOperator(alp.Event)(config["n_gen_events"], w_nobTag_v))
    selector.addOperator(JetPlotterOperator(alp.Event)(
        btagAlgo, w_nobTag_v))  #with bTag since jets are sorted
    selector.addOperator(GenJetPlotterOperator(alp.Event)(btagAlgo))

    selector.addOperator(FolderOperator(alp.Event)("btag"))
    selector.addOperator(
        BTagFilterOperator(alp.Event)(btagAlgo, btag_wp[1], 4, 99,
                                      config["isData"],
                                      data_path))  #99=noAntitag  3
    selector.addOperator(
        CounterOperator(alp.Event)(config["n_gen_events"], weights_v))
    selector.addOperator(JetPlotterOperator(alp.Event)(btagAlgo, weights_v))
    selector.addOperator(GenJetPlotterOperator(alp.Event)(btagAlgo))