Index: /trunk/Mars/fact/analysis/gain/extract_temp.C
===================================================================
--- /trunk/Mars/fact/analysis/gain/extract_temp.C	(revision 17326)
+++ /trunk/Mars/fact/analysis/gain/extract_temp.C	(revision 17326)
@@ -0,0 +1,156 @@
+#include <iostream>
+
+#include <TString.h>
+#include <TFile.h>
+#include <TGraph.h>
+
+#include "zfits.h"
+#include "PixelMap.h"
+#include "Interpolator2D.h"
+
+#include "MHCamera.h"
+#include "MGeomCamFACT.h"
+
+int extract_temp(UInt_t day=20130902, UInt_t run=146, const char *output=0)
+{
+    TString nraw = Form("/fact/raw/%4d/%02d/%02d/%8d_%03d.fits.fz",
+                        day/10000, (day/100)%100, day%100, day, run);
+
+    TString naux = Form("/fact/aux/%4d/%02d/%02d/%8d.FSC_CONTROL_TEMPERATURE.fits",
+                        day/10000, (day/100)%100, day%100, day);
+
+    PixelMap pmap;
+    if (!pmap.Read("FACTmap111030.txt"))
+    {
+        cout << "FACTmap111030.txt not found." << endl;
+        return 1;
+    }
+
+    Interpolator2D interpol;
+
+    const auto sens = Interpolator2D::ReadGrid("operation/sensor-pos.txt");
+    if (sens.size()!=31)
+    {
+        cout << "Reading sensor-pos.txt failed: " << interpol.getInputGrid().size() << endl;
+        return 2;
+    }
+
+    // =====================================================================
+
+    zfits fraw(nraw.Data());
+    if (!fraw)
+    {
+        cout << "Open raw file failed: " << nraw << endl;
+        return 1;
+    }
+
+    double tstart = fraw.GetUInt("TSTARTI") + fraw.GetFloat("TSTARTF");
+    double tstop  = fraw.GetUInt("TSTOPI")  + fraw.GetFloat("TSTOPF");
+
+    // =====================================================================
+
+    // Now we read the temperatures of the sensors during the
+    // single pe run and average them
+    fits faux(naux.Data());
+    if (!faux)
+    {
+        cout << "Could not open " << naux << endl;
+        return 1;
+    }
+
+    Double_t time;
+    Float_t  temp[31];
+
+    if (!faux.SetPtrAddress("Time",  &time))
+        return -1;
+    if (!faux.SetPtrAddress("T_sens", temp))
+        return -1;
+
+    TArrayD avg(31);
+    TArrayD rms(31);
+    TArrayI cnt(31);
+
+    while (faux.GetNextRow())
+    {
+        if (time<tstart || time>tstop)
+            continue;
+
+        for (int i=0; i<31; i++)
+        {
+            if (temp[i]==0)
+                continue;
+
+            double T = temp[i];
+
+            avg[i] += T;
+            rms[i] += T*T;
+            cnt[i]++;
+        }
+    }
+
+    vector<double> z;
+
+    vector<Interpolator2D::vec> pos;
+    for (int i=0; i<31; i++)
+    {
+        if (cnt[i]>0)
+        {
+            avg[i] /= cnt[i];
+            rms[i] /= cnt[i];
+            rms[i] = sqrt(rms[i]-avg[i]*avg[i]);
+
+            pos.push_back(sens[i]);
+            z.push_back(avg[i]);
+        }
+    }
+
+    // ================================================================
+
+    interpol.SetInputGrid(pos);
+
+    if (!interpol.ReadOutputGrid("operation/bias-positions.txt"))
+    {
+        cout << "Error setting output grid." << endl;
+        return 3;
+    }
+    if (interpol.getOutputGrid().size()!=320)
+    {
+        cout << "Reading bias-positions.txt failed: " << interpol.getOutputGrid().size() << endl;
+        return 3;
+    }
+
+    // ================================================================
+
+    const auto &input = interpol.getInputGrid();
+
+    TGraph sensors(input.size());
+
+    for (unsigned int i=0; i<input.size(); i++)
+        sensors.SetPoint(i, input[i].x, input[i].y);
+
+    const vector<double> rc = interpol.Interpolate(z);
+
+    // ================================================================
+
+    TCanvas *c = new TCanvas;
+    c->SetName("Temp");
+
+    MGeomCamFACT geom;
+
+    MHCamera h1(geom);
+    h1.SetName("Temp");
+    for (int i=0; i<1440; i++)
+        h1.SetBinContent(i+1, rc[pmap.index(i).hv()]);
+
+    h1.SetAllUsed();
+    h1.DrawCopy();
+
+    sensors.SetName("Sensors");
+    sensors.SetMarkerColor(kWhite);
+    sensors.DrawClone("*");
+
+    if (output)
+        c->SaveAs(output);
+
+    return 0;
+}
Index: /trunk/Mars/fact/analysis/gain/temp.sh
===================================================================
--- /trunk/Mars/fact/analysis/gain/temp.sh	(revision 17326)
+++ /trunk/Mars/fact/analysis/gain/temp.sh	(revision 17326)
@@ -0,0 +1,45 @@
+#!/bin/bash
+
+macro=$PWD/extract_temp.C
+outpath=$PWD/temp
+
+mkdir -p $outpath
+cp ${macro} ${outpath}
+
+where="(((fNumPedestalTrigger=3000 OR fNumPedestalTrigger=5000) AND fRunTypeKey=2) OR (fNumPedestalTrigger=10000 AND fRunTypeKey=3) OR fRunTypeKey=17)"
+wheredrs="(drs.fROI=single.fROI AND fDrsStep=2)"
+
+query="SELECT Concat(fNight, '_', fRunID, '_', (SELECT fRunID FROM RunInfo \`drs\` WHERE drs.fNight=single.fNight AND "$wheredrs" AND single.fRunID<30+drs.fRunID ORDER BY ABS(CAST(single.fRunID AS SIGNED) - CAST(drs.fRunID AS SIGNED)) ASC LIMIT 0,1)) AS num "
+query=$query" FROM RunInfo \`single\` WHERE "$where
+
+runpairs=( `mysql -u factread --password=r3adfac! --host=lp-fact factdata -s -e "$query"` )
+
+for runpair in ${runpairs[@]}
+do
+   if [ $runpair = 'NULL' ]
+   then
+      continue
+   fi
+
+   night=`echo $runpair | cut -d_ -f1`
+   runnum=`printf %03d $run`
+
+   log=$outpath"/"$night"_"$runnum
+
+   outfile=$outpath/${runnum}_${runnum}.root
+
+   if [ -e $outfile ]
+   then
+      continue
+   fi
+
+   cd ~/Mars
+
+   cmd="ROOT_HIST=0 ${ROOTSYS}/bin/root -q -b -l ${macro}+\($night,$runnum,\\\"${outfile}\\\"\)"
+
+   echo "date; echo '${cmd}'; ${cmd}" | qsub -N t${night}${runnum} -q test -cwd -e ${log}".err" -o ${log}".log"
+
+   cd - > /dev/null
+done
+
+
