#! /bin/tcsh
cmsenv

echo " START Geometry Validation"
set loctag = ''
if ($#argv == 0) then
    set gtag="MC_31X_V8::All"
    set geometry="GeometryIdeal"
else if($#argv == 1) then
    set gtag=`echo ${1}`
    set geometry="GeometryIdeal"
else if ($#argv == 2) then
    set gtag=`echo ${1}`
    set geometry=`echo ${2}`
else if ($#argv == 3) then 
    set gtag=`echo ${1}`
    set geometry=`echo ${2}`
    set loctag = `echo ${3}`
endif
echo geometry = ${geometry}
#global tag gtag is assumed to be of the form GeometryWORD such as GeometryExtended or GeometryIdeal
#as of 3.4.X loaded objects in the DB, these correspond to condlabels Extended, Ideal, etc...
set condlabel = `(echo $geometry | sed '{s/Geometry//g}')`
echo ${condlabel} " geometry label from db"
echo "Check out and compile the needed packages"
#The following are examples which should be changed if you are testing new code
#or scripts 
#required:
addpkg Geometry/TrackerGeometryBuilder
addpkg Geometry/RPCGeometry
addpkg Geometry/DTGeometry
addpkg Geometry/CSCGeometry
addpkg Geometry/CaloEventSetup
addpkg GeometryReaders/XMLIdealGeometryESSource
addpkg DetectorDescription/Schema
addpkg Configuration/StandardSequences
addpkg CondTools/Geometry

#add cvs updates here for REAL LOCAL testing
# or change the above to cvs co -r yourtag to make sure new files get in :)
# e.g. cvs update -A Geometry/DTGeometry/test/*.py
# e.g. cp -r /from/my/work/area/Geometry/* Geometry/.
#cvs update -r V03-02-05-01 Validation/Geometry
#cvs update -r V03-09-10-02 Geometry/CaloEventSetup 
#cvs update -r V01-02-21-07 GeometryReaders/XMLIdealGeometryESSource 

#cvs update -r V00-03-11 Geometry/DTGeometry 
#cvs update -r V01-01-25 Geometry/RPCGeometry 
#cvs update -r V01-05-06 Geometry/CSCGeometry 
#cvs update -r V05-00-11 Geometry/TrackerGeometryBuilder

#add cvs updates here for REAL LOCAL testing
# e.g. cvs update -A Geometry/DTGeometry/test/*.py
# e.g. cp -r /from/my/work/area/Geometry/* Geometry/.
#cvs update -r V03-02-05-01 Validation/Geometry
#cvs update -r V03-09-10-02 Geometry/CaloEventSetup 
#cvs update -r V01-02-21-07 GeometryReaders/XMLIdealGeometryESSource 

#cvs update -r V00-03-11 Geometry/DTGeometry 
#cvs update -r V01-01-25 Geometry/RPCGeometry 
#cvs update -r V01-05-06 Geometry/CSCGeometry 
#cvs update -r V05-00-11 Geometry/TrackerGeometryBuilder

if ($loctag != '') then 
    cd CondCore/ESSources/python
    set escloctag = `(echo $loctag | sed '{s/\//\\\//g}')`
    sed -i "{s/frontier:\/\/FrontierProd\/CMS_CONDITIONS/${escloctag}/g}" CondDBESSource_cfi.py 
endif

cd $CMSSW_BASE/src
scram build

echo "Finish the setup of release working area"

mkdir workArea
cd workArea
set myDir=`pwd`
echo $myDir

cp $CMSSW_BASE/src/CondTools/Geometry/test/geometryxmlwriter.py .
echo $geometry
sed -i "{s/GeometryExtended/${geometry}/}" geometryxmlwriter.py >  GeometryValidation.log
cmsRun geometryxmlwriter.py >>  GeometryValidation.log

cp $CMSSW_BASE/src/CondTools/Geometry/test/geometrywriter.py .
sed -i "{s/GeometryExtended/${geometry}/}" geometrywriter.py >>  GeometryValidation.log
sed -i "{s/geTagXX.xml/fred.xml/g}" geometrywriter.py >>  GeometryValidation.log
cmsRun geometrywriter.py >>  GeometryValidation.log
if ( -e myfile.db ) then
    echo "The local DB file is present" | tee -a GeometryValidation.log
else
    echo "ERROR the local DB file is not present" | tee -a GeometryValidation.log
    exit
endif

echo "Start compare the content of GT and the local DB" | tee -a GeometryValidation.log

# (MEC:1) The following two tests with the diff below them actually make
# sure that the Global Tag (GT) and Local DB XML file blobs are fine... 
# meaning that the full simulation geometry source is fine (XML blob)
# as well as the reco geometries.
cp $CMSSW_BASE/src/CondTools/Geometry/test/geometrytest_local.py .
sed -i "/FrontierConditions_GlobalTag_cff/ a\process.GlobalTag.globaltag = '${gtag}'" geometrytest_local.py >> GeometryValidation.log
cmsRun geometrytest_local.py > outLocalDB.log
if ( -s outLocalDB.log ) then
    echo "Local DB access run ok" | tee -a GeometryValidation.log
else
    echo "ERROR the output of Local DB access test is empty" | tee -a GeometryValidation.log
    exit
endif

cp $CMSSW_BASE/src/CondTools/Geometry/test/geometrytest_db.py .
sed -i "{/process.GlobalTag.globaltag/d}" geometrytest_db.py >> GeometryValidation.log 
sed -i "/FrontierConditions_GlobalTag_cff/ a\process.GlobalTag.globaltag = '${gtag}'" geometrytest_db.py >> GeometryValidation.log
sed -i "/FrontierConditions_GlobalTag_cff/ a\process.XMLFromDBSource.label = cms.string('${condlabel}')" geometrytest_db.py >> GeometryValidation.log 
cmsRun geometrytest_db.py > outGTDB.log
if ( -s outGTDB.log ) then
    echo "GT DB access run ok" | tee -a GeometryValidation.log
else
    echo "ERROR the output of GT DB access test is empty" | tee -a GeometryValidation.log
    exit
endif

diff outLocalDB.log outGTDB.log > logDiffLocalvsGT.log
if ( -s logLocalvsGTDiff.log ) then
    echo "WARNING THE CONTENT OF GLOBAL TAG IS DIFFERENT WHIT RESPECT TO THE LOCAL DB FILE" | tee -a GeometryValidation.log
endif

echo "End compare the content of GT and the local DB" | tee -a GeometryValidation.log

echo "Start Tracker RECO geometry validation" | tee -a GeometryValidation.log

mkdir tkdb
mkdir tkdblocal
mkdir tkddd

cp myfile.db tkdblocal

cd tkdb
cp $CMSSW_BASE/src/Geometry/TrackerGeometryBuilder/test/trackerModuleInfoDB_cfg.py .
sed -i "{/process.GlobalTag.globaltag/d}" trackerModuleInfoDB_cfg.py >> ../GeometryValidation.log
sed -i "/FrontierConditions_GlobalTag_cff/ a\process.GlobalTag.globaltag = '${gtag}'" trackerModuleInfoDB_cfg.py >> ../GeometryValidation.log 
sed -i "/FrontierConditions_GlobalTag_cff/ a\process.XMLFromDBSource.label = cms.string('${condlabel}')" trackerModuleInfoDB_cfg.py >> ../GeometryValidation.log 
cmsRun trackerModuleInfoDB_cfg.py >> ../GeometryValidation.log
mv trackerModuleInfoDB_cfg.py ../
if ( -s ModuleInfo.log ) then
    echo "TK test from DB run ok" | tee -a ../GeometryValidation.log
else
    echo "ERROR the output of TK test from DB is empty" | tee -a ../GeometryValidation.log
    exit
endif

cd ../tkdblocal
cp $CMSSW_BASE/src/Geometry/TrackerGeometryBuilder/test/trackerModuleInfoLocalDB_cfg.py .
sed -i "{/process.GlobalTag.globaltag/d}" trackerModuleInfoLocalDB_cfg.py >> ../GeometryValidation.log
sed -i "/FrontierConditions_GlobalTag_cff/ a\process.GlobalTag.globaltag = '${gtag}'" trackerModuleInfoLocalDB_cfg.py >> ../GeometryValidation.log 
sed -i "/FrontierConditions_GlobalTag_cff/ a\process.XMLFromDBSource.label = cms.string('${condlabel}')" trackerModuleInfoLocalDB_cfg.py >> ../GeometryValidation.log 
cmsRun trackerModuleInfoLocalDB_cfg.py >> ../GeometryValidation.log
mv trackerModuleInfoLocalDB_cfg.py ../
if ( -s ModuleInfo.log ) then
    echo "TK test from Local DB run ok" | tee -a ../GeometryValidation.log
else
    echo "ERROR the output of TK test from Local DB is empty" | tee -a ../GeometryValidation.log
    exit
endif

cd ../tkddd
cp $CMSSW_BASE/src/Geometry/TrackerGeometryBuilder/test/trackerModuleInfoDDD_cfg.py .
sed -i "{/process.GlobalTag.globaltag/d}" trackerModuleInfoDDD_cfg.py >> ../GeometryValidation.log
sed -i "/FrontierConditions_GlobalTag_cff/ a\process.GlobalTag.globaltag = '${gtag}'" trackerModuleInfoDDD_cfg.py >> ../GeometryValidation.log 
cmsRun trackerModuleInfoDDD_cfg.py >> ../GeometryValidation.log
mv trackerModuleInfoDDD_cfg.py ../
if ( -s ModuleInfo.log ) then
    echo "TK test from DDD run ok" | tee -a ../GeometryValidation.log
else
    echo "ERROR the output of TK test from DDD is empty" | tee -a ../GeometryValidation.log
    exit
endif

cd ../
rm -f tkdblocal/myfile.db
diff -r tkdb/ tkddd/ > logTkDiffGTvsDDD.log
if ( -s logTkDiffGTvsDDD.log ) then
    echo "WARNING THE TRACKER RECO GEOMETRY IS DIFFERENT BETWEEN DDD AND GT DB" | tee -a GeometryValidation.log
endif

diff -r tkdblocal/ tkddd/ > logTkDiffLocalvsDDD.log
if ( -s logTkDiffLocalvsDDD.log ) then
    echo "WARNING THE TRACKER RECO GEOMETRY IS DIFFERENT BETWEEN DDD AND LOCAL DB" | tee -a GeometryValidation.log
endif

diff -r tkdb/ tkdblocal/ > logTkDiffGTvsLocal.log
if ( -s logTkDiffGTvsLocal.log ) then
    echo "WARNING THE TRACKER RECO GEOMETRY IS DIFFERENT BETWEEN GT DB AND LOCAL DB" | tee -a GeometryValidation.log
endif

echo "End Tracker RECO geometry validation" | tee -a GeometryValidation.log

echo "Start DT RECO geometry validation" | tee -a GeometryValidation.log

cp $CMSSW_BASE/src/Geometry/DTGeometry/test/testDTGeometryFromDB_cfg.py .  
sed -i "{/process.GlobalTag.globaltag/d}" testDTGeometryFromDB_cfg.py >> GeometryValidation.log
sed -i "/FrontierConditions_GlobalTag_cff/ a\process.GlobalTag.globaltag = '${gtag}'" testDTGeometryFromDB_cfg.py >> GeometryValidation.log 
sed -i "/FrontierConditions_GlobalTag_cff/ a\process.XMLFromDBSource.label = cms.string('${condlabel}')" testDTGeometryFromDB_cfg.py >> GeometryValidation.log 
cmsRun testDTGeometryFromDB_cfg.py > outDB_DT.log
if ( -s outDB_DT.log ) then
    echo "DT test from DB run ok" | tee -a GeometryValidation.log
else
    echo "ERROR the output of DT test from DB is empty" | tee -a GeometryValidation.log
    exit
endif

cp $CMSSW_BASE/src/Geometry/DTGeometry/test/testDTGeometryFromLocalDB_cfg.py .  
sed -i "{/process.GlobalTag.globaltag/d}" testDTGeometryFromLocalDB_cfg.py >> GeometryValidation.log
sed -i "/FrontierConditions_GlobalTag_cff/ a\process.GlobalTag.globaltag = '${gtag}'" testDTGeometryFromLocalDB_cfg.py >> GeometryValidation.log 
sed -i "/FrontierConditions_GlobalTag_cff/ a\process.XMLFromDBSource.label = cms.string('${condlabel}')" testDTGeometryFromLocalDB_cfg.py >> GeometryValidation.log 
cmsRun testDTGeometryFromLocalDB_cfg.py > outLocalDB_DT.log
if ( -s outDB_DT.log ) then
    echo "DT test from Local DB run ok" | tee -a GeometryValidation.log
else
    echo "ERROR the output of DT test from Local DB is empty" | tee -a GeometryValidation.log
    exit
endif

cp $CMSSW_BASE/src/Geometry/DTGeometry/test/testDTGeometry_cfg.py .
sed -i "{s/GeometryExtended/${geometry}/}" testDTGeometry_cfg.py >>  GeometryValidation.log
sed -i "{/process.GlobalTag.globaltag/d}" testDTGeometry_cfg.py >> GeometryValidation.log
sed -i "/FrontierConditions_GlobalTag_cff/ a\process.GlobalTag.globaltag = '${gtag}'" testDTGeometry_cfg.py >> GeometryValidation.log 
cmsRun testDTGeometry_cfg.py > outDDD_DT.log
if ( -s outDDD_DT.log ) then
    echo "DT test from DDD run ok" | tee -a GeometryValidation.log
else
    echo "ERROR the output of DT test from DDD is empty" | tee -a GeometryValidation.log
    exit
endif

diff --ignore-matching-lines='Geometry node for DTGeom' outDB_DT.log outDDD_DT.log > logDTDiffGTvsDDD.log
if ( -s logDTDiffGTvsDDD.log ) then
    echo "WARNING THE DT RECO GEOMETRY IS DIFFERENT BETWEEN DDD AND GT DB" | tee -a GeometryValidation.log
endif

diff --ignore-matching-lines='Geometry node for DTGeom' outLocalDB_DT.log outDDD_DT.log > logDTDiffLocalvsDDD.log
if ( -s logDTDiffLocalvsDDD.log ) then
    echo "WARNING THE DT RECO GEOMETRY IS DIFFERENT BETWEEN DDD AND LOCAL DB" | tee -a GeometryValidation.log
endif

diff --ignore-matching-lines='Geometry node for DTGeom' outDB_DT.log outLocalDB_DT.log > logDTDiffGTvsLocal.log
if ( -s logDTDiffGTvsLocal.log ) then
    echo "WARNING THE DT RECO GEOMETRY IS DIFFERENT BETWEEN GT DB AND  LOCAL DB" | tee -a GeometryValidation.log
endif

echo "End DT RECO geometry validation" | tee -a GeometryValidation.log

echo "Start CSC RECO geometry validation" | tee -a GeometryValidation.log

cp $CMSSW_BASE/src/Geometry/CSCGeometry/test/testCSCGeometryFromDB_cfg.py .  
sed -i "{/process.GlobalTag.globaltag/d}" testCSCGeometryFromDB_cfg.py >> GeometryValidation.log
sed -i "/FrontierConditions_GlobalTag_cff/ a\process.GlobalTag.globaltag = '${gtag}'" testCSCGeometryFromDB_cfg.py >> GeometryValidation.log 
sed -i "/FrontierConditions_GlobalTag_cff/ a\process.XMLFromDBSource.label = cms.string('${condlabel}')" testCSCGeometryFromDB_cfg.py >> GeometryValidation.log 
cmsRun testCSCGeometryFromDB_cfg.py > outDB_CSC.log
if ( -s outDB_CSC.log ) then
    echo "CSC test from GT DB run ok" | tee -a GeometryValidation.log
else
    echo "ERROR the output of CSC test from GT DB is empty" | tee -a GeometryValidation.log
    exit
endif

cp $CMSSW_BASE/src/Geometry/CSCGeometry/test/testCSCGeometryFromLocalDB_cfg.py .  
sed -i "{/process.GlobalTag.globaltag/d}" testCSCGeometryFromLocalDB_cfg.py >> GeometryValidation.log
sed -i "/FrontierConditions_GlobalTag_cff/ a\process.GlobalTag.globaltag = '${gtag}'" testCSCGeometryFromLocalDB_cfg.py >> GeometryValidation.log 
sed -i "/FrontierConditions_GlobalTag_cff/ a\process.XMLFromDBSource.label = cms.string('${condlabel}')" testCSCGeometryFromLocalDB_cfg.py >> GeometryValidation.log 
cmsRun testCSCGeometryFromLocalDB_cfg.py > outLocalDB_CSC.log
if ( -s outLocalDB_CSC.log ) then
    echo "CSC test from Local DB run ok" | tee -a GeometryValidation.log
else
    echo "ERROR the output of CSC test from Local DB is empty" | tee -a GeometryValidation.log
    exit
endif

cp $CMSSW_BASE/src/Geometry/CSCGeometry/test/testCSCGeometry_cfg.py .
sed -i "{s/GeometryExtended/${geometry}/}" testCSCGeometry_cfg.py >>  GeometryValidation.log
sed -i "{/process.GlobalTag.globaltag/d}" testCSCGeometry_cfg.py >> GeometryValidation.log
sed -i "/FrontierConditions_GlobalTag_cff/ a\process.GlobalTag.globaltag = '${gtag}'" testCSCGeometry_cfg.py >> GeometryValidation.log 
cmsRun testCSCGeometry_cfg.py > outDDD_CSC.log
if ( -s outDDD_CSC.log ) then
    echo "CSC test from DDD run ok" | tee -a GeometryValidation.log
else
    echo "ERROR the output of CSC test from DDD is empty" | tee -a GeometryValidation.log
    exit
endif

diff --ignore-matching-lines='Geometry node for CSCGeom' outDB_CSC.log outDDD_CSC.log > logCSCDiffGTvsDDD.log
if ( -s logCSCDiffGTvsDDD.log ) then
    echo "WARNING THE CSC RECO GEOMETRY IS DIFFERENT BETWEEN DDD AND GT DB" | tee -a GeometryValidation.log
endif

diff --ignore-matching-lines='Geometry node for CSCGeom' outLocalDB_CSC.log outDDD_CSC.log > logCSCDiffLocalvsDDD.log
if ( -s logCSCDiffLocalvsDDD.log ) then
    echo "WARNING THE CSC RECO GEOMETRY IS DIFFERENT BETWEEN DDD AND LOCAL DB" | tee -a GeometryValidation.log
endif

diff --ignore-matching-lines='Geometry node for CSCGeom' outLocalDB_CSC.log outDB_CSC.log > logCSCDiffLocalvsGT.log
if ( -s logCSCDiffLocalvsGT.log ) then
    echo "WARNING THE CSC RECO GEOMETRY IS DIFFERENT BETWEEN GT DB AND LOCAL DB" | tee -a GeometryValidation.log
endif

echo "End CSC RECO geometry validation" | tee -a GeometryValidation.log

echo "Start RPC RECO geometry validation" | tee -a GeometryValidation.log

cp $CMSSW_BASE/src/Geometry/RPCGeometry/test/testRPCGeometryFromDB_cfg.py .  
sed -i "{/process.GlobalTag.globaltag/d}" testRPCGeometryFromDB_cfg.py >> GeometryValidation.log
sed -i "/FrontierConditions_GlobalTag_cff/ a\process.GlobalTag.globaltag = '${gtag}'" testRPCGeometryFromDB_cfg.py >> GeometryValidation.log 
sed -i "/FrontierConditions_GlobalTag_cff/ a\process.XMLFromDBSource.label = cms.string('${condlabel}')" testRPCGeometryFromDB_cfg.py >> GeometryValidation.log 
cmsRun testRPCGeometryFromDB_cfg.py > outDB_RPC.log
if ( -s outDB_RPC.log ) then
    echo "RPC test from GT DB run ok" | tee -a GeometryValidation.log
else
    echo "ERROR the output of RPC test from GT DB is empty" | tee -a GeometryValidation.log
    exit
endif

cp $CMSSW_BASE/src/Geometry/RPCGeometry/test/testRPCGeometryFromLocalDB_cfg.py .  
sed -i "{/process.GlobalTag.globaltag/d}" testRPCGeometryFromLocalDB_cfg.py >> GeometryValidation.log
sed -i "/FrontierConditions_GlobalTag_cff/ a\process.GlobalTag.globaltag = '${gtag}'" testRPCGeometryFromLocalDB_cfg.py >> GeometryValidation.log 
sed -i "/FrontierConditions_GlobalTag_cff/ a\process.XMLFromDBSource.label = cms.string('${condlabel}')" testRPCGeometryFromLocalDB_cfg.py >> GeometryValidation.log 
cmsRun testRPCGeometryFromLocalDB_cfg.py > outLocalDB_RPC.log
if ( -s outLocalDB_RPC.log ) then
    echo "RPC test from Local DB run ok" | tee -a GeometryValidation.log
else
    echo "ERROR the output of RPC test from Local DB is empty" | tee -a GeometryValidation.log
    exit
endif

cp $CMSSW_BASE/src/Geometry/RPCGeometry/test/testRPCGeometry_cfg.py .
sed -i "{s/GeometryExtended/${geometry}/}" testRPCGeometry_cfg.py >>  GeometryValidation.log
sed -i "{/process.GlobalTag.globaltag/d}" testRPCGeometry_cfg.py >> GeometryValidation.log
sed -i "/FrontierConditions_GlobalTag_cff/ a\process.GlobalTag.globaltag = '${gtag}'" testRPCGeometry_cfg.py >> GeometryValidation.log 
cmsRun testRPCGeometry_cfg.py > outDDD_RPC.log
if ( -s outDDD_RPC.log ) then
    echo "RPC test from DDD run ok" | tee -a GeometryValidation.log
else
    echo "ERROR the output of RPC test from DDD is empty" | tee -a GeometryValidation.log
    exit
endif

diff --ignore-matching-lines='Geometry node for RPCGeom' outDB_RPC.log outDDD_RPC.log > logRPCDiffGTvsDDD.log
if ( -s logRPCDiffGTvsDDD.log ) then
    echo "WARNING THE RPC RECO GEOMETRY IS DIFFERENT BETWEEN DDD AND GT DB" | tee -a GeometryValidation.log
endif

diff --ignore-matching-lines='Geometry node for RPCGeom' outLocalDB_RPC.log outDDD_RPC.log > logRPCDiffLocalvsDDD.log
if ( -s logRPCDiffLocalvsDDD.log ) then
    echo "WARNING THE RPC RECO GEOMETRY IS DIFFERENT BETWEEN DDD AND LOCAL DB" | tee -a GeometryValidation.log
endif

diff --ignore-matching-lines='Geometry node for RPCGeom' outLocalDB_RPC.log outDB_RPC.log > logRPCDiffLocalvsDB.log
if ( -s logRPCDiffLocalvsDB.log ) then
    echo "WARNING THE RPC RECO GEOMETRY IS DIFFERENT BETWEEN GT DB AND LOCAL DB" | tee -a GeometryValidation.log
endif

echo "End RPC RECO geometry validation" | tee -a GeometryValidation.log

echo "Start CALO RECO geometry validation" | tee -a GeometryValidation.log

cp myfile.db $CMSSW_BASE/src/Geometry/CaloEventSetup/test/
cd $CMSSW_BASE/src/Geometry/CaloEventSetup/
cd data
wget -i download.url
cd ../test
source setup.scr >> ${myDir}/GeometryValidation.log
sed -i "{s/GeometryExtended/${geometry}/}" runTestCaloGeometryDDD_cfg.py >> ${myDir}/GeometryValidation.log
sed -i "{/process.GlobalTag.globaltag/d}" runTestCaloGeometryDDD_cfg.py >> ${myDir}/GeometryValidation.log
sed -i "/FrontierConditions_GlobalTag_cff/ a\process.GlobalTag.globaltag = '${gtag}'" runTestCaloGeometryDDD_cfg.py >> ${myDir}/GeometryValidation.log 
cmsRun runTestCaloGeometryDDD_cfg.py > GeometryCaloValidationDDD.log
if ( -s GeometryCaloValidationDDD.log ) then
    echo "CALO test from DDD run ok" | tee -a ${myDir}/GeometryValidation.log
else
    echo "ERROR the output of CALO test from DDD is empty" | tee -a ${myDir}/GeometryValidation.log
    exit
endif

sed -i "{/process.GlobalTag.globaltag/d}" runTestCaloGeometryDB_cfg.py >> ${myDir}/GeometryValidation.log
sed -i "/FrontierConditions_GlobalTag_cff/ a\process.GlobalTag.globaltag = '${gtag}'" runTestCaloGeometryDB_cfg.py >> ${myDir}/GeometryValidation.log 
sed -i "/FrontierConditions_GlobalTag_cff/ a\process.XMLFromDBSource.label = cms.string('${condlabel}')" runTestCaloGeometryDB_cfg.py >> ${myDir}/GeometryValidation.log 
cmsRun runTestCaloGeometryDB_cfg.py > GeometryCaloValidationDB.log
if ( -s GeometryCaloValidationDB.log ) then
    echo "CALO test from GT DB run ok" | tee -a ${myDir}/GeometryValidation.log
else
    echo "ERROR the output of CALO test from GT DB is empty" | tee -a ${myDir}/GeometryValidation.log
    exit
endif

sed -i "{/process.GlobalTag.globaltag/d}" runTestCaloGeometryLocalDB_cfg.py >> ${myDir}/GeometryValidation.log
sed -i "/FrontierConditions_GlobalTag_cff/ a\process.GlobalTag.globaltag = '${gtag}'" runTestCaloGeometryLocalDB_cfg.py >> ${myDir}/GeometryValidation.log 
sed -i "/FrontierConditions_GlobalTag_cff/ a\process.XMLFromDBSource.label = cms.string('${condlabel}')" runTestCaloGeometryLocalDB_cfg.py >> ${myDir}/GeometryValidation.log 
cmsRun runTestCaloGeometryLocalDB_cfg.py > GeometryCaloValidationLocal.log
if ( -s GeometryCaloValidationLocal.log ) then
    echo "CALO Local test from Local DB run ok" | tee -a ${myDir}/GeometryValidation.log
else
    echo "ERROR the output of CALO test from Local DB is empty" | tee -a ${myDir}/GeometryValidation.log
    exit
endif
cd ${myDir}

less $CMSSW_BASE/src/Geometry/CaloEventSetup/test/GeometryCaloValidationDDD.log | tee -a GeometryValidation.log
less $CMSSW_BASE/src/Geometry/CaloEventSetup/test/GeometryCaloValidationDB.log | tee -a GeometryValidation.log
less $CMSSW_BASE/src/Geometry/CaloEventSetup/test/GeometryCaloValidationLocal.log | tee -a GeometryValidation.log

grep 'BIG DISAGREEMENT FOUND' $CMSSW_BASE/src/Geometry/CaloEventSetup/test/GeometryCaloValidationDDD.log > CALODDDError.log 
grep 'BIG DISAGREEMENT FOUND' $CMSSW_BASE/src/Geometry/CaloEventSetup/test/GeometryCaloValidationDB.log > CALODBError.log 
grep 'BIG DISAGREEMENT FOUND' $CMSSW_BASE/src/Geometry/CaloEventSetup/test/GeometryCaloValidationLocal.log > CALOLocalError.log 

rm -f $CMSSW_BASE/src/Geometry/CaloEventSetup/test/GeometryCaloValidationDDD.log
rm -f $CMSSW_BASE/src/Geometry/CaloEventSetup/test/GeometryCaloValidationDB.log
rm -f $CMSSW_BASE/src/Geometry/CaloEventSetup/test/GeometryCaloValidationLocal.log
source $CMSSW_BASE/src/Geometry/CaloEventSetup/test/clean.scr

if ( -s CALODDDError.log ) then                                                               
    echo "WARNING THE CALO GEOMETRY IS DIFFERENT BETWEEN DDD AND REF" | tee -a GeometryValidation.log                                                                                  
endif                                                                                                      

if ( -s CALODBError.log ) then                                                               
    echo "WARNING THE CALO GEOMETRY IS DIFFERENT BETWEEN GT DB AND REF" | tee -a GeometryValidation.log                                                                                  
endif                                                                                                      

if ( -s CALOLocalError.log ) then                                                               
    echo "WARNING THE CALO GEOMETRY IS DIFFERENT BETWEEN LOCAL DB AND REF" | tee -a GeometryValidation.log                                                                                  
endif                                                                                                      
                                                                                              
echo "End CALO RECO geometry validation" | tee -a GeometryValidation.log

echo "Start Simulation geometry validation" | tee -a GeometryValidation.log

# (MEC:2) see (MEC:1) Since the global tag versus the local database
# blobs have been verified, it is possible to argue that 
# there is really no reason to check those two blobs using this method.
# However, in this test, the actual DDD is built and dumped for each 
# of standard (STD, i.e. the list of smaller xml files), the "BIG" XML
# File (BDB, i.e. the one prepped to become a blob), the local database
# file blob (LocDB, after Big is loaded into the local database), and
# the file blob that comes from the global tag that was provided to the 
# script (GTDB, could be same or older version).

if ( $geometry == "GeometryIdeal" ) then
    echo "Here I am " > readXML.expected
    echo "Top Most LogicalPart =cms:OCMS " >> readXML.expected
    echo " mat=materials:Air" >> readXML.expected
    echo " solid=cms:OCMS   Polycone_rrz: 0 6.28319 -27100 0 1000 -27000 0 1000 -27000 0 13000 27000 0 13000 27000 0 1000 27100 0 1000 " >> readXML.expected
    echo "After the GeoHistory in the output file dumpGeoHistoryOnRead you will see x, y, z, r11, r12, r13, r21, r22, r23, r31, r32, r33" >> readXML.expected
    echo "finished" >> readXML.expected
else
    echo "Here I am " > readXML.expected
    echo "Top Most LogicalPart =cms:OCMS " >> readXML.expected
    echo " mat=materials:Air" >> readXML.expected
    echo " solid=cms:OCMS   Polycone_rrz: 0 6.28319 -450000 0 1000 -27000 0 1000 -27000 0 13000 27000 0 13000 27000 0 1000 450000 0 1000 " >> readXML.expected
    echo "After the GeoHistory in the output file dumpGeoHistoryOnRead you will see x, y, z, r11, r12, r13, r21, r22, r23, r31, r32, r33" >> readXML.expected
    echo "finished" >> readXML.expected
endif

cp $CMSSW_BASE/src/GeometryReaders/XMLIdealGeometryESSource/test/readIdealAndDump.py .
sed -i "{s/GeometryExtended/${geometry}/}" readIdealAndDump.py >>  GeometryValidation.log
cmsRun readIdealAndDump.py > readXMLAndDump.log

cp $CMSSW_BASE/src/GeometryReaders/XMLIdealGeometryESSource/test/testReadXMLFromGTDB.py .
sed -i "{/process.GlobalTag.globaltag/d}" testReadXMLFromGTDB.py >> GeometryValidation.log
sed -i "{/process.XMLFromDBSource.label/d}" testReadXMLFromGTDB.py >> GeometryValidation.log
sed -i "/FrontierConditions_GlobalTag_cff/ a\process.GlobalTag.globaltag = '${gtag}'" testReadXMLFromGTDB.py >> GeometryValidation.log
sed -i "/FrontierConditions_GlobalTag_cff/ a\process.XMLFromDBSource.label = cms.string('${condlabel}')" testReadXMLFromGTDB.py >> GeometryValidation.log
cmsRun testReadXMLFromGTDB.py > readXMLfromGTDB.log

cp $CMSSW_BASE/src/GeometryReaders/XMLIdealGeometryESSource/test/testReadXMLFromDB.py .
sed -i "{/process.GlobalTag.globaltag/d}" testReadXMLFromDB.py >> GeometryValidation.log
sed -i "{/process.XMLFromDBSource.label/d}" testReadXMLFromDB.py >> GeometryValidation.log
sed -i "/FrontierConditions_GlobalTag_cff/ a\process.GlobalTag.globaltag = '${gtag}'" testReadXMLFromDB.py >> GeometryValidation.log
sed -i "/FrontierConditions_GlobalTag_cff/ a\process.XMLFromDBSource.label = cms.string('${condlabel}')" testReadXMLFromDB.py >> GeometryValidation.log
cmsRun testReadXMLFromDB.py > readXMLfromLocDB.log

cp $CMSSW_BASE/src/GeometryReaders/XMLIdealGeometryESSource/test/readBigXMLAndDump.py .
sed -i "{/,geomXMLFiles = cms.vstring('GeometryReaders\/XMLIdealGeometryESSource\/test\/fred.xml')/d}" readBigXMLAndDump.py >> GeometryValidation.log
sed -i "/XMLIdealGeometryESSource/ a\\t\t,geomXMLFiles=cms.vstring('workArea\/fred.xml')" readBigXMLAndDump.py >>  GeometryValidation.log
cmsRun readBigXMLAndDump.py > readBigXMLAndDump.log

diff readXMLAndDump.log readXML.expected > diffreadXMLSTD.log
diff readXMLfromGTDB.log readXML.expected > diffreadXMLGTDB.log
diff readXMLfromLocDB.log readXML.expected > diffreadXMLLocDB.log
diff readBigXMLAndDump.log readXML.expected > diffreadXMLBDB.log

if ( -s diffreadXMLSTD.log ) then
    echo "ERROR THE MULTI-XML FILE GEOMETRY WAS NOT DUMPED PROPERLY." | tee -a GeometryValidation.log
    exit
else
    echo "GeometryFile dump from multiple XML files done."
endif

if ( -s diffreadXMLGTDB.log ) then
    echo "ERROR THE GLOBAL TAG DATABASE GEOMETRY WAS NOT DUMPED PROPERLY." | tee -a GeometryValidation.log
    exit
else
    echo "GeometryFile dump from global tag database done."
endif

if ( -s diffreadXMLLocDB.log ) then
    echo "ERROR THE LOCAL DATABASE GEOMETRY WAS NOT DUMPED PROPERLY." | tee -a GeometryValidation.log
    exit
else
    echo "GeometryFile dump from local database done."
endif

if ( -s diffreadXMLBDB.log ) then
    echo "ERROR THE BIG SINGLE XML FILE WAS NOT DUMPED PROPERLY." | tee -a GeometryValidation.log
    exit
else
    echo "GeometryFile dump from big single XML file done."
endif

#    ,dumpFile1 = cms.string("workarea/xml/dumpSTD")
#    ,dumpFile2 = cms.string("workarea/db/dumpBDB")
#dumpBDB                            dumpGTDB
#dumpLocDB                          dumpSTD
#>>> processing event # run: 1 lumi: 1 event: 1 time 1
#>>> processed 1 events

echo ">>> processing event # run: 1 lumi: 1 event: 1 time 1" >compDDdumperrors.expected
echo ">>> processed 1 events" >>compDDdumperrors.expected

cp $CMSSW_BASE/src/GeometryReaders/XMLIdealGeometryESSource/test/testCompareDumpFiles.py .
sed -i "{/,dumpFile1 /d}" testCompareDumpFiles.py
sed -i "{/,dumpFile2 /d}" testCompareDumpFiles.py
sed -i "/TestCompareDDDumpFiles/ a\,dumpFile1=cms.string\('./dumpSTD'\)\,dumpFile2=cms.string\('./dumpBDB'\)" testCompareDumpFiles.py
cmsRun testCompareDumpFiles.py > tcdfSTDvsBDB.log


if (-s tcdfSTDvsBDB.log || -s diffcompSTDvsBDB.log ) then
    echo "WARNING THE GEOMETRYFILE IS DIFFERENT BETWEEN STD XML AND BIG SINGLE XML." | tee -a GeometryValidation.log
endif

cp $CMSSW_BASE/src/GeometryReaders/XMLIdealGeometryESSource/test/testCompareDumpFiles.py .
sed -i "{/,dumpFile1 /d}" testCompareDumpFiles.py
sed -i "{/,dumpFile2 /d}" testCompareDumpFiles.py
sed -i "/TestCompareDDDumpFiles/ a\,dumpFile1=cms.string\('./dumpSTD'\)\,dumpFile2=cms.string\('./dumpLocDB'\)" testCompareDumpFiles.py
cmsRun testCompareDumpFiles.py > tcdfSTDvsLocDB.log

diff compDDdumperrors.log compDDdumperrors.expected > diffcompSTDvsLocDB.log
if (-s tcdfSTDvsLocDB.log || -s diffcompSTDvsLocDB.log ) then
    echo "WARNING THE GEOMETRYFILE IS DIFFERENT BETWEEN STD XML AND LOCAL DATABASE BLOB." | tee -a GeometryValidation.log
endif

cp $CMSSW_BASE/src/GeometryReaders/XMLIdealGeometryESSource/test/testCompareDumpFiles.py .
sed -i "{/,dumpFile1 /d}" testCompareDumpFiles.py
sed -i "{/,dumpFile2 /d}" testCompareDumpFiles.py
sed -i "/TestCompareDDDumpFiles/ a\,dumpFile1=cms.string\('./dumpSTD'\)\,dumpFile2=cms.string\('./dumpGTDB'\)" testCompareDumpFiles.py
cmsRun testCompareDumpFiles.py > tcdfSTDvsGTDB.log

diff compDDdumperrors.log compDDdumperrors.expected > diffcompSTDvsGTDB.log
if (-s tcdfSTDvsGTDB.log || -s diffcompSTDvsGTDB.log ) then
    echo "WARNING THE GEOMETRYFILE IS DIFFERENT BETWEEN STD AND GLOBALTAG DATABASE BLOB." | tee -a GeometryValidation.log
endif

cp $CMSSW_BASE/src/GeometryReaders/XMLIdealGeometryESSource/test/testCompareDumpFiles.py .
sed -i "{/,dumpFile1 /d}" testCompareDumpFiles.py
sed -i "{/,dumpFile2 /d}" testCompareDumpFiles.py
sed -i "/TestCompareDDDumpFiles/ a\,dumpFile1=cms.string\('./dumpBDB'\)\,dumpFile2=cms.string\('./dumpLocDB'\)" testCompareDumpFiles.py
cmsRun testCompareDumpFiles.py > tcdfBDBvsLocDB.log

diff compDDdumperrors.log compDDdumperrors.expected > diffcompBDBvsLocDB.log
if (-s tcdfBDBvsLocDB.log || -s diffcompBDBvsLocDB.log ) then
    echo "WARNING THE GEOMETRYFILE IS DIFFERENT BETWEEN SINGLE BIG XML FILE AND LOCAL DATABASE BLOB." | tee -a GeometryValidation.log
endif

cp $CMSSW_BASE/src/GeometryReaders/XMLIdealGeometryESSource/test/testCompareDumpFiles.py .
sed -i "{/,dumpFile1 /d}" testCompareDumpFiles.py
sed -i "{/,dumpFile2 /d}" testCompareDumpFiles.py
sed -i "/TestCompareDDDumpFiles/ a\,dumpFile1=cms.string\('./dumpBDB'\)\,dumpFile2=cms.string\('./dumpGTDB'\)" testCompareDumpFiles.py
cmsRun testCompareDumpFiles.py > tcdfBDBvsGTDB.log

diff compDDdumperrors.log compDDdumperrors.expected > diffcompBDBvsGTDB.log
if (-s tcdfBDBvsGTDB.log || -s diffcompBDBvsGTDB.log ) then
    echo "WARNING THE GEOMETRYFILE IS DIFFERENT BETWEEN SINGLE BIG XML FILE AND GLOBALTAG DATABASE BLOB."  | tee -a GeometryValidation.log
endif

cp $CMSSW_BASE/src/GeometryReaders/XMLIdealGeometryESSource/test/testCompareDumpFiles.py .
sed -i "{/,dumpFile1 /d}" testCompareDumpFiles.py
sed -i "{/,dumpFile2 /d}" testCompareDumpFiles.py
sed -i "/TestCompareDDDumpFiles/ a\,dumpFile1=cms.string\('./dumpLocDB'\)\,dumpFile2=cms.string\('./dumpGTDB'\)" testCompareDumpFiles.py
cmsRun testCompareDumpFiles.py > tcdfLocDBvsGTDB.log

diff compDDdumperrors.log compDDdumperrors.expected > diffcompLocDBvsGTDB.log
if (-s tcdfBDBvsLocDB.log || -s diffcompLocDBvsGTDB.log ) then
    echo "WARNING THE GEOMETRYFILE IS DIFFERENT BETWEEN LOCAL AND GLOBALTAG DATABASE BLOBS."  | tee -a GeometryValidation.log
endif

echo "End Simulation geometry validation" | tee -a GeometryValidation.log
