make_metadata35t_declare_Slice.sh
Go to the documentation of this file.
1 #!/bin/bash
2 
3 #------------------------------------------------------------------
4 #
5 # Name: make_metadata_35t.sh
6 #
7 # Purpose: Using an initial .json sam metadata file as an example,
8 # make additional metadata files for all .root files in a
9 # directory.
10 #
11 # Usage:
12 #
13 # make_metadata35t.sh <rootfiledirectory> <failedfiledirectory>
14 # if the metadata extraction procedure fails, then move the rootfile to the failedfiledirectory
15 #
16 # Tom Junk, using the make_json.sh example from Qizhong Li
17 #------------------------------------------------------------------
18 
19 source /grid/fermiapp/products/dune/setup_dune.sh
20 setup dunetpc v06_19_00 -q e10:prof
21 
22 rdir=''
23 fdir=''
24 DHDIR=''
25 filelistdir=''
26 
27 function dohelp {
28  echo "Usage: make_metadata35t_declare.sh <rootfiledirectory> <failedfiledirectory> <dirtofindexamplejson> <dirforfilelist>"
29 }
30 
31 # Parse arguments.
32 
33 if [ $# -eq 0 ]; then
34  dohelp
35  exit
36 fi
37 
38 if [ $1 = "--help" ]; then
39  dohelp
40  exit
41 fi
42 
43 if [ $# -eq 4 ]; then
44  rdir=$1
45  fdir=$2
46  DHDIR=$3
47  filelistdir=$4
48 fi
49 example=$DHDIR/exampleSlice.json
50 
51 cd $rdir
52 
53 ls | grep lbne_r | grep '.root$' | while read root
54 do
55 
56  json=$root.json
57 
58  # If this .json file already exist, skip this file.
59 
60  if [ -f $json ]; then
61  echo "$json already exists, so deleting it"
62  rm -f *.json
63  fi
64  echo " "
65  echo "Making $json."
66 
67 
68  size=`stat -c %s $root`
69  if [ $? -ne 0 ]; then
70  mv $root $fdir
71  echo "$0 Failed to get filesize for $root"
72  continue
73  fi
74 
75  nev=`echo "Events->GetEntriesFast()" | root -l -b $root 2>&1 | tail -1 | cut -d')' -f2`
76  if [ $? -ne 0 ]; then
77  mv $root $fdir
78  echo "$0 Failed to get event count for $root"
79  continue
80  fi
81 
82  run=`echo $root | cut -d_ -f2 | cut -c2- | awk '{printf "%d\n",$0}'`
83  if [ $? -ne 0 ]; then
84  mv $root $fdir
85  echo "$0 Failed to get run number for $root"
86  continue
87  fi
88 
89  echo "Got all my parameters"
90 
91  egrep -v 'file_name|file_size|event_count|last_event|runs' $example | \
92  awk '{print $0}/^{ *$/{printf " \"file_name\": \"%s\",\n \"file_size\": %d,\n \"event_count\": %d,\n \"last_event\": %d,\n \"runs\": [ [ %d, \"test\" ] ],\n",'"\"$root\",${size},${nev},${nev},${run}}" > Tempjson1.txt
93 
94  echo "Next step"
95 
96  if [ $? -ne 0 ]; then
97  mv $root $fdir
98  echo "$0 Failed to replace metadata for $root"
99  mv $json $fdir/$json.failed
100  continue
101  fi
102 
103  echo "Now to set the parent files"
104 
105  Myrun=$run
106  while [ ${#Myrun} -ne 6 ];
107  do
108  Myrun="0"$Myrun
109  done
110  echo '"parents": [' > TempParent1.txt
111  samweb list-definition-files rawdata35t_run_$Myrun >> TempParent2.txt
112  cat TempParent1.txt TempParent2.txt > TempParent3.txt
113  lines=$(wc -l < "TempParent3.txt")
114  echo "TempParent3.txt has $lines lines"
115  sed 's/lbne/{"file_name": "lbne/' TempParent3.txt > TempParent4.txt
116  sed 's/.root/.root"},/' TempParent4.txt > TempParent5.txt
117  sed -e "$lines s/,/\n],/" < TempParent5.txt > Tempjson2.txt
118 
119  echo "Done the parent file"
120 
121  $DHDIR/dbjson.py $run >> Tempjson3.txt
122  cat Tempjson1.txt Tempjson2.txt Tempjson3.txt > $json
123  rm Temp*
124  if [ $? -ne 0 ]; then
125  mv $root $fdir
126  echo "$0 Failed to query online database for $root"
127  mv $json $fdir/$json.failed
128  continue
129  fi
130 
131  echo "Done all I'm happy to do so far......"
132 
133 
134  echo "Declaring metadata to SAM: $json"
135  #samweb -e dune declare-file --cert=/dune/app/home/dunepro/trj/service_cert_dec9_2015/duneprocert_dec2015.pem --key=/dune/app/home/dunepro/trj/service_cert_dec9_2015/duneprokey_dec2015.pem $json
136  samweb -e dune declare-file $json
137  echo "Tried to declare"
138  if [ $? -ne 0 ]; then
139  mv $root $fdir
140  echo "$0 Failed to declare metadata to SAM"
141  mv $json $fdir/$json.failed
142  continue
143  fi
144  echo "Finished declaring $json"
145 
146  mv $root /pnfs/dune/scratch/dunepro/dropbox/data/
147  echo "Moved $root to dropbox"
148 
149  rm -f TempRunning.txt
150  echo "$root $size $run $nev" >> $filelistdir/GoodFileList.txt
151  RunningFile=/dune/data2/lbnepro/AutoSlice/GoodFileList/RunningList.txt
152  printf "%06d\n" $run >> $RunningFile
153  sort --version-sort $RunningFile > TempRunning.txt
154  uniq -u TempRunning.txt > $RunningFile
155  rm -f TempRunning.txt
156  echo "Removed $run from the Running File List in $RunningFile"
157 
158 done
159 
160