| 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-2006 | 
|---|
| 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 | // The bad pixel information and other information, extracted from the status | 
|---|
| 37 | // display, is inserted into the database in the table Calibration, which | 
|---|
| 38 | // stores the results from the calibration. | 
|---|
| 39 | // The corresponding sequence number is extracted from the filename... | 
|---|
| 40 | // FIXME: MSeqeuence should be stored in the calib-file? | 
|---|
| 41 | // | 
|---|
| 42 | // Usage: | 
|---|
| 43 | //  .x fillcalib.C("/magic/data/callisto/0004/00047421/calib00047421.root", kTRUE) | 
|---|
| 44 | // | 
|---|
| 45 | // The second argument is the 'dummy-mode'. If it is kTRUE dummy-mode is | 
|---|
| 46 | // switched on and nothing will be written into the database. This is usefull | 
|---|
| 47 | // for tests. | 
|---|
| 48 | // | 
|---|
| 49 | // The macro can also be run without ACLiC but this is a lot slower... | 
|---|
| 50 | // | 
|---|
| 51 | // Remark: Running it from the commandline looks like this: | 
|---|
| 52 | //   root -q -l -b fillcalib.C+\(\"filename\"\,kFALSE\) 2>&1 | tee fillcalib.log | 
|---|
| 53 | // | 
|---|
| 54 | // Make sure, that database and password are corretly set in a resource | 
|---|
| 55 | // file called sql.rc and the resource file is found. | 
|---|
| 56 | // | 
|---|
| 57 | // Returns 2 in case of failure, 1 in case of success and 0 if the connection | 
|---|
| 58 | // to the database is not working. | 
|---|
| 59 | // | 
|---|
| 60 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
| 61 | #include <iostream> | 
|---|
| 62 |  | 
|---|
| 63 | #include <TEnv.h> | 
|---|
| 64 | #include <TRegexp.h> | 
|---|
| 65 |  | 
|---|
| 66 | #include <TH1.h> | 
|---|
| 67 |  | 
|---|
| 68 | #include <TFile.h> | 
|---|
| 69 | #include <TSQLResult.h> | 
|---|
| 70 | #include <TSQLRow.h> | 
|---|
| 71 |  | 
|---|
| 72 | #include "MSQLServer.h" | 
|---|
| 73 |  | 
|---|
| 74 | #include "MStatusArray.h" | 
|---|
| 75 | #include "MHCamera.h" | 
|---|
| 76 | #include "MGeomCamMagic.h" | 
|---|
| 77 | #include "MBadPixelsCam.h" | 
|---|
| 78 |  | 
|---|
| 79 | using namespace std; | 
|---|
| 80 |  | 
|---|
| 81 | // -------------------------------------------------------------------------- | 
|---|
| 82 | // | 
|---|
| 83 | // Checks whether an entry is already existing | 
|---|
| 84 | // | 
|---|
| 85 | Bool_t ExistStr(MSQLServer &serv, const char *column, const char *table, Int_t test) | 
|---|
| 86 | { | 
|---|
| 87 | TString query(Form("SELECT %s FROM %s WHERE %s='%d'", column, table, column, test)); | 
|---|
| 88 | TSQLResult *res = serv.Query(query); | 
|---|
| 89 | if (!res) | 
|---|
| 90 | return kFALSE; | 
|---|
| 91 |  | 
|---|
| 92 | TSQLRow *row; | 
|---|
| 93 |  | 
|---|
| 94 | Bool_t rc = kFALSE; | 
|---|
| 95 | while ((row=res->Next())) | 
|---|
| 96 | { | 
|---|
| 97 | if ((*row)[0]) | 
|---|
| 98 | { | 
|---|
| 99 | rc = kTRUE; | 
|---|
| 100 | break; | 
|---|
| 101 | } | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | delete res; | 
|---|
| 105 |  | 
|---|
| 106 | return rc; | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 | int Process(MSQLServer &serv, TString fname, Bool_t dummy) | 
|---|
| 110 | { | 
|---|
| 111 | //getting number of unsuitable, unreliable and isolated pixel | 
|---|
| 112 | MBadPixelsCam badpix; | 
|---|
| 113 |  | 
|---|
| 114 | TFile file(fname, "READ"); | 
|---|
| 115 | if (!file.IsOpen()) | 
|---|
| 116 | { | 
|---|
| 117 | cout << "ERROR - Could not find file " << fname << endl; | 
|---|
| 118 | return 2; | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 | if (badpix.Read("MBadPixelsCam")<=0) | 
|---|
| 122 | { | 
|---|
| 123 | cout << "ERROR - Reading of MBadPixelsCam failed." << endl; | 
|---|
| 124 | return 2; | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | MGeomCamMagic def; | 
|---|
| 128 |  | 
|---|
| 129 | MGeomCam *geom = (MGeomCam*)file.Get("MGeomCam"); | 
|---|
| 130 | if (!geom) | 
|---|
| 131 | { | 
|---|
| 132 | cout << "WARNING - Reading of MGeomCam failed... using default <MGeomCamMagic>" << endl; | 
|---|
| 133 | geom = &def; | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|
| 136 | cout << "Camera Geometry: " << geom->ClassName() << endl; | 
|---|
| 137 |  | 
|---|
| 138 | const Short_t unsin  = badpix.GetNumUnsuitable(MBadPixelsPix::kUnsuitableRun, geom, 0); | 
|---|
| 139 | const Short_t unsout = badpix.GetNumUnsuitable(MBadPixelsPix::kUnsuitableRun, geom, 1); | 
|---|
| 140 |  | 
|---|
| 141 | const Short_t unrin  = badpix.GetNumUnsuitable(MBadPixelsPix::kUnreliableRun, geom, 0); | 
|---|
| 142 | const Short_t unrout = badpix.GetNumUnsuitable(MBadPixelsPix::kUnreliableRun, geom, 1); | 
|---|
| 143 |  | 
|---|
| 144 | const Short_t isoin  = badpix.GetNumIsolated(*geom, 0); | 
|---|
| 145 | const Short_t isoout = badpix.GetNumIsolated(*geom, 1); | 
|---|
| 146 |  | 
|---|
| 147 | const Short_t clumax = badpix.GetNumMaxCluster(*geom); | 
|---|
| 148 |  | 
|---|
| 149 | if (unsin<0 || unsout<0 || unrin<0 || unrout<0 || isoin<0 || isoout<0 || clumax<0) | 
|---|
| 150 | { | 
|---|
| 151 | cout << "ERROR - one of the pixel values < 0." << endl; | 
|---|
| 152 | return 2; | 
|---|
| 153 | } | 
|---|
| 154 |  | 
|---|
| 155 | //     MHCamera hist(geom); | 
|---|
| 156 | //     hist.SetCamContent(badpix, 1); | 
|---|
| 157 | //     hist.DrawCopy(); | 
|---|
| 158 | //     hist.SetCamContent(badpix, 3); | 
|---|
| 159 | //     hist.DrawCopy(); | 
|---|
| 160 |  | 
|---|
| 161 | //Getting values from the status display | 
|---|
| 162 | MStatusArray arr; | 
|---|
| 163 | if (arr.Read()<=0) | 
|---|
| 164 | { | 
|---|
| 165 | cout << "ERROR - could not read MStatusArray." << endl; | 
|---|
| 166 | return 2; | 
|---|
| 167 | } | 
|---|
| 168 |  | 
|---|
| 169 | TH1 *h; | 
|---|
| 170 |  | 
|---|
| 171 | //getting the mean and rms from the arrival time (inner cam) | 
|---|
| 172 | h = (TH1*)arr.FindObjectInCanvas("HRelTimeHiGainArea0", "TH1F", "Time"); | 
|---|
| 173 | if (!h) | 
|---|
| 174 | { | 
|---|
| 175 | cout << "WARNING - Could not find histogram HRelTimeHiGainArea0." << endl; | 
|---|
| 176 | return 2; | 
|---|
| 177 | } | 
|---|
| 178 |  | 
|---|
| 179 | Float_t meani = h->GetMean(); | 
|---|
| 180 | Float_t rmsi  = h->GetRMS(); | 
|---|
| 181 | meani = TMath::Nint(meani*10)/10.; | 
|---|
| 182 | rmsi  = TMath::Nint(rmsi*100)/100.; | 
|---|
| 183 | TString meaninner = Form("%5.1f", meani); | 
|---|
| 184 | TString rmsinner  = Form("%6.2f", rmsi); | 
|---|
| 185 |  | 
|---|
| 186 |  | 
|---|
| 187 | //getting the mean and rms from the arrival time (outer cam) | 
|---|
| 188 | h = (TH1*)arr.FindObjectInCanvas("HRelTimeHiGainArea1", "TH1F", "Time"); | 
|---|
| 189 | if (!h) | 
|---|
| 190 | { | 
|---|
| 191 | cout << "WARNING - Could not find histogram HRelTimeHiGainArea1." << endl; | 
|---|
| 192 | return 2; | 
|---|
| 193 | } | 
|---|
| 194 |  | 
|---|
| 195 | Float_t meano = h->GetMean(); | 
|---|
| 196 | Float_t rmso  = h->GetRMS(); | 
|---|
| 197 | meano = TMath::Nint(meano*10)/10.; | 
|---|
| 198 | rmso  = TMath::Nint(rmso*100)/100.; | 
|---|
| 199 | TString meanouter = Form("%5.1f", meano); | 
|---|
| 200 | TString rmsouter  = Form("%6.2f", rmso); | 
|---|
| 201 |  | 
|---|
| 202 |  | 
|---|
| 203 | //Getting conversion factors | 
|---|
| 204 | MHCamera *c = (MHCamera*)arr.FindObjectInCanvas("TotalConvPhe", "MHCamera", "Conversion"); | 
|---|
| 205 | if (!c) | 
|---|
| 206 | { | 
|---|
| 207 | cout << "WARNING - Could not find MHCamera TotalConv." << endl; | 
|---|
| 208 | return 2; | 
|---|
| 209 | } | 
|---|
| 210 |  | 
|---|
| 211 | TArrayI inner(1), outer(1); | 
|---|
| 212 | inner[0] = 0; | 
|---|
| 213 | outer[0] = 1; | 
|---|
| 214 |  | 
|---|
| 215 | Int_t s0[] = { 1, 2, 3, 4, 5, 6 }; | 
|---|
| 216 |  | 
|---|
| 217 | Stat_t meanconvi = c->GetMeanSectors(TArrayI(6, s0), inner); | 
|---|
| 218 | Stat_t meanconvo = c->GetMeanSectors(TArrayI(6, s0), outer); | 
|---|
| 219 | meanconvi = TMath::Nint(meanconvi*1000)/1000.; | 
|---|
| 220 | meanconvo = TMath::Nint(meanconvo*1000)/1000.; | 
|---|
| 221 | TString meanconvinner=Form("%6.3f", meanconvi); | 
|---|
| 222 | TString meanconvouter=Form("%6.3f", meanconvo); | 
|---|
| 223 |  | 
|---|
| 224 |  | 
|---|
| 225 | //Getting sequ# from filename | 
|---|
| 226 | TString sequence = fname(TRegexp("calib[0-9]+[.]root$")); | 
|---|
| 227 | if (sequence.IsNull()) | 
|---|
| 228 | { | 
|---|
| 229 | cout << "WARNING - Could get sequence# from filename: " << fname << endl; | 
|---|
| 230 | return 2; | 
|---|
| 231 | } | 
|---|
| 232 |  | 
|---|
| 233 | Int_t seq = atoi(sequence.Data()+5); | 
|---|
| 234 |  | 
|---|
| 235 |  | 
|---|
| 236 | cout << "Sequence #" << seq << endl; | 
|---|
| 237 | cout << "  Unsuitable: (i/o) " << Form("%3d %3d", (int)unsin, (int)unsout) << endl; // Unbrauchbar | 
|---|
| 238 | cout << "  Unreliable: (i/o) " << Form("%3d %3d", (int)unrin, (int)unrout) << endl; // Unzuverlaessig | 
|---|
| 239 | cout << "  Isolated:   (i/o) " << Form("%3d %3d", (int)isoin, (int)isoout) << endl; // Isolated (unbrauchbar) | 
|---|
| 240 | cout << "  Max.Cluster:      " << Form("%3d", (int)clumax)                 << endl; // Max Cluster | 
|---|
| 241 | cout << "  Arr Time inner:    " << meaninner << " +- " << rmsinner  << endl; | 
|---|
| 242 | cout << "  Arr Time outer:    " << meanouter << " +- " << rmsouter  << endl; | 
|---|
| 243 | cout << "  Mean Conv inner:   " << meanconvinner << endl; | 
|---|
| 244 | cout << "  Mean Conv outer:   " << meanconvouter << endl; | 
|---|
| 245 |  | 
|---|
| 246 | //inserting or updating the information in the database | 
|---|
| 247 | TString query; | 
|---|
| 248 | if (!ExistStr(serv, "fSequenceFirst", "Calibration", seq)) | 
|---|
| 249 | { | 
|---|
| 250 | query = Form("INSERT Calibration SET" | 
|---|
| 251 | " fSequenceFirst=%d," | 
|---|
| 252 | " fUnsuitableInner=%d, " | 
|---|
| 253 | " fUnsuitableOuter=%d, " | 
|---|
| 254 | " fUnreliableInner=%d, " | 
|---|
| 255 | " fUnreliableOuter=%d, " | 
|---|
| 256 | " fIsolatedInner=%d, " | 
|---|
| 257 | " fIsolatedOuter=%d, " | 
|---|
| 258 | " fIsolatedMaxCluster=%d, " | 
|---|
| 259 | " fArrTimeMeanInner=%s, " | 
|---|
| 260 | " fArrTimeRmsInner=%s, " | 
|---|
| 261 | " fArrTimeMeanOuter=%s, " | 
|---|
| 262 | " fArrTimeRmsOuter=%s, " | 
|---|
| 263 | " fConvFactorInner=%s, " | 
|---|
| 264 | " fConvFactorOuter=%s ", | 
|---|
| 265 | seq, (int)unsin, (int)unsout, (int)unrin, | 
|---|
| 266 | (int)unrout, (int)isoin, (int)isoout, | 
|---|
| 267 | (int)clumax, | 
|---|
| 268 | meaninner.Data(), rmsinner.Data(), | 
|---|
| 269 | meanouter.Data(), rmsouter.Data(), | 
|---|
| 270 | meanconvinner.Data(), meanconvouter.Data()); | 
|---|
| 271 | } | 
|---|
| 272 | else | 
|---|
| 273 | { | 
|---|
| 274 | query = Form("UPDATE Calibration SET" | 
|---|
| 275 | " fUnsuitableInner=%d, " | 
|---|
| 276 | " fUnsuitableOuter=%d, " | 
|---|
| 277 | " fUnreliableInner=%d, " | 
|---|
| 278 | " fUnreliableOuter=%d, " | 
|---|
| 279 | " fIsolatedInner=%d, " | 
|---|
| 280 | " fIsolatedOuter=%d, " | 
|---|
| 281 | " fIsolatedMaxCluster=%d, " | 
|---|
| 282 | " fArrTimeMeanInner=%s, " | 
|---|
| 283 | " fArrTimeRmsInner=%s, " | 
|---|
| 284 | " fArrTimeMeanOuter=%s, " | 
|---|
| 285 | " fArrTimeRmsOuter=%s, " | 
|---|
| 286 | " fConvFactorInner=%s, " | 
|---|
| 287 | " fConvFactorOuter=%s " | 
|---|
| 288 | " WHERE fSequenceFirst=%d ", | 
|---|
| 289 | (int)unsin, (int)unsout, (int)unrin,(int)unrout, | 
|---|
| 290 | (int)isoin, (int)isoout, (int)clumax, | 
|---|
| 291 | meaninner.Data(), rmsinner.Data(), | 
|---|
| 292 | meanouter.Data(), rmsouter.Data(), | 
|---|
| 293 | meanconvinner.Data(), meanconvouter.Data(), | 
|---|
| 294 | seq); | 
|---|
| 295 | } | 
|---|
| 296 |  | 
|---|
| 297 | if (dummy) | 
|---|
| 298 | return 1; | 
|---|
| 299 |  | 
|---|
| 300 | TSQLResult *res = serv.Query(query); | 
|---|
| 301 | if (!res) | 
|---|
| 302 | { | 
|---|
| 303 | cout << "ERROR - Query failed: " << query << endl; | 
|---|
| 304 | return 2; | 
|---|
| 305 | } | 
|---|
| 306 | delete res; | 
|---|
| 307 |  | 
|---|
| 308 | return 1; | 
|---|
| 309 | } | 
|---|
| 310 |  | 
|---|
| 311 | int fillcalib(TString fname, Bool_t dummy=kTRUE) | 
|---|
| 312 | { | 
|---|
| 313 | TEnv env("sql.rc"); | 
|---|
| 314 |  | 
|---|
| 315 | MSQLServer serv(env); | 
|---|
| 316 | if (!serv.IsConnected()) | 
|---|
| 317 | { | 
|---|
| 318 | cout << "ERROR - Connection to database failed." << endl; | 
|---|
| 319 | return 0; | 
|---|
| 320 | } | 
|---|
| 321 |  | 
|---|
| 322 | cout << "fillcalib" << endl; | 
|---|
| 323 | cout << "---------" << endl; | 
|---|
| 324 | cout << endl; | 
|---|
| 325 | cout << "Connected to " << serv.GetName() << endl; | 
|---|
| 326 | cout << "File: " << fname << endl; | 
|---|
| 327 | cout << endl; | 
|---|
| 328 |  | 
|---|
| 329 | return Process(serv, fname, dummy); | 
|---|
| 330 | } | 
|---|