Exemplo n.º 1
0
    def orderandadd(histo_dicts,setup):
        '''
        Setup is defined in the plot conf file
        histo_dicts contains an array of dictionnary
        '''

        from array import array
        doubleVariable = array('d',[0])
        
        #print "Start orderandadd"
        #print "=================\n"
        #print "Input dict is", histo_dicts

        ordered_histo_dict = {}
        #print "orderandadd-setup",setup
        #print "orderandadd-histo_dicts",histo_dicts
        for sample in setup:
            nSample = 0
            for histo_dict in histo_dicts:
                if histo_dict.has_key(sample):
                    integral = histo_dict[sample].IntegralAndError(0,histo_dict[sample].GetNbinsX(),doubleVariable)
                    error = doubleVariable[0]
                    entries = histo_dict[sample].GetEntries()
                    subsamplename = histo_dict[sample].GetTitle()
                    if nSample == 0:
                        ordered_histo_dict[sample] = histo_dict[sample].Clone()
                    else:
                        ordered_histo_dict[sample].Add(histo_dict[sample])
                    printc('magenta','','\t--> added %s to %s Integral: %s Entries: %s Error: %s'%(subsamplename,sample,integral,entries,error))
                    nSample += 1
        del histo_dicts
        #print "Output dict is", ordered_histo_dict
        return ordered_histo_dict 
Exemplo n.º 2
0
def copytree(pathIN,pathOUT,prefix,newprefix,file,Aprefix,Acut):

    start = time.time()

    print "##### COPY TREE - BEGIN ######"
    print "Input File : %s/%s%s.root " %(pathIN,prefix,file)
    print "Output File : %s/%s%s%s.root" %(pathOUT,newprefix,Aprefix,file)
    
    
    #input = ROOT.TFile.Open("%s/%s%s.root" %(pathIN,prefix,file),'read')
    input = ROOT.TFile.Open("%s/%s%s%s.root" %(pathIN,newprefix,Aprefix,file),'read')
    output = ROOT.TFile.Open("%s/%s%s%s.root" %(pathOUT,newprefix,Aprefix,file),'recreate')
    
    
    input.cd()

    obj = ROOT.TObject
    for key in ROOT.gDirectory.GetListOfKeys():
        input.cd()
        obj = key.ReadObj()
        #print obj.GetName()
        if obj.GetName() == 'tree':
            continue
        output.cd()
        #print key.GetName()
        obj.Write(key.GetName())
        
    inputTree = input.Get("tree")
    nEntries = inputTree.GetEntries()

    # Turn of branches we do not need
    inputTree = branch_reduce(inputTree)

    output.cd()
    print '\n\t copy file: %s with cut: %s' %(file,Acut)
    outputTree = inputTree.CopyTree(Acut)
    kEntries = outputTree.GetEntries()
    printc('blue','',"\t before cuts\t %s" %nEntries)
    printc('green','',"\t survived\t %s" %kEntries)
    outputTree.AutoSave()
    output.ls()
    print "Writing output file"
    output.Write()
    print "Closing output file"
    output.Close()
    print "Closing input file"
    input.Close()

    print "##### COPY TREE - END ######"
    
    end = time.time()
    
    print '\n-----> Total Prep Time: ', end - start
Exemplo n.º 3
0
 def orderandadd(histo_dicts,setup):
     ordered_histo_dict = {}
     for sample in setup:
         nSample = 0
         for histo_dict in histo_dicts:
             if histo_dict.has_key(sample):
                 if nSample == 0:
                     ordered_histo_dict[sample] = histo_dict[sample].Clone()
                 else:
                     printc('magenta','','\t--> added %s to %s'%(sample,sample))
                     ordered_histo_dict[sample].Add(histo_dict[sample])
                 nSample += 1
     del histo_dicts
     return ordered_histo_dict 
Exemplo n.º 4
0
def copytree(pathIN,pathOUT,prefix,newprefix,file,Aprefix,Acut):

    start = time.time()

    print "##### COPY TREE - BEGIN ######"
    print "Input File : %s/%s%s.root " %(pathIN,prefix,file)
    print "Output File : %s/%s%s%s.root" %(pathOUT,newprefix,Aprefix,file)
        
    input = ROOT.TFile.Open("%s/%s%s.root" %(pathIN,prefix,file),'read')
    output = ROOT.TFile.Open("%s/%s%s%s.root" %(pathOUT,newprefix,Aprefix,file),'recreate')
    
    input.cd()

    obj = ROOT.TObject
    for key in ROOT.gDirectory.GetListOfKeys():
        input.cd()
        obj = key.ReadObj()
        #print obj.GetName()
        if obj.GetName() == 'tree':
            continue
        output.cd()
        #print key.GetName()
        obj.Write(key.GetName())
        
    inputTree = input.Get("tree")
    nEntries = inputTree.GetEntries()

    # Turn of branches we do not need
    inputTree = branch_reduce(inputTree)

    output.cd()
    print '\n\t copy file: %s with cut: %s' %(file,Acut)
    outputTree = inputTree.CopyTree(Acut)
    kEntries = outputTree.GetEntries()
    printc('blue','',"\t before cuts\t %s" %nEntries)
    printc('green','',"\t survived\t %s" %kEntries)
    outputTree.AutoSave()
    output.ls()
    print "Writing output file"
    output.Write()
    print "Closing output file"
    output.Close()
    print "Closing input file"
    input.Close()

    print "##### COPY TREE - END ######"
    
    end = time.time()
    
    print '\n-----> Total Prep Time: ', end - start
Exemplo n.º 5
0
 def orderandadd(histo_dicts, setup):
     ordered_histo_dict = {}
     for sample in setup:
         nSample = 0
         for histo_dict in histo_dicts:
             if histo_dict.has_key(sample):
                 if nSample == 0:
                     ordered_histo_dict[sample] = histo_dict[sample].Clone()
                 else:
                     printc('magenta', '',
                            '\t--> added %s to %s' % (sample, sample))
                     ordered_histo_dict[sample].Add(histo_dict[sample])
                 nSample += 1
     del histo_dicts
     return ordered_histo_dict
Exemplo n.º 6
0
def copySingleFile(whereToLaunch, inputFile, outputFile, Acut,
                   remove_branches):
    print 'inputFile', inputFile
    if ('pisa' in whereToLaunch):
        input = ROOT.TFile.Open(inputFile, 'read')
    else:
        input = ROOT.TFile.Open('root://t3dcachedb03.psi.ch:1094/' + inputFile,
                                'read')

    output = ROOT.TFile.Open(outputFile, 'create')
    print "Writing file:", outputFile

    input.ls()
    input.cd()
    obj = ROOT.TObject
    for key in ROOT.gDirectory.GetListOfKeys():
        input.cd()
        obj = key.ReadObj()
        #print obj.GetName()
        if obj.GetName() == 'tree':
            continue
        output.cd()
        #print key.GetName()
        obj.Write(key.GetName())

    inputTree = input.Get("tree")
    nEntries = inputTree.GetEntries()
    for branch in remove_branches:
        if branch and not branch.isspace():
            # print 'DROPPING BRANCHES LIKE',str(branch)
            inputTree.SetBranchStatus(str(branch), ROOT.kFALSE)

    output.cd()
    print '\n\t copy file: %s with cut: %s' % (inputFile, Acut)
    outputTree = inputTree.CopyTree(Acut)
    kEntries = outputTree.GetEntries()
    printc('blue', '', "\t before cuts\t %s" % nEntries)
    printc('green', '', "\t survived\t %s" % kEntries)
    outputTree.AutoSave()
    output.ls()
    print "Writing output file"
    output.Write()
    print "Closing output file"
    output.Close()
    print "Closing input file"
    input.Close()
Exemplo n.º 7
0
def copySingleFile(whereToLaunch,inputFile,outputFile,Acut,remove_branches):
        if ('pisa' in whereToLaunch):
          input = ROOT.TFile.Open(inputFile,'read')
        else:
          input = ROOT.TFile.Open(inputFile,'read')

        if not input:
          print 'input file NOT EXISTING:',inputFile
          #input.Close()
          return

        __tmpPath = os.environ["TMPDIR"]
        outputFileName = outputFile.split('/')[len(outputFile.split('/'))-1]
        print 'outputFileName',__tmpPath+'/'+outputFileName
        output = ROOT.TFile.Open(__tmpPath+'/'+outputFileName,'recreate')

        inputTree = input.Get("tree")
        nEntries = inputTree.GetEntries()
        for branch in remove_branches:
          if branch and not branch.isspace():
            # print 'DROPPING BRANCHES LIKE',str(branch)
            inputTree.SetBranchStatus(str(branch), ROOT.kFALSE);

        output.cd()
        print '\n\t copy file: %s with cut: %s' %(inputFile,Acut)
        outputTree = inputTree.CopyTree(Acut)
        kEntries = outputTree.GetEntries()
        printc('blue','',"\t before cuts\t %s" %nEntries)
        printc('green','',"\t survived\t %s" %kEntries)
        outputTree.AutoSave()
        input.cd()
        obj = ROOT.TObject
        for key in ROOT.gDirectory.GetListOfKeys():
            input.cd()
            obj = key.ReadObj()
            if obj.GetName() == 'tree':
                continue
            output.cd()
            obj.Write(key.GetName())
        output.Write()
        output.Close()
        input.Close()
        command = 'srmcp -2 -globus_tcp_port_range 20000,25000 file:///'+__tmpPath+'/'+outputFileName+' '+ outputFile.replace('root://t3dcachedb03.psi.ch:1094','srm://t3se01.psi.ch:8443/srm/managerv2?SFN=/')
        subprocess.call([command], shell=True)
        command = 'rm '+__tmpPath+'/'+outputFileName
        subprocess.call([command], shell=True)
Exemplo n.º 8
0
 def orderandadd(histo_dicts,setup,jobnames):
     ordered_histo_dict = {}
     print "orderandadd-setup",setup
     print "orderandadd-histo_dicts",histo_dicts
     for sample in setup:
         nSample = 0
         for histo_dict in histo_dicts:
             if histo_dict.has_key(sample):
                 integral = histo_dict[sample].Integral()
                 entries = histo_dict[sample].GetEntries()
                 if nSample == 0:
                     ordered_histo_dict[sample] = histo_dict[sample].Clone()
                 printc('magenta','','\t--> added %s to %s. Integral: %s. Entries: %s'%(jobnames[histo_dicts.index(histo_dict)],sample,integral,entries))
                 ordered_histo_dict[sample].Add(histo_dict[sample])
                 nSample += 1
     print "orderandadd-ordered_histo_dict",ordered_histo_dict
     del histo_dicts
     return ordered_histo_dict 
Exemplo n.º 9
0
def copySingleFile(whereToLaunch,inputFile,outputFile,Acut,remove_branches):
        print 'inputFile',inputFile
        if ('pisa' in whereToLaunch):
          input = ROOT.TFile.Open(inputFile,'read')
        else:
          input = ROOT.TFile.Open('root://t3dcachedb03.psi.ch:1094/'+inputFile,'read')

        output = ROOT.TFile.Open(outputFile,'create')
        print "Writing file:",outputFile

        input.ls()
        input.cd()
        obj = ROOT.TObject
        for key in ROOT.gDirectory.GetListOfKeys():
            input.cd()
            obj = key.ReadObj()
            #print obj.GetName()
            if obj.GetName() == 'tree':
                continue
            output.cd()
            #print key.GetName()
            obj.Write(key.GetName())

        inputTree = input.Get("tree")
        nEntries = inputTree.GetEntries()
        for branch in remove_branches:
          if branch and not branch.isspace():
            # print 'DROPPING BRANCHES LIKE',str(branch)
            inputTree.SetBranchStatus(str(branch), ROOT.kFALSE);

        output.cd()
        print '\n\t copy file: %s with cut: %s' %(inputFile,Acut)
        outputTree = inputTree.CopyTree(Acut)
        kEntries = outputTree.GetEntries()
        printc('blue','',"\t before cuts\t %s" %nEntries)
        printc('green','',"\t survived\t %s" %kEntries)
        outputTree.AutoSave()
        output.ls()
        print "Writing output file"
        output.Write()
        print "Closing output file"
        output.Close()
        print "Closing input file"
        input.Close()
Exemplo n.º 10
0
    def orderandadd(histo_dicts,setup):
        ordered_histo_dict = {}
	nloops = 0
        for sample in setup:
	    #print sample
            nSample = 0	
            for histo_dict in histo_dicts:
		nloops += 1
                if histo_dict.has_key(sample):
		    #print nSample
                    if nSample == 0:
                        ordered_histo_dict[sample] = histo_dict[sample].Clone()
                    else:
                        printc('magenta','','\t--> added %s to %s'%(sample,sample))
                        ordered_histo_dict[sample].Add(histo_dict[sample])
                    nSample += 1
        del histo_dicts
	#print "nloops: "
	#print nloops
	#print nSample
        return ordered_histo_dict 
Exemplo n.º 11
0
    def copySingleFile(self, whereToLaunch,inputFile,outputFile,skimmingCut,remove_branches):

        if self.debug:
            print("INPUT:", inputFile)
        input = ROOT.TFile.Open(inputFile,'read')
        if not input:
          print 'input file NOT EXISTING:',inputFile
          #input.Close()
          return
        try:
            __tmpPath = os.environ["TMPDIR"]
        except:
            __tmpPath = self.config.get('Directories', 'scratch')
        try:
            if not os.path.isdir(__tmpPath):
                os.makedirs(__tmpPath)
        except:
            pass
        outputFileName = outputFile.split('/')[-1]
        print 'outputFileName',__tmpPath+'/'+outputFileName
        output = ROOT.TFile.Open(__tmpPath+'/'+outputFileName,'recreate')

        inputTree = input.Get("tree")
        if not inputTree:
            inputTree = input.Get("Events")
        nEntries = inputTree.GetEntries()
        for branch in remove_branches:
          if branch and not branch.isspace():
            # print 'DROPPING BRANCHES LIKE',str(branch)
            inputTree.SetBranchStatus(str(branch), ROOT.kFALSE);

        output.cd()
        print '\n\t copy file: %s with cut: %s' %(inputFile,skimmingCut)
        outputTree = inputTree.CopyTree(skimmingCut)
        kEntries = outputTree.GetEntries()
        printc('blue','',"\t before cuts\t %s" %nEntries)
        printc('green','',"\t survived\t %s" %kEntries)
        outputTree.AutoSave()
        input.cd()
        obj = ROOT.TObject
        for key in ROOT.gDirectory.GetListOfKeys():
            input.cd()
            obj = key.ReadObj()
            # this contains the event tree, which will be copied skimmed only
            if obj.GetName() in  ['tree', 'Events']:
                continue
            if self.debug:
                print "DEBUG: clone object ", obj.GetName()
            # all other objects are just cloned
            output.cd()
            if obj.IsA().InheritsFrom(ROOT.TTree.Class()):
                objClone = obj.CloneTree(-1)
            else:
                objClone = obj
            objClone.Write(key.GetName())
        output.Write()
        output.Close()
        input.Close()
        tmpFile = __tmpPath+'/'+outputFileName
        print 'copy to final location:\x1b[34m', outputFile, '\x1b[0m'
        self.fileLocator.cp(source=tmpFile, target=outputFile)
        print 'checking if the copy worked'
        # check root file existence
        if self.fileLocator.exists(outputFile, attempts=2):
            if self.fileLocator.isValidRootFile(outputFile):
                if self.debug:
                    print('DEBUG: file exists and is good!')
            else:
                raise Exception('ERROR: file DOES NOT exist OR is corrupted!')
        else:
            raise Exception("self.fileLocator.exists(outputFile, attempts=2) failed: file DOES NOT exist")
              
        self.fileLocator.rm(tmpFile)
Exemplo n.º 12
0
    def copySingleFile(self, whereToLaunch, inputFile, outputFile, skimmingCut,
                       remove_branches):

        if self.debug:
            print("INPUT:", inputFile)
        input = ROOT.TFile.Open(inputFile, 'read')
        if not input:
            print 'input file NOT EXISTING:', inputFile
            #input.Close()
            return
        try:
            __tmpPath = os.environ["TMPDIR"]
        except:
            __tmpPath = self.config.get('Directories', 'scratch')
        try:
            if not os.path.isdir(__tmpPath):
                os.makedirs(__tmpPath)
        except:
            pass
        outputFileName = outputFile.split('/')[-1]
        print 'outputFileName', __tmpPath + '/' + outputFileName
        output = ROOT.TFile.Open(__tmpPath + '/' + outputFileName, 'recreate')

        inputTree = input.Get("tree")
        if not inputTree:
            inputTree = input.Get("Events")
        nEntries = inputTree.GetEntries()
        for branch in remove_branches:
            if branch and not branch.isspace():
                # print 'DROPPING BRANCHES LIKE',str(branch)
                inputTree.SetBranchStatus(str(branch), ROOT.kFALSE)

        output.cd()
        print '\n\t copy file: %s with cut: %s' % (inputFile, skimmingCut)
        outputTree = inputTree.CopyTree(skimmingCut)
        kEntries = outputTree.GetEntries()
        printc('blue', '', "\t before cuts\t %s" % nEntries)
        printc('green', '', "\t survived\t %s" % kEntries)
        outputTree.AutoSave()
        input.cd()
        obj = ROOT.TObject
        for key in ROOT.gDirectory.GetListOfKeys():
            input.cd()
            obj = key.ReadObj()
            # this contains the event tree, which will be copied skimmed only
            if obj.GetName() in ['tree', 'Events']:
                continue
            if self.debug:
                print "DEBUG: clone object ", obj.GetName()
            # all other objects are just cloned
            output.cd()
            if obj.IsA().InheritsFrom(ROOT.TTree.Class()):
                objClone = obj.CloneTree(-1)
            else:
                objClone = obj
            objClone.Write(key.GetName())
        output.Write()
        output.Close()
        input.Close()
        tmpFile = __tmpPath + '/' + outputFileName
        self.fileLocator.cp(source=tmpFile, target=outputFile)
        print 'copy to final location:\x1b[34m', outputFile, '\x1b[0m'
        self.fileLocator.rm(tmpFile)
Exemplo n.º 13
0
xMin=float(options[4])
xMax=float(options[5])
SIG=options[9]
data=options[10]
anType=options[11]
RCut=options[7]
setup=eval(config.get('LimitGeneral','setup'))
ROOToutname = options[6]
outpath=config.get('Directories','limits')
systematicsnaming=eval(config.get('LimitGeneral','systematicsnaming7TeV'))
if anaTag =='8TeV':
    systematicsnaming=eval(config.get('LimitGeneral','systematicsnaming8TeV'))
    MC_rescale_factor=1.0
elif anaTag =='7TeV':
    MC_rescale_factor=2.0
    printc('red','', 'I  RESCALE by 2.0! (from training)')  
else:
    print "What is your Analysis Tag in config? (anaTag)"
    sys.exit("What is your Analysis Tag in config? (anaTag)")
scaling=eval(config.get('LimitGeneral','scaling'))
rescaleSqrtN=eval(config.get('LimitGeneral','rescaleSqrtN'))
if 'RTight' in RCut:
    Datacradbin=options[10]
elif 'RMed' in RCut:
    Datacradbin=options[10]
else:
    Datacradbin=options[10]
blind=eval(config.get('LimitGeneral','blind'))
BKGlist = eval(config.get('LimitGeneral','BKG'))
#Groups for adding samples together
Group = eval(config.get('LimitGeneral','Group'))
Exemplo n.º 14
0
def copytree(pathIN,pathOUT,prefix,newprefix,folderName,Aprefix,Acut):
    print (pathIN,pathOUT,prefix,newprefix,folderName,Aprefix,Acut)
    from os import walk
    dirpath = ""
    filename = ""
    filenames = []
    print "##### COPY TREE - BEGIN ######"
    for (dirpath_, dirnames, filenames_) in walk(pathIN+'/'+folderName):
        for filename_ in filenames_:
            if 'root' in filename_ and not 'failed' in dirpath_:
                dirpath = dirpath_
                filename = filename_
                filenames = filenames_
                break
        if len(filenames)>0: break
    
    if dirpath == "":
        print "No .root files found in ",pathIN+'/'+folderName
        return
    
    pathIN = dirpath
    
    FileList = ROOT.TList();
    for filename in filenames:
        inputFile = '%s/%s ' %(pathIN,filename)
        input = ROOT.TFile.Open(inputFile,'read')
        outputFolder = "%s/%s/" %(pathOUT,folderName)
        outputFile = "%s/%s/%s" %(pathOUT,folderName,filename)
        try:
            os.mkdir(outputFolder)
        except:
            pass
        del_protocol = pathOUT
        del_protocol = del_protocol.replace('gsidcap://t3se01.psi.ch:22128/','srm://t3se01.psi.ch:8443/srm/managerv2?SFN=')
        del_protocol = del_protocol.replace('dcap://t3se01.psi.ch:22125/','srm://t3se01.psi.ch:8443/srm/managerv2?SFN=')
        del_protocol = del_protocol.replace('root://t3dcachedb03.psi.ch:1094/','srm://t3se01.psi.ch:8443/srm/managerv2?SFN=')
        print("cutting ",inputFile," ---> ",outputFile)
        if os.path.isfile(outputFile): 
            command = 'rm %s' %(outputFile)
            print(command)
            subprocess.call([command], shell=True)
        
        output = ROOT.TFile.Open(outputFile,'create')
        print "Writing file:",outputFile

        input.ls()
        input.cd()
        obj = ROOT.TObject
        for key in ROOT.gDirectory.GetListOfKeys():
            input.cd()
            obj = key.ReadObj()
            #print obj.GetName()
            if obj.GetName() == 'tree':
                continue
            output.cd()
            #print key.GetName()
            obj.Write(key.GetName())

        inputTree = input.Get("tree")
        nEntries = inputTree.GetEntries()
        output.cd()
        print '\n\t copy file: %s with cut: %s' %(folderName,Acut)
        outputTree = inputTree.CopyTree(Acut)
        kEntries = outputTree.GetEntries()
        printc('blue','',"\t before cuts\t %s" %nEntries)
        printc('green','',"\t survived\t %s" %kEntries)
        outputTree.AutoSave()
        output.ls()
        print "Writing output file"
        output.Write()
        print "Closing output file"
        output.Close()
        print "Closing input file"
        input.Close()

        print "##### COPY TREE - END ######"
        
#    for vhbbfolder in inputFile.split("/"):
#        if 'VHBB_HEPPY' in vhbbfolder:
#            break
    fileToMerge = outputFile[:outputFile.rfind("tree_")+5]+"*"+outputFile[outputFile.rfind(".root"):]
#    command = "hadd -f "+pathOUT+'/'+newprefix+vhbbfolder+".root "+fileToMerge
    command = "hadd -f "+pathOUT+'/'+newprefix+folderName+".root "+fileToMerge
    print command
    os.system(command)
Exemplo n.º 15
0
def copytree(pathIN,pathOUT,prefix,newprefix,folderName,Aprefix,Acut,whereToLaunch):
    '''
    List of variables
    pathIN: path of the input file containing the data
    pathOUT: path of the output files
    prefix: "prefix" variable from "samples_nosplit.cfg"
    newprefix: "newprefix" variable from "samples_nosplit.cfg"
    file: sample header (as DYJetsToLL_M-50_TuneZ2Star_8TeV-madgraph-tarball)
    Aprefix: empty string ''
    Acut: the sample cut as defined in "samples_nosplit.cfg"
    '''
    print (pathIN,pathOUT,prefix,newprefix,folderName,Aprefix,Acut)
    outputFolder = ""
    from os import walk
    dirpath = ""
    filename = ""
    filenames = []
    print "##### COPY TREE - BEGIN ######"
    if('pisa' in whereToLaunch):
      for (dirpath_, dirnames, filenames_) in walk(pathIN+'/'+folderName):
          for filename_ in filenames_:
              if 'root' in filename_ and not 'failed' in dirpath_:
                  dirpath = dirpath_
                  filename = filename_
                  filenames = filenames_
                  break
          if len(filenames)>0: break

      if dirpath == "":
          print "No .root files found in ",pathIN+'/'+folderName
          return

      pathIN = dirpath

    else:
      FOLDER = pathIN+'/'+folderName
      folder_prefix = ''
      if FOLDER.startswith('dcap://t3se01.psi.ch:22125'):
          FOLDER = FOLDER.replace('dcap://t3se01.psi.ch:22125','')
          folder_prefix = 'dcap://t3se01.psi.ch:22125'
      print 'pathIN+foldername is', pathIN+'/'+folderName
      print 'FOLDER is', FOLDER
      for (dirpath_, dirnames, filenames_) in walk(FOLDER):
          print 'yeah, I am in the for loop'
          print 'dirpath is', dirpath_
          print 'dirnames is', dirnames
          print 'filenames is', filenames_

#    FileList = ROOT.TList();
    for filename in filenames:
        inputFile = '%s/%s ' %(pathIN,filename)
        input = ROOT.TFile.Open(inputFile,'read')
        outputFolder = "%s/%s/" %(pathOUT,folderName)
        outputFile = "%s/%s/%s" %(pathOUT,folderName,filename)

        if('PSI' in whereToLaunch):
          print 'outputFile is', outputFile
          #Create the ouput folder if not existing
          print 'outputFolder is', outputFolder
          #mkdir_protocol = outputFolder.replace('root://t3dcachedb03.psi.ch:1094/','srm://t3se01.psi.ch/')
          mkdir_protocol = outputFolder.replace('root://t3dcachedb03.psi.ch:1094/','')
          _output_folder = ''
          print 'split is', mkdir_protocol.split('/')
          for _folder in mkdir_protocol.split('/'):
              print '_folder is', _folder
              #if mkdir_protocol.split('/').index(_folder) < 3: continue
              _output_folder += '/'+_folder
              print '_output_folder is', _output_folder
              if os.path.exists(_output_folder): print 'exists'
              else:
                  print 'Folder', _output_folder, 'doesn\'t exist\n. Creating it now'
                  command = 'srmmkdir srm://t3se01.psi.ch/' + _output_folder
                  subprocess.call([command], shell = True)
              if os.path.exists(_output_folder): print 'Folder', _output_folder, 'sucessfully created'


        try:
            os.mkdir(outputFolder)
        except:
            pass

        del_protocol = pathOUT
        if('PSI' in whereToLaunch):
          del_protocol = outputFile
          print 'del_protocol is', del_protocol

        del_protocol = del_protocol.replace('gsidcap://t3se01.psi.ch:22128/','srm://t3se01.psi.ch:8443/srm/managerv2?SFN=')
        del_protocol = del_protocol.replace('dcap://t3se01.psi.ch:22125/','srm://t3se01.psi.ch:8443/srm/managerv2?SFN=')
        del_protocol = del_protocol.replace('root://t3dcachedb03.psi.ch:1094/','srm://t3se01.psi.ch:8443/srm/managerv2?SFN=')
        print 'del_protocol is', del_protocol
        print "cutting ",inputFile," ---> ",outputFile

        if('pisa' in whereToLaunch):
          if os.path.isfile(outputFile):
              command = 'rm %s' %(outputFile)
              print(command)
              subprocess.call([command], shell=True)
        else:
          print 'path used to check if the file exists', del_protocol.replace('srm://t3se01.psi.ch:8443/srm/managerv2?SFN=','')
          if os.path.isfile(del_protocol.replace('srm://t3se01.psi.ch:8443/srm/managerv2?SFN=','')):
              print 'File', del_protocol.replace('srm://t3se01.psi.ch:8443/srm/managerv2?SFN=',''), 'already exists.\n Gonna delete it.'
              #command = 'rm %s' %(outputFile)
              command = 'srmrm %s' %(del_protocol)
              print(command)
              print(command)
              subprocess.call([command], shell=True)
              subprocess.call([command], shell=True)
              print 'DEBUG1'
          else: print 'FALSE'

# <<<<<<< HEAD
    # #!! get the input file, remove the previous output files
    # input = ROOT.TFile.Open("%s/%s%s.root" %(pathIN,prefix,file),'read')
    # print ("%s/%s%s%s.root" %(pathOUT,newprefix,Aprefix,file),'recreate')
    # del_protocol = pathOUT
    # del_protocol = del_protocol.replace('gsidcap://t3se01.psi.ch:22128/','srm://t3se01.psi.ch:8443/srm/managerv2?SFN=')
    # del_protocol = del_protocol.replace('dcap://t3se01.psi.ch:22125/','srm://t3se01.psi.ch:8443/srm/managerv2?SFN=')
    # del_protocol = del_protocol.replace('root://t3dcachedb03.psi.ch:1094/','srm://t3se01.psi.ch:8443/srm/managerv2?SFN=')
    # # RECURSIVELY CREATE REMOTE FOLDER ON PSI SE, but only up to 3 new levels
    # if del_protocol.find('srm://t3se01.psi.ch:8443/srm/managerv2?SFN=') != -1:
        # print "Remote folder contain ssrm://t3se01.psi.ch:8443/srm/managerv2?SFN="
        # mkdir_command = del_protocol.replace('srm://t3se01.psi.ch:8443/srm/managerv2?SFN=','srm://t3se01.psi.ch/')
        # mkdir_command1 = mkdir_command.rsplit('/',1)[0]
        # mkdir_command2 = mkdir_command1.rsplit('/',1)[0]
        # mkdir_command3 = mkdir_command2.rsplit('/',1)[0]
        # my_user = os.popen("whoami").read().strip('\n').strip('\r')+'/'
        # if my_user in mkdir_command3:
          # print 'mkdir_command3',mkdir_command3
          # subprocess.call(['srmmkdir '+mkdir_command3], shell=True)# delete the files already created ?
        # if my_user in mkdir_command2:
          # print 'mkdir_command2',mkdir_command2
          # subprocess.call(['srmmkdir '+mkdir_command2], shell=True)# delete the files already created ?
        # if my_user in mkdir_command1:
          # print 'mkdir_command1',mkdir_command1
          # subprocess.call(['srmmkdir '+mkdir_command1], shell=True)# delete the files already created ?
        # if my_user in mkdir_command:
          # print 'mkdir_command',mkdir_command
          # subprocess.call(['srmmkdir '+mkdir_command], shell=True)# delete the files already created ?
        # command = 'srmrm %s/%s%s%s.root' %(del_protocol,newprefix,Aprefix,file)# command to delete previous files ?
        # print(command)
        # subprocess.call([command], shell=True)# delete the files already created ?
    # else:
        # print "Remote folder doesn't contain ssrm://t3se01.psi.ch:8443/srm/managerv2?SFN="
        # print 'mkdir_command', 'mkdir '+ del_protocol
        # subprocess.call(['mkdir '+ del_protocol], shell=True)# delete the files already created ?
        # command = 'rm %s/%s%s%s.root' %(del_protocol,newprefix,Aprefix,file)# command to delete previous files ?
        # print(command)
        # subprocess.call([command], shell=True)# delete the files already created ?

    # output = ROOT.TFile.Open("%s/%s%s%s.root" %(pathOUT,newprefix,Aprefix,file),'create')
    # input.ls()
    # input.cd()

    # #read the content of the ROOT file
    # #copy the directorys of the tree
    # obj = ROOT.TObject
    # for key in ROOT.gDirectory.GetListOfKeys():
# =======
        output = ROOT.TFile.Open(outputFile,'create')
        print "Writing file:",outputFile

        input.ls()
        input.cd()
        obj = ROOT.TObject
        for key in ROOT.gDirectory.GetListOfKeys():
            input.cd()
            obj = key.ReadObj()
            #print obj.GetName()
            if obj.GetName() == 'tree':
                continue
            output.cd()
            #print key.GetName()
            obj.Write(key.GetName())

        inputTree = input.Get("tree")
        nEntries = inputTree.GetEntries()
        output.cd()
        print '\n\t copy file: %s with cut: %s' %(folderName,Acut)
        outputTree = inputTree.CopyTree(Acut)
        kEntries = outputTree.GetEntries()
        printc('blue','',"\t before cuts\t %s" %nEntries)
        printc('green','',"\t survived\t %s" %kEntries)
        outputTree.AutoSave()
        output.ls()
        print "Writing output file"
        output.Write()
        print "Closing output file"
        output.Close()
        print "Closing input file"
        input.Close()

# <<<<<<< HEAD
    # #copy the tree with the additional cuts
    # inputTree = input.Get("tree")
    # nEntries = inputTree.GetEntries()
    # output.cd()
    # print '\n\t copy file: %s with cut: %s' %(file,Acut)
    # outputTree = inputTree.CopyTree(Acut)
    # kEntries = outputTree.GetEntries()
    # printc('blue','',"\t before cuts\t %s" %nEntries)
    # printc('green','',"\t survived\t %s" %kEntries)
    # outputTree.AutoSave()
    # output.ls()
    # print "Writing output file"
    # output.Write()
    # print "Closing output file"
    # output.Close()
    # print "Closing input file"
    # input.Close()
# =======
        print "##### COPY TREE - END ######"
Exemplo n.º 16
0
def copytree(pathIN,pathOUT,prefix,newprefix,file,Aprefix,Acut):
    ''' 
    List of variables
    pathIN: path of the input file containing the data
    pathOUT: path of the output files 
    prefix: "prefix" variable from "samples_nosplit.cfg" 
    newprefix: "newprefix" variable from "samples_nosplit.cfg" 
    file: sample header (as DYJetsToLL_M-50_TuneZ2Star_8TeV-madgraph-tarball)
    Aprefix: empty string ''
    Acut: the sample cut as defined in "samples_nosplit.cfg"
    '''

    #!!print input parameters
    print "##### COPY TREE - BEGIN ######"
    print "Input File : %s/%s%s.root " %(pathIN,prefix,file)
    print "Output File : %s/%s%s%s.root" %(pathOUT,newprefix,Aprefix,file)
        
    #!! get the input file, remove the previous output files 
    input = ROOT.TFile.Open("%s/%s%s.root" %(pathIN,prefix,file),'read')
    print ("%s/%s%s%s.root" %(pathOUT,newprefix,Aprefix,file),'recreate')
    del_protocol = pathOUT
    del_protocol = del_protocol.replace('gsidcap://t3se01.psi.ch:22128/','srm://t3se01.psi.ch:8443/srm/managerv2?SFN=')
    del_protocol = del_protocol.replace('dcap://t3se01.psi.ch:22125/','srm://t3se01.psi.ch:8443/srm/managerv2?SFN=')
    del_protocol = del_protocol.replace('root://t3dcachedb03.psi.ch:1094/','srm://t3se01.psi.ch:8443/srm/managerv2?SFN=')
    command = 'srmrm %s/%s%s%s.root' %(del_protocol,newprefix,Aprefix,file)# command to delete previous files ?
    print(command)
    subprocess.call([command], shell=True)# delete the files already created ?     
    output = ROOT.TFile.Open("%s/%s%s%s.root" %(pathOUT,newprefix,Aprefix,file),'create')
    input.ls()
    input.cd()

    #read the content of the ROOT file 
    #copy the directorys of the tree
    obj = ROOT.TObject
    for key in ROOT.gDirectory.GetListOfKeys():
        input.cd()
        obj = key.ReadObj()
        #print obj.GetName()
        if obj.GetName() == 'tree':
            continue
        output.cd()
        #print key.GetName()
        obj.Write(key.GetName())

    #copy the tree with the additional cuts
    inputTree = input.Get("tree")
    nEntries = inputTree.GetEntries()
    output.cd()
    print '\n\t copy file: %s with cut: %s' %(file,Acut)
    outputTree = inputTree.CopyTree(Acut)
    kEntries = outputTree.GetEntries()
    printc('blue','',"\t before cuts\t %s" %nEntries)
    printc('green','',"\t survived\t %s" %kEntries)
    outputTree.AutoSave()
    output.ls()
    print "Writing output file"
    output.Write()
    print "Closing output file"
    output.Close()
    print "Closing input file"
    input.Close()

    print "##### COPY TREE - END ######"