| 1 | /* ======================================================================== *\
|
|---|
| 2 | !
|
|---|
| 3 | ! *
|
|---|
| 4 | ! * This file is part of MARS, the MAGIC Analysis and Reconstruction
|
|---|
| 5 | ! * Software. It is distributed to you in the hope that it can be a useful
|
|---|
| 6 | ! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
|
|---|
| 7 | ! * It is distributed WITHOUT ANY WARRANTY.
|
|---|
| 8 | ! *
|
|---|
| 9 | ! * Permission to use, copy, modify and distribute this software and its
|
|---|
| 10 | ! * documentation for any purpose is hereby granted without fee,
|
|---|
| 11 | ! * provided that the above copyright notice appear in all copies and
|
|---|
| 12 | ! * that both that copyright notice and this permission notice appear
|
|---|
| 13 | ! * in supporting documentation. It is provided "as is" without express
|
|---|
| 14 | ! * or implied warranty.
|
|---|
| 15 | ! *
|
|---|
| 16 | !
|
|---|
| 17 | !
|
|---|
| 18 | ! Author(s): Thomas Bretz, 08/2004 <mailto:tbretz@astro.uni-wuerzburg.de>
|
|---|
| 19 | ! Author(s): Daniela Dorner, 08/2004 <mailto:dorner@astro.uni-wuerzburg.de>
|
|---|
| 20 | !
|
|---|
| 21 | ! Copyright: MAGIC Software Development, 2000-2004
|
|---|
| 22 | !
|
|---|
| 23 | !
|
|---|
| 24 | \* ======================================================================== */
|
|---|
| 25 |
|
|---|
| 26 | /////////////////////////////////////////////////////////////////////////////
|
|---|
| 27 | //
|
|---|
| 28 | // fillcalib.C
|
|---|
| 29 | // ===========
|
|---|
| 30 | //
|
|---|
| 31 | // This macro is used to read the calibartion-/callisto-output files.
|
|---|
| 32 | // These files are automatically called calib00000.root.
|
|---|
| 33 | //
|
|---|
| 34 | // From this file the MBadPixelsCam and the MGeomCam is extracted. If
|
|---|
| 35 | // the geometry isn't found MGeomCamMagic is used as a default.
|
|---|
| 36 | //
|
|---|
| 37 | // Usage:
|
|---|
| 38 | // .x fillcalib.C("/data/MAGIC/Period014/calib00000.root", kTRUE)
|
|---|
| 39 | //
|
|---|
| 40 | // The second argument is the 'dummy-mode'. If it is kTRUE dummy-mode is
|
|---|
| 41 | // switched on and nothing will be written into the database. This is usefull
|
|---|
| 42 | // for tests.
|
|---|
| 43 | //
|
|---|
| 44 | // The corresponding sequence number is extracted from the filename...
|
|---|
| 45 | // FIXME: MSeqeuence should be stored in the calib-file?
|
|---|
| 46 | //
|
|---|
| 47 | // The macro can also be run without ACLiC but this is a lot slower...
|
|---|
| 48 | //
|
|---|
| 49 | // Remark: Running it from the commandline looks like this:
|
|---|
| 50 | // root -q -l -b fillcalib.C+\(\"filename\"\,kFALSE\) 2>&1 | tee fillcalib.log
|
|---|
| 51 | //
|
|---|
| 52 | // Make sure, that database and password are corretly set in a resource
|
|---|
| 53 | // file called sql.rc and the resource file is found.
|
|---|
| 54 | //
|
|---|
| 55 | // Returns 0 in case of failure and 1 in case of success.
|
|---|
| 56 | //
|
|---|
| 57 | /////////////////////////////////////////////////////////////////////////////
|
|---|
| 58 | #include <iostream>
|
|---|
| 59 |
|
|---|
| 60 | #include <TEnv.h>
|
|---|
| 61 | #include <TRegexp.h>
|
|---|
| 62 |
|
|---|
| 63 | #include <TH1.h>
|
|---|
| 64 |
|
|---|
| 65 | #include <TFile.h>
|
|---|
| 66 | #include <TSQLResult.h>
|
|---|
| 67 | #include <TSQLRow.h>
|
|---|
| 68 |
|
|---|
| 69 | #include "MSQLServer.h"
|
|---|
| 70 |
|
|---|
| 71 | #include "MStatusArray.h"
|
|---|
| 72 | #include "MHCamera.h"
|
|---|
| 73 | #include "MGeomCamMagic.h"
|
|---|
| 74 | #include "MBadPixelsCam.h"
|
|---|
| 75 |
|
|---|
| 76 | using namespace std;
|
|---|
| 77 |
|
|---|
| 78 | // --------------------------------------------------------------------------
|
|---|
| 79 | //
|
|---|
| 80 | // Checks whether an entry is already existing
|
|---|
| 81 | //
|
|---|
| 82 | Bool_t ExistStr(MSQLServer &serv, const char *column, const char *table, Int_t test)
|
|---|
| 83 | {
|
|---|
| 84 | TString query(Form("SELECT %s FROM %s WHERE %s='%d'", column, table, column, test));
|
|---|
| 85 | TSQLResult *res = serv.Query(query);
|
|---|
| 86 | if (!res)
|
|---|
| 87 | return kFALSE;
|
|---|
| 88 |
|
|---|
| 89 | TSQLRow *row;
|
|---|
| 90 |
|
|---|
| 91 | Bool_t rc = kFALSE;
|
|---|
| 92 | while ((row=res->Next()))
|
|---|
| 93 | {
|
|---|
| 94 | if ((*row)[0])
|
|---|
| 95 | {
|
|---|
| 96 | rc = kTRUE;
|
|---|
| 97 | break;
|
|---|
| 98 | }
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | delete res;
|
|---|
| 102 |
|
|---|
| 103 | return rc;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | int Process(MSQLServer &serv, TString fname, Bool_t dummy)
|
|---|
| 107 | {
|
|---|
| 108 | MBadPixelsCam badpix;
|
|---|
| 109 |
|
|---|
| 110 | TFile file(fname, "READ");
|
|---|
| 111 | if (badpix.Read("MBadPixelsCam")<=0)
|
|---|
| 112 | {
|
|---|
| 113 | cout << "ERROR - Reading of MBadPixelsCam failed." << endl;
|
|---|
| 114 | return 0;
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | MGeomCamMagic def;
|
|---|
| 118 |
|
|---|
| 119 | MGeomCam *geom = (MGeomCam*)file.Get("MGeomCam");
|
|---|
| 120 | if (!geom)
|
|---|
| 121 | {
|
|---|
| 122 | cout << "WARNING - Reading of MGeomCam failed... using default <MGeomCamMagic>" << endl;
|
|---|
| 123 | geom = &def;
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | cout << "Camera Geometry: " << geom->ClassName() << endl;
|
|---|
| 127 |
|
|---|
| 128 | const Short_t unsin = badpix.GetNumUnsuitable(MBadPixelsPix::kUnsuitableRun, geom, 0);
|
|---|
| 129 | const Short_t unsout = badpix.GetNumUnsuitable(MBadPixelsPix::kUnsuitableRun, geom, 1);
|
|---|
| 130 |
|
|---|
| 131 | const Short_t unrin = badpix.GetNumUnsuitable(MBadPixelsPix::kUnreliableRun, geom, 0);
|
|---|
| 132 | const Short_t unrout = badpix.GetNumUnsuitable(MBadPixelsPix::kUnreliableRun, geom, 1);
|
|---|
| 133 |
|
|---|
| 134 | const Short_t isoin = badpix.GetNumIsolated(*geom, 0);
|
|---|
| 135 | const Short_t isoout = badpix.GetNumIsolated(*geom, 1);
|
|---|
| 136 |
|
|---|
| 137 | const Short_t clumax = badpix.GetNumMaxCluster(*geom);
|
|---|
| 138 |
|
|---|
| 139 | if (unsin<0 || unsout<0 || unrin<0 || unrout<0 || isoin<0 || isoout<0 || clumax<0)
|
|---|
| 140 | return 0;
|
|---|
| 141 |
|
|---|
| 142 | // MHCamera hist(geom);
|
|---|
| 143 | // hist.SetCamContent(badpix, 1);
|
|---|
| 144 | // hist.DrawCopy();
|
|---|
| 145 | // hist.SetCamContent(badpix, 3);
|
|---|
| 146 | // hist.DrawCopy();
|
|---|
| 147 |
|
|---|
| 148 | //Getting values from the status display
|
|---|
| 149 | MStatusArray arr;
|
|---|
| 150 | if (arr.Read()<=0)
|
|---|
| 151 | return 0;
|
|---|
| 152 |
|
|---|
| 153 | TH1 *h;
|
|---|
| 154 |
|
|---|
| 155 | //getting the mean and rms from the arrival time (inner cam)
|
|---|
| 156 | h = (TH1*)arr.FindObjectInCanvas("HRelTimeHiGainArea0", "TH1F", "Time");
|
|---|
| 157 | if (!h)
|
|---|
| 158 | {
|
|---|
| 159 | cout << "WARNING - Could not find histogram HRelTimeHiGainArea0." << endl;
|
|---|
| 160 | return 0;
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | Float_t meani = h->GetMean();
|
|---|
| 164 | Float_t rmsi = h->GetRMS();
|
|---|
| 165 | meani = TMath::Nint(meani*10)/10.;
|
|---|
| 166 | rmsi = TMath::Nint(rmsi*10)/10.;
|
|---|
| 167 | TString meaninner = Form("%4.1f", meani);
|
|---|
| 168 | TString rmsinner = Form("%4.1f", rmsi);
|
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 | //getting the mean and rms from the arrival time (outer cam)
|
|---|
| 172 | h = (TH1*)arr.FindObjectInCanvas("HRelTimeHiGainArea1", "TH1F", "Time");
|
|---|
| 173 | if (!h)
|
|---|
| 174 | {
|
|---|
| 175 | cout << "WARNING - Could not find histogram HRelTimeHiGainArea1." << endl;
|
|---|
| 176 | return 0;
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | Float_t meano = h->GetMean();
|
|---|
| 180 | Float_t rmso = h->GetRMS();
|
|---|
| 181 | meano = TMath::Nint(meano*10)/10.;
|
|---|
| 182 | rmso = TMath::Nint(rmso*10)/10.;
|
|---|
| 183 | TString meanouter = Form("%4.1f", meano);
|
|---|
| 184 | TString rmsouter = Form("%4.1f", rmso);
|
|---|
| 185 |
|
|---|
| 186 |
|
|---|
| 187 | //Getting conversion factors
|
|---|
| 188 | MHCamera *c = (MHCamera*)arr.FindObjectInCanvas("TotalConvPhe", "MHCamera", "Conversion");
|
|---|
| 189 | if (!c)
|
|---|
| 190 | {
|
|---|
| 191 | cout << "WARNING - Could not find MHCamera TotalConv." << endl;
|
|---|
| 192 | return 0;
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | TArrayI inner(1), outer(1);
|
|---|
| 196 | inner[0] = 0;
|
|---|
| 197 | outer[0] = 1;
|
|---|
| 198 |
|
|---|
| 199 | Int_t s0[] = { 1, 2, 3, 4, 5, 6 };
|
|---|
| 200 |
|
|---|
| 201 | Stat_t meanconvi = c->GetMeanSectors(TArrayI(6, s0), inner);
|
|---|
| 202 | Stat_t meanconvo = c->GetMeanSectors(TArrayI(6, s0), outer);
|
|---|
| 203 | meanconvi = TMath::Nint(meanconvi*10)/10.;
|
|---|
| 204 | meanconvo = TMath::Nint(meanconvo*10)/10.;
|
|---|
| 205 | TString meanconvinner=Form("%4.1f", meanconvi);
|
|---|
| 206 | TString meanconvouter=Form("%4.1f", meanconvo);
|
|---|
| 207 |
|
|---|
| 208 |
|
|---|
| 209 | //Getting sequ# from filename
|
|---|
| 210 | TString sequence = fname(TRegexp("calib[0-9]+[.]root$"));
|
|---|
| 211 | if (sequence.IsNull())
|
|---|
| 212 | {
|
|---|
| 213 | cout << "WARNING - Could get sequence# from filename: " << fname << endl;
|
|---|
| 214 | return 0;
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | Int_t seq = atoi(sequence.Data()+5);
|
|---|
| 218 |
|
|---|
| 219 |
|
|---|
| 220 | cout << "Sequence #" << seq << endl;
|
|---|
| 221 | cout << " Unsuitable: (i/o) " << Form("%3d %3d", (int)unsin, (int)unsout) << endl; // Unbrauchbar
|
|---|
| 222 | cout << " Unreliable: (i/o) " << Form("%3d %3d", (int)unrin, (int)unrout) << endl; // Unzuverlaessig
|
|---|
| 223 | cout << " Isolated: (i/o) " << Form("%3d %3d", (int)isoin, (int)isoout) << endl; // Isolated (unbrauchbar)
|
|---|
| 224 | cout << " Max.Cluster: " << Form("%3d", (int)clumax) << endl; // Max Cluster
|
|---|
| 225 | cout << " Arr Time inner: " << Form("%4.1f +- %4.1f", meani, rmsi) << endl;
|
|---|
| 226 | cout << " Arr Time outer: " << Form("%4.1f +- %4.1f", meano, rmso) << endl;
|
|---|
| 227 | cout << " Mean Conv inner: " << Form("%4.1f", meanconvi) << endl;
|
|---|
| 228 | cout << " Mean Conv outer: " << Form("%4.1f", meanconvo) << endl;
|
|---|
| 229 |
|
|---|
| 230 | TString query;
|
|---|
| 231 | if (!ExistStr(serv, "fSequenceFirst", "MyMagic.Calibration", seq))
|
|---|
| 232 | {
|
|---|
| 233 | query = Form("INSERT MyMagic.Calibration SET"
|
|---|
| 234 | " fSequenceFirst=%d,"
|
|---|
| 235 | " fUnsuitableInner=%d, "
|
|---|
| 236 | " fUnsuitableOuter=%d, "
|
|---|
| 237 | " fUnreliableInner=%d, "
|
|---|
| 238 | " fUnreliableOuter=%d, "
|
|---|
| 239 | " fIsolatedInner=%d, "
|
|---|
| 240 | " fIsolatedOuter=%d, "
|
|---|
| 241 | " fIsolatedMaxCluster=%d, ",
|
|---|
| 242 | " fArrTimeMeanInner=%s, "
|
|---|
| 243 | " fArrTimeRmsInner=%s, "
|
|---|
| 244 | " fArrTimeMeanOuter=%s, "
|
|---|
| 245 | " fArrTimeRmsOuter=%s, "
|
|---|
| 246 | " fConvFactorInner=%s, "
|
|---|
| 247 | " fConvFactorOuter=%s ",
|
|---|
| 248 | seq, (int)unsin, (int)unsout, (int)unrin,
|
|---|
| 249 | (int)unrout, (int)isoin, (int)isoout,
|
|---|
| 250 | (int)clumax,
|
|---|
| 251 | meaninner.Data(), rmsinner.Data(),
|
|---|
| 252 | meanouter.Data(), rmsouter.Data(),
|
|---|
| 253 | meanconvinner.Data(), meanconvouter.Data());
|
|---|
| 254 | }
|
|---|
| 255 | else
|
|---|
| 256 | {
|
|---|
| 257 | query = Form("UPDATE MyMagic.Calibration SET"
|
|---|
| 258 | " fUnsuitableInner=%d, "
|
|---|
| 259 | " fUnsuitableOuter=%d, "
|
|---|
| 260 | " fUnreliableInner=%d, "
|
|---|
| 261 | " fUnreliableOuter=%d, "
|
|---|
| 262 | " fIsolatedInner=%d, "
|
|---|
| 263 | " fIsolatedOuter=%d, "
|
|---|
| 264 | " fIsolatedMaxCluster=%d, "
|
|---|
| 265 | " fArrTimeMeanInner=%s, "
|
|---|
| 266 | " fArrTimeRmsInner=%s, "
|
|---|
| 267 | " fArrTimeMeanOuter=%s, "
|
|---|
| 268 | " fArrTimeRmsOuter=%s, "
|
|---|
| 269 | " fConvFactorInner=%s, "
|
|---|
| 270 | " fConvFactorOuter=%s "
|
|---|
| 271 | " WHERE fSequenceFirst=%d ",
|
|---|
| 272 | (int)unsin, (int)unsout, (int)unrin,(int)unrout,
|
|---|
| 273 | (int)isoin, (int)isoout, (int)clumax,
|
|---|
| 274 | meaninner.Data(), rmsinner.Data(),
|
|---|
| 275 | meanouter.Data(), rmsouter.Data(),
|
|---|
| 276 | meanconvinner.Data(), meanconvouter.Data(),
|
|---|
| 277 | seq);
|
|---|
| 278 | }
|
|---|
| 279 |
|
|---|
| 280 | if (dummy)
|
|---|
| 281 | return 0;
|
|---|
| 282 |
|
|---|
| 283 | TSQLResult *res = serv.Query(query);
|
|---|
| 284 | if (!res)
|
|---|
| 285 | {
|
|---|
| 286 | cout << "ERROR - Query failed: " << query << endl;
|
|---|
| 287 | return 0;
|
|---|
| 288 | }
|
|---|
| 289 | delete res;
|
|---|
| 290 |
|
|---|
| 291 | return 1;
|
|---|
| 292 | }
|
|---|
| 293 |
|
|---|
| 294 | int fillcalib(TString fname, Bool_t dummy=kTRUE)
|
|---|
| 295 | {
|
|---|
| 296 | TEnv env("sql.rc");
|
|---|
| 297 |
|
|---|
| 298 | MSQLServer serv(env);
|
|---|
| 299 | if (!serv.IsConnected())
|
|---|
| 300 | {
|
|---|
| 301 | cout << "ERROR - Connection to database failed." << endl;
|
|---|
| 302 | return 0;
|
|---|
| 303 | }
|
|---|
| 304 |
|
|---|
| 305 | cout << "fillcalib" << endl;
|
|---|
| 306 | cout << "---------" << endl;
|
|---|
| 307 | cout << endl;
|
|---|
| 308 | cout << "Connected to " << serv.GetName() << endl;
|
|---|
| 309 | cout << "File: " << fname << endl;
|
|---|
| 310 | cout << endl;
|
|---|
| 311 |
|
|---|
| 312 | return Process(serv, fname, dummy);
|
|---|
| 313 | }
|
|---|