make_json_lbne.sh
Go to the documentation of this file.
1 #! /bin/bash
2 #------------------------------------------------------------------
3 #
4 # Name: make_json.sh
5 #
6 # Purpose: Using an initial .json sam metadata file as an example,
7 # make additional metadata files for all .root files in a
8 # directory.
9 #
10 # Usage:
11 #
12 # make_json.sh <example-json-file>
13 #
14 # Qizhong Li
15 #------------------------------------------------------------------
16 
17 # Help function.
18 
19 function dohelp {
20  echo "Usage: make_json.sh <example-json-file>"
21 }
22 
23 # Parse arguments.
24 
25 if [ $# -eq 0 ]; then
26  dohelp
27  exit
28 fi
29 
30 example=''
31 
32 while [ $# -gt 0 ]; do
33  case "$1" in
34 
35  # Help.
36  -h|--help )
37  dohelp
38  exit
39  ;;
40 
41  # Example .json file.
42  * )
43  if [ x$example = x ]; then
44  example=$1
45  else
46  echo "Too many arguments."
47  dohelp
48  exit 1
49  fi
50 
51  esac
52  shift
53 
54 done
55 
56 # Make sure example file exists.
57 
58 if [ ! -f $example ]; then
59  echo "Example file $example does not exist."
60  exit 1
61 fi
62 
63 # Loop over .root files.
64 
65 ls | grep '.root$' | while read root
66 do
67 
68  # Construct the name of the .json file corresponding to this root file.
69 
70  json=${root}.json
71 
72  # If this .json file already exist, skip this file.
73 
74  if [ -f $json ]; then
75  echo "$json already exists."
76  else
77  echo "Making $json."
78 
79  # Get the size in bytes of this root file.
80 
81  size=`stat -c %s $root`
82 
83  # Get number of events.
84 
85  nev=`echo "Events->GetEntriesFast()" | root -l $root 2>&1 | tail -1 | cut -d')' -f2`
86 
87  # Get the run number.
88 
89  run=`echo $root | cut -d_ -f2 | cut -c2- | awk '{printf "%d\n",$0}'`
90 
91  # Generate new .json file using example.
92 
93  egrep -v 'file_name|file_size|event_count|last_event|runs' $example | \
94  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
95  fi
96 
97 done