ATO-37 - STEER/CDB/AliOCDBtoolkit.cx - catch exception in case not valid OCDB entri...
[u/mrichter/AliRoot.git] / PWGPP / CalibMacros / AliOCDBtoolkit.sh
1 #!/bin/bash
2 #
3 # Shell script to compare content of the OCDB entries.
4 #
5 # ocdbMakeTable() 
6 #       Usage: bash $inputFile $flag $outputFile
7 # dumpObject()
8 #       Usage: bash $inputFile $object_name $dump_type [XML/MI] $outfile
9 # diffObject
10 #       Usage: bash $inputFile1 $inputFile2 $object_name $dump_type [XML/MI] $outfile
11 # Example usage:  see example functions below
12
13 ocdbMakeTable(){
14 #
15 # create a text file with the OCDB setupt descriptors#
16 #
17 # Input: 
18 #   $1 inputFile name
19 #   $2 flag type of the input file
20 #   flags: 
21 #   log file = "log"
22 #   AliESDs.root = "ESD"
23 #   galice.root = "MC"
24 # Output:
25 #   $3 output file name
26     if [ $# -lt 3 ] ; then
27         echo "Usage: $0 \$inputFile \$flag \$outputFile"
28         return 1
29     fi
30     export ALIROOT_FORCE_COREDUMP=1
31     local inFile=${1}
32     local inFlag=${2}
33     local outFile=${3}
34     shift 3
35     if [ ! -f ${inFile} ] ; then 
36         echo ${inFile} not found!
37         return 1
38     fi
39     if [ -f ${outFile} ] ; then 
40         >${outFile}
41     fi
42
43     tmpscript=$(mktemp)
44     cat > ${tmpscript} <<HEREDOC
45         {
46             AliOCDBtoolkit::DumpOCDBAsTxt("${inFile}","${inFlag}","${outFile}");
47         }
48 HEREDOC
49
50     aliroot -l -q -b ${tmpscript} 
51     sleep 60 && rm ${tmpscript} &
52     return 1
53 }
54
55
56 dumpObject(){
57 #
58 #
59 #  Input:
60 #    $1 path
61 #    $2 obj name 
62 #    $3 type of the dump (XML or MI recursive dump )
63 #  Output:
64 #    $4 output file name   
65     export ALIROOT_FORCE_COREDUMP=1
66     if [ $# -lt 4 ] ; then
67         echo "Usage: $0 \$inputFile \$object_name \$dump_type [XML/MI] \$outfile"
68         return 1
69     fi
70     local inFile=${1}
71     local fobject=${2}
72     local ftype=${3}
73     local outFile=${4}
74     shift 4
75     if [ ! -f ${inFile} ] ; then 
76         echo ${inFile} not found!
77         return 1
78     fi
79     if [ -f ${outFile} ] ; then 
80         >${outFile}
81     fi
82     if [ ${ftype} = "XML" ] ; then
83         isXML=kTRUE
84     elif [ ${ftype} = "MI" ] ; then
85         isXML=kFALSE
86     else
87         echo "option ${ftype} not recognized! Use \"XML\" or \"MI\"!"
88         return 1
89     fi
90     tmpscript=$(mktemp)
91     cat > ${tmpscript} <<HEREDOC
92         {
93             AliOCDBtoolkit::DumpOCDBFile("${inFile}","${outFile}",1,${isXML});
94         }
95 HEREDOC
96
97     aliroot -l -q -b ${tmpscript} 
98     sleep 60 && rm ${tmpscript} &
99     return 1
100 }
101
102 diffObject(){
103 #
104 #
105 #  Input:
106 #    $1 path0
107 #    $2 path1
108 #    $3 obj name 
109 #    $4 type of the dump (xml or MI recursive dump )
110 #  Output:
111 #    $5 output diff file name   
112     export ALIROOT_FORCE_COREDUMP=1
113     if [ $# -lt 5 ] ; then
114         echo "Usage: $0 \$inputFile1 \$inputFile2 \$object_name \$dump_type [XML/MI] \$outfile"
115         return 1
116     fi
117     local inFile1=${1}
118     local inFile2=${2}
119     local fobject=${3}
120     local ftype=${4}
121     local outFile=${5}
122     shift 5
123     local tmp1=$(mktemp)
124     local tmp2=$(mktemp)
125     if [ ${ftype} = "XML" ] ; then
126         isXML=kTRUE
127         tmp1="${tmp1}.xml"
128         tmp2="${tmp2}.xml"
129     elif [ ${ftype} = "MI" ] ; then
130         isXML=kFALSE
131     else
132         echo "option ${ftype} not recognized! Use \"XML\" or \"MI\"!"
133         return 1
134     fi
135     dumpObject ${inFile1} ${fobject} ${ftype} ${tmp1%.xml}
136     dumpObject ${inFile2} ${fobject} ${ftype} ${tmp2%.xml}
137     diff ${tmp1} ${tmp2} >${outFile}
138     rm ${tmp1} ${tmp2} 2>/dev/null
139     rm "${tmp1}.xml" "${tmp2}.xml" 2>/dev/null
140     return 1
141 }
142
143 dumpOCDBDiffTable(){
144 #
145 # Dump  differences between the OCDB tables -comparison based on the hash values of OCDB entries
146 # Input:
147 #   $1  - list 1
148 #   $2  - list 2
149 # Output:
150 #   difference is stdout
151     export ALIROOT_FORCE_COREDUMP=1
152     list1=$1
153     list2=$2
154     shift 2
155     cdbName=$(cut -f1 $list1)
156     for i in $cdbName ; do
157         line1="$(grep $i $list1)"
158         line2="$(grep $i $list2)"
159         if [ -z "${line2}" ] ; then
160             echo $i not found in $list2!
161             continue
162         fi
163         match1=$(echo $line1 | cut -d' ' -f 5)
164         match2=$(echo $line2 | cut -d' ' -f 5)
165         if [ "$match1" -ne "$match2" ] ; then
166             echo $i doesnt match:
167             echo $(echo $line1| awk '{print $2 "/" $3}') \#hash: $match1
168             echo $(echo $line2| awk '{print $2 "/" $3}') \#hash: $match2
169             echo 
170         fi
171     done
172 }
173
174
175
176
177
178 #
179 # Example usage+ developer test routines. 
180 #
181
182 example1(){
183     ocdbMakeTable "/hera/alice/jwagner/simulations/benchmark/aliroot_tests/ppbench/rec.log" "log" "testout"
184 }
185 example2(){
186     dumpObject "/hera/alice/jwagner/OCDB/temp/TPC/Calib/RecoParam/Run0_999999999_v0_s0.root" "object" "XML" "testout2XML"
187     dumpObject "/hera/alice/jwagner/OCDB/temp/TPC/Calib/RecoParam/Run0_999999999_v0_s0.root" "object" "MI" "testout2MI" 
188 }
189 example3(){
190     file1="/hera/alice/jwagner/OCDB/temp/TPC/Calib/RecoParam/Run0_999999999_v0_s0.root"
191     file2="$ALICE_ROOT/OCDB/TPC/Calib/RecoParam/Run0_999999999_v0_s0.root"
192     diffObject ${file1} ${file2} "object" "MI" "testdiffMI"
193     diffObject ${file1} ${file2} "object" "XML" "testdiffXML"
194 }
195
196 developerTest(){
197     source /hera/alice/jwagner/software/aliroot/loadMyAliroot.sh TPCdev
198     example1
199     example2
200     example3
201 }