make_metadata35t_declare.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 v04_29_00 -q e9: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/example2.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."
62  else
63  echo "Making $json."
64 
65 
66  size=`stat -c %s $root`
67  if [ $? -ne 0 ]; then
68  mv $root $fdir
69  echo "$0 Failed to get filesize for $root"
70  continue
71  fi
72 
73  nev=`echo "Events->GetEntriesFast()" | root -l -b $root 2>&1 | tail -1 | cut -d')' -f2`
74  if [ $? -ne 0 ]; then
75  mv $root $fdir
76  echo "$0 Failed to get event count for $root"
77  continue
78  fi
79 
80  run=`echo $root | cut -d_ -f2 | cut -c2- | awk '{printf "%d\n",$0}'`
81  if [ $? -ne 0 ]; then
82  mv $root $fdir
83  echo "$0 Failed to get run number for $root"
84  continue
85  fi
86 
87 
88  egrep -v 'file_name|file_size|event_count|last_event|runs' $example | \
89  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}}" > $json
90 
91  if [ $? -ne 0 ]; then
92  mv $root $fdir
93  echo "$0 Failed to replace metadata for $root"
94  mv $json $fdir/$json.failed
95  continue
96  fi
97 
98 
99  $DHDIR/dbjson.py $run >> $json
100  if [ $? -ne 0 ]; then
101  mv $root $fdir
102  echo "$0 Failed to query online database for $root"
103  mv $json $fdir/$json.failed
104  continue
105  fi
106 
107 
108  echo "Declaring metadata to SAM: $json"
109 # old certificate -- switch to new one Dec. 11, 2015
110 # samweb -e lbne declare-file --cert=${DHDIR}/duneprocert.pem --key=${DHDIR}/duneprokey.pem $json
111  samweb -e lbne declare-file --cert=${DHDIR}/duneprocert_dec2015.pem --key=${DHDIR}/duneprokey_dec2015.pem $json
112  if [ $? -ne 0 ]; then
113  mv $root $fdir
114  echo "$0 Failed to declare metadata to SAM"
115  mv $json $fdir/$json.failed
116  continue
117  fi
118  echo "Finished declaring $json"
119 
120  echo "$root $size $run $nev" >> $filelistdir/filelist.txt
121  cp $filelistdir/filelist.txt /web/sites/dune-data.fnal.gov/htdocs/data/35t/testfiles/
122 
123  fi
124 
125 done
126 
127