# create temporary intermediate file
intermediateFileName = 'temp_FileFor_%s.root'%dataSetKey
intermediateFile = TFile.Open(intermediateFileName, 'recreate')
intermediateTree = protoTree.CloneTree()

protoFile.Close()
del protoFile

# create new branches
from P2VV.Load import P2VVLibrary
from ROOT import addIntegerToTree 

print 'P2VV - INFO: Processing tree (%s initial entries): %s'%(intermediateTree.GetEntries(),protoTreePath)
if addRunPeriodInfo:   
    print 'P2VV - INFO: Adding run period (%s) info to ntuple'%runPeriod
    addIntegerToTree(intermediateTree, int(runPeriod), 'runPeriod' )

if addKaonSignInfo:
    print 'P2VV - INFO: Adding kaon sign (%+i) info to ntuple'%kaonSign
    addIntegerToTree(intermediateTree, int(kaonSign), 'kaonSign' )

if calculateHelAngles: calculateHelicityAngles(intermediateTree)

if addKpiMassCategory: 
    from ROOT import addCategoryToTree, std
    print 'P2VV - INFO: Adding Kpi-mass category with indices "%s" and KK-mass boundaries "%s" to n-tuple' % ( KpiMassInds, KpiMassBinBounds )
    bounds = std.vector('Double_t')()
    inds   = std.vector('Int_t')()
    for bound in KpiMassBinBounds : bounds.push_back(bound)
    for ind in KpiMassInds : inds.push_back(ind)
    addCategoryToTree( intermediateTree, KpiMassBranchName, 'KpiMassCat', bounds, inds )
tagSSBounds = [ ] # [ -531.1, -530.9, 530.9, 531.1 ]

from ROOT import TFile
print 'reading file "%s" and cloning n-tuple "%s"' % ( nTupleFilePathIn, nTupleName )
nTupleFileIn = TFile.Open(nTupleFilePathIn)
nTupleIn = nTupleFileIn.Get(nTupleName)
nTupleFileOut = TFile.Open( nTupleFilePathOut, 'RECREATE' )
nTupleOut = nTupleIn.CloneTree()
nTupleFileIn.Close()
del nTupleFileIn

from P2VV.Load import P2VVLibrary
if runPeriod :
    from ROOT import addIntegerToTree
    print 'adding run period "%s" to n-tuple' % runPeriod
    addIntegerToTree( nTupleOut, runPeriod, 'runPeriod' )

if firstData :
    from ROOT import addCategoryToTree, std
    print 'adding "first data" flag to n-tuple for data with run number < %d' % firstData
    bounds = std.vector('Double_t')()
    inds   = std.vector('Int_t')()
    bounds.push_back( float(firstData) )
    inds.push_back(1)
    inds.push_back(0)
    addCategoryToTree( nTupleOut, 'runNumber', 'firstData', bounds, inds )

if prescaleBounds :
    from ROOT import addCategoryToTree, std
    print 'adding prescale category with indices "%s" and run-number boundaries "%s" to n-tuple' % ( prescaleInds, prescaleBounds )
    bounds = std.vector('Double_t')()
#     # if branch.GetName() not in [item[-1] for item in obsDict.items() ]:
#     for element in  ['L0','has','SS','OS','Hlt','PID','END','OWN','TOP','TRACK','Prob','ORI']:
#         if element in  branch.GetName() : protoTree.SetBranchStatus(branch.GetName(),0)
intermediateFileName = "temp_FileFor_%s.root" % dataSetKey
intermediateFile = TFile.Open(intermediateFileName, "recreate")
intermediateTree = protoTree.CloneTree()

protoFile.Close()
del protoFile

# create new branches
from ROOT import addIntegerToTree

if addRunPeriodInfo:
    print "P2VV - INFO: Adding run period (%s) info to ntuple" % runPeriod
    addIntegerToTree(intermediateTree, int(runPeriod), "runPeriod")

if addKaonSignInfo:
    print "P2VV - INFO: Adding kaon sign (%+i) info to ntuple" % kaonSign
    addIntegerToTree(intermediateTree, int(kaonSign), "kaonSign")

if addKpiMassCategory:
    from ROOT import addCategoryToTree, std

    print 'P2VV - INFO: Adding Kpi-mass category with indices "%s" and KK-mass boundaries "%s" to n-tuple' % (
        KpiMassInds,
        KpiMassBinBounds,
    )
    bounds = std.vector("Double_t")()
    inds = std.vector("Int_t")()
    for bound in KpiMassBinBounds: