Index: trunk/DataCheck/Tools/get_data.sh
===================================================================
--- trunk/DataCheck/Tools/get_data.sh	(revision 19035)
+++ trunk/DataCheck/Tools/get_data.sh	(revision 19036)
@@ -78,32 +78,63 @@
 function get_results()
 {
-   # some basic query parts
-
-   # data check based on artificial trigger rate
-   #dch=" AND fR750Cor/fR750Ref >0.93 "
-   dch=" AND fR750Cor/fR750Ref BETWEEN 0.93 AND 1.3 "
+   # some query parts
+
+   # some numbers for flux calculation
+   crabflux="3.37e-11"
+   fluxprec=13
+   crabflux="3.37"
+   fluxprec=2
+   
+   # some names and definitions needed several times below
    # ontime 
    ontime1=" TIME_TO_SEC(TIMEDIFF(fRunStop,fRunStart))*fEffectiveOn "
    ontime2=" fOnTimeAfterCuts "
    ontimeif=" IF(ISNULL(fEffectiveOn), "$ontime2", "$ontime1") "
-   from=" FROM RunInfo LEFT JOIN "$table" USING (fNight, fRunID) "
-   # time range and source
-   where=" WHERE fSourceKey="$source" AND fNight BETWEEN "$nightmin" AND "$nightmax
-   where=$where" AND NOT ISNULL(fNumExcEvts) "
-   # some sanity checks
-   where=$where" AND fRunTypeKey=1 "
-   # zd cut
-   where=$where" AND fZenithDistanceMax < "$zdmax
-   # th cut
-   where=$where" AND fThresholdMedian < "$thmax
-   where=$where" "$dch
-   
-   # 
+   # zd and threshold
+   zenith="fZenithDistance"
+   thresh="IF(ISNULL(fThresholdMinSet),fThresholdMedian,fThresholdMinSet)"
+   # correction factor for excess rate (formula by TB)
+   correvts=" fNumExcEvts*(pow(cos("$zenith"Mean*PI()/180),3)+14.8/21.9*pow(sin(2*"$zenith"Mean*PI()/180),5))/((1-0.00124/1.21*("$thresh"-500)*("$thresh">=500))) "
+   # conversion to CU (determined by DD for QLA)
+   # https://www.fact-project.org/logbook/showthread.php?tid=4927
    cufactor=" Avg(CUQLA(fNight)) "
-   crabflux="3.37e-11"
-   fluxprec=13
-   crabflux="3.37"
-   fluxprec=2
-   
+   # some calculations
+   excerr="ExcErr(Sum(fNumSigEvts), SUM(fNumBgEvts))"
+   CU="SUM("$correvts"/CUQLA(fNight))/SUM("$ontimeif")*3600"
+   CUerr=$excerr"/SUM("$ontimeif")*3600*SUM("$correvts"/CUQLA(fNight))/SUM(fNumExcEvts)"
+   excerr2="ExcErr(SUM(o.sigevts),SUM(o.bgevts))"
+   CU2="SUM(o.corevts/o.cu)/SUM(o.ot)*3600"
+   CUerr2=$excerr2"/SUM(o.ot)*3600*SUM(o.corevts/o.cu)/(SUM(o.sigevts)-SUM(o.bgevts))"
+   
+   # columns to be selected
+   # for night-binning
+   ontime=" ROUND(SUM("$ontimeif")/60., 1) AS ontime"
+   excrate=" ROUND(SUM(fNumExcEvts)/SUM("$ontimeif")*3600, 1) AS excrate"
+   significance="ROUND(LiMa(Sum(fNumSigEvts), SUM(fNumBgEvts)), 1) AS significance"
+   numexc="Sum(fNumExcEvts) AS numexc"
+   numsig="Sum(fNumSigEvts) AS numsig"
+   numbg="Sum(fNumBgEvts) AS numbg"
+   excrateerr=" ROUND("$excerr"/SUM("$ontimeif")*3600, 1) AS excrateerr"
+   correxcrate=" ROUND(SUM("$correvts")/SUM("$ontimeif")*3600, 1) AS correxcrate"
+   correxcrateerr=" ROUND("$excerr"/SUM("$ontimeif")*3600*SUM("$correvts")/SUM(fNumExcEvts), 1) AS correxcrateerr"
+   cu=" ROUND("$CU", 2) AS cu"
+   cuerr=" ROUND("$CUerr", 2) AS cuerr"
+   flux="ROUND("$CU" * "$crabflux", 2) AS flux"
+   fluxerr="ROUND("$CUerr" * "$crabflux", 2) AS fluxerr"
+   # for minute binning
+   ontime2=" ROUND(SUM(o.ot)/60., 1) AS ontime"
+   excrate2=" ROUND((SUM(o.sigevts)-SUM(o.bgevts))/SUM(o.ot)*3600, 1) AS excrate"
+   significance2=" ROUND(LiMa(SUM(o.sigevts),SUM(o.bgevts)), 1) AS significance"
+   numexc2="Sum(o.sigevts-o.bgevts) AS numexc"
+   numsig2="Sum(o.sigevts) AS numsig"
+   numbg2="Sum(o.bgevts) AS numbg"
+   excrateerr2=" ROUND("$excerr2"/SUM(o.ot)*3600, 1) AS excrateerr"
+   correxcrate2=" ROUND(SUM(o.corevts)/SUM(o.ot)*3600, 1) AS correxcrate"
+   correxcrateerr2=" ROUND("$excerr2"/SUM(o.ot)*3600*SUM(o.corevts)/(SUM(o.sigevts)-SUM(o.bgevts)), 1) AS correxcrateerr"
+   cu2=" ROUND("$CU2", 2) AS cu"
+   cuerr2=" ROUND("$CUerr2", 2) AS cuerr"
+   flux2="ROUND("$CU2" * "$crabflux", "$fluxprec") AS flux"
+   fluxerr2="ROUND("$CUerr2" *"$crabflux", "$fluxprec") AS fluxerr"
+
    case $timeunit in
       mjd)  delta="(Mjd(MAX(fRunStop))-Mjd(Min(fRunStart)))/2"
@@ -141,45 +172,24 @@
             ;;
    esac
-   zenith="fZenithDistance"
-   thresh="IF(ISNULL(fThresholdMinSet),fThresholdMedian,fThresholdMinSet)"
-   # thomas correction factor
-   correvts=" fNumExcEvts*(pow(cos("$zenith"Mean*PI()/180),3)+14.8/21.9*pow(sin(2*"$zenith"Mean*PI()/180),5))/((1-0.00124/1.21*("$thresh"-500)*("$thresh">=500))) "
-   excerr="ExcErr(Sum(fNumSigEvts), SUM(fNumBgEvts))"
-   CU="SUM("$correvts"/CUQLA(fNight))/SUM("$ontimeif")*3600"
-   CUerr=$excerr"/SUM("$ontimeif")*3600*SUM("$correvts"/CUQLA(fNight))/SUM(fNumExcEvts)"
-   excerr2="ExcErr(SUM(o.sigevts),SUM(o.bgevts))"
-   CU2="SUM(o.corevts/o.cu)/SUM(o.ot)*3600"
-   CUerr2=$excerr2"/SUM(o.ot)*3600*SUM(o.corevts/o.cu)/(SUM(o.sigevts)-SUM(o.bgevts))"
-   
-   ontime=" ROUND(SUM("$ontimeif")/60., 1) AS ontime"
-   excrate=" ROUND(SUM(fNumExcEvts)/SUM("$ontimeif")*3600, 1) AS excrate"
-   significance="ROUND(LiMa(Sum(fNumSigEvts), SUM(fNumBgEvts)), 1) AS significance"
-   numexc="Sum(fNumExcEvts) AS numexc"
-   numsig="Sum(fNumSigEvts) AS numsig"
-   numbg="Sum(fNumBgEvts) AS numbg"
-   excrateerr=" ROUND("$excerr"/SUM("$ontimeif")*3600, 1) AS excrateerr"
-   correxcrate=" ROUND(SUM("$correvts")/SUM("$ontimeif")*3600, 1) AS correxcrate"
-   # corerr = MMath::ErrorExc(excevtssum+bgevtssum, bgevtssum*5, 0.2)/ontimesum*3600.*corrate/excrate;
-   correxcrateerr=" ROUND("$excerr"/SUM("$ontimeif")*3600*SUM("$correvts")/SUM(fNumExcEvts), 1) AS correxcrateerr"
-   # correction on run basis
-   cu=" ROUND("$CU", 2) AS cu"
-   cuerr=" ROUND("$CUerr", 2) AS cuerr"
-   flux="ROUND("$CU" * "$crabflux", 2) AS flux"
-   fluxerr="ROUND("$CUerr" * "$crabflux", 2) AS fluxerr"
-   
-   ontime2=" ROUND(SUM(o.ot)/60., 1) AS ontime"
-   excrate2=" ROUND((SUM(o.sigevts)-SUM(o.bgevts))/SUM(o.ot)*3600, 1) AS excrate"
-   significance2=" ROUND(LiMa(SUM(o.sigevts),SUM(o.bgevts)), 1) AS significance"
-   numexc2="Sum(o.sigevts-o.bgevts) AS numexc"
-   numsig2="Sum(o.sigevts) AS numsig"
-   numbg2="Sum(o.bgevts) AS numbg"
-   excrateerr2=" ROUND("$excerr2"/SUM(o.ot)*3600, 1) AS excrateerr"
-   correxcrate2=" ROUND(SUM(o.corevts)/SUM(o.ot)*3600, 1) AS correxcrate"
-   correxcrateerr2=" ROUND("$excerr2"/SUM(o.ot)*3600*SUM(o.corevts)/(SUM(o.sigevts)-SUM(o.bgevts)), 1) AS correxcrateerr"
-   cu2=" ROUND("$CU2", 2) AS cu"
-   cuerr2=" ROUND("$CUerr2", 2) AS cuerr"
-   flux2="ROUND("$CU2" * "$crabflux", "$fluxprec") AS flux"
-   fluxerr2="ROUND("$CUerr2" *"$crabflux", "$fluxprec") AS fluxerr"
-
+   
+   # from and join of query 
+   from=" FROM RunInfo LEFT JOIN "$table" USING (fNight, fRunID) "
+   
+   # data check based on artificial trigger rate
+   # details see https://www.fact-project.org/logbook/showthread.php?tid=5790
+   #dch=" AND fR750Cor/fR750Ref >0.93 "
+   dch=" AND fR750Cor/fR750Ref BETWEEN 0.93 AND 1.3 "
+   
+   # put together where-clause of query
+   # time range and source
+   where=" WHERE fSourceKey="$source" AND fNight BETWEEN "$nightmin" AND "$nightmax
+   where=$where" AND NOT ISNULL(fNumExcEvts) "
+   # some sanity checks
+   where=$where" AND fRunTypeKey=1 "
+   # zd cut
+   where=$where" AND fZenithDistanceMax < "$zdmax
+   # th cut
+   where=$where" AND fThresholdMedian < "$thmax
+   where=$where" "$dch
    
    if [ $bin -le 0 ]
@@ -407,5 +417,15 @@
 nightmin=20140714
 nightmax=20140805
-#get_results
+get_results
+
+bin=30
+name="Mrk501_2014_forMAGIC30"
+get_results
+
+bin=0
+name="P"
+nightmin=20140501
+nightmax=20140930
+get_results
 
 bin=20
@@ -414,9 +434,4 @@
 name="Mrk501_test"
 get_results
-
-#bin=30
-#name="Mrk501_2014_forMAGIC30"
-#get_results
-
 
 
