babymaker  e95a6a9342d4604277fe7cc6149b6b5b24447d89
rename_data_eras.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 ###### Script that adds the era to data root files that have been split by run (and have the run number in the name)
4 import os, sys, subprocess
5 import pprint
6 import glob
7 import argparse
8 
9 ## Parsing input arguments
10 parser = argparse.ArgumentParser(description='Process some integers.')
11 parser.add_argument("-i", "--infolder", help="Folder to find root files in",
12  default="/net/cms29/cms29r0/babymaker/babies/2016_11_21/data/unskimmed/")
13 parser.add_argument("-o", "--outfolder", help="Folder to write files to",
14  default="/net/cms29/cms29r0/babymaker/babies/2016_11_21/data/eras/")
15 args = parser.parse_args()
16 args.outfolder = args.outfolder+"/"
17 
18 eras = [["RunB", [272007, 275376]], ["RunC", [275657, 276283]], ["RunD", [276315, 276811]], ["RunE", [276831, 277420]],
19  ["RunF", [277772, 278808]], ["RunG", [278820, 280385]], ["RunH", [280919, 28038500]]] ## Added zeros to runH
20 
21 ## Create output folder
22 if not os.path.exists(args.outfolder):
23  print "\nCreating "+args.outfolder
24  os.system("mkdir -p "+args.outfolder)
25 
26 noera_runs = []
27 nfiles = 0
28 files = glob.glob(args.infolder+'/*.root')
29 for file in files:
30  outfile = file.replace(args.infolder, args.outfolder)
31 
32  ## Parsing run from file name
33  run = file.split('runs')[-1]
34  run = int(run.split('.root')[0])
35 
36  ## Finding era for run
37  found_era = False
38  for era in eras:
39  if run >= era[1][0] and run <= era[1][1]:
40  outfile = outfile.replace("baby_", "baby_"+era[0]+"_")
41  found_era = True
42  break
43  if not found_era: noera_runs.append(run)
44 
45  ## Moving file
46  cmd = "mv "+file+" "+outfile
47  os.system(cmd)
48  nfiles += 1
49 
50 
51 ## Final printouts
52 if len(noera_runs)>0:
53  print "\nNot found era for runs "
54  print noera_runs
55 print "\nCopied and renamed "+str(nfiles)+" files into "+args.outfolder+"\n\n"
56