| 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, 04/2005 <mailto:tbretz@astro.uni-wuerzburg.de> | 
|---|
| 19 | !   Author(s): Daniela Dorner, 04/2005 <mailto:dorner@astro.uni-wuerzburg.de> | 
|---|
| 20 | ! | 
|---|
| 21 | !   Copyright: MAGIC Software Development, 2000-2006 | 
|---|
| 22 | ! | 
|---|
| 23 | ! | 
|---|
| 24 | \* ======================================================================== */ | 
|---|
| 25 |  | 
|---|
| 26 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
| 27 | // | 
|---|
| 28 | // fillsinope.C | 
|---|
| 29 | // ============ | 
|---|
| 30 | // | 
|---|
| 31 | // This macro is used to read the sinope output files sinope*.txt | 
|---|
| 32 | // For each run sinope is run twice: once for the data events and once for the | 
|---|
| 33 | // calibration events. The pulse position and height is checked. The output is | 
|---|
| 34 | // stored in a sinope*.root and a sinope*.txt file. The files for data events | 
|---|
| 35 | // are marked with -dat and the ones for calibration events with -cal. | 
|---|
| 36 | // From the txt files the result is extracted and inserted into the database | 
|---|
| 37 | // in the table DataCheck, which stores the datacheck results. | 
|---|
| 38 | // As the column names in the database differ only by the addition 'Interlaced' | 
|---|
| 39 | // which is given to columns corresponding to the calibration events, the | 
|---|
| 40 | // files can be processed with the same function. | 
|---|
| 41 | // | 
|---|
| 42 | // Usage: | 
|---|
| 43 | //   .x fillsinope.C(runnumber, "datapath", kTRUE) | 
|---|
| 44 | // | 
|---|
| 45 | // The first argument is the runnumber (given as int), the second argument is | 
|---|
| 46 | // the datapath, where the rawfiles can be found. The last argument is the | 
|---|
| 47 | // 'dummy-mode'. If it is kTRUE dummy-mode is switched on and nothing will be | 
|---|
| 48 | // written into the database. This is usefull for tests. | 
|---|
| 49 | // | 
|---|
| 50 | // Remark: Running it from the commandline looks like this: | 
|---|
| 51 | //   root -q -l -b fillsinope.C+\(runno\,kFALSE\) 2>&1 | tee fillsinope.log | 
|---|
| 52 | // | 
|---|
| 53 | // Make sure, that database and password are corretly set in a resource | 
|---|
| 54 | // file called sql.rc and the resource file is found. | 
|---|
| 55 | // | 
|---|
| 56 | // Returns 2 in case of failure, 1 in case of success and 0 if the connection | 
|---|
| 57 | // to the database is not working. | 
|---|
| 58 | // | 
|---|
| 59 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
| 60 | #include <iostream> | 
|---|
| 61 |  | 
|---|
| 62 | #include <TEnv.h> | 
|---|
| 63 | #include <TRegexp.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 |  | 
|---|
| 74 | using namespace std; | 
|---|
| 75 |  | 
|---|
| 76 | Bool_t ExistStr(MSQLServer &serv, const char *column, const char *table, const char *test) | 
|---|
| 77 | { | 
|---|
| 78 | TString query(Form("SELECT %s FROM %s WHERE %s='%s'", column, table, column, test)); | 
|---|
| 79 | TSQLResult *res = serv.Query(query); | 
|---|
| 80 | if (!res) | 
|---|
| 81 | { | 
|---|
| 82 | cout << "ERROR - Query failed: " << query << endl; | 
|---|
| 83 | return kFALSE; | 
|---|
| 84 | } | 
|---|
| 85 |  | 
|---|
| 86 | Bool_t rc = kFALSE; | 
|---|
| 87 |  | 
|---|
| 88 | TSQLRow *row=res->Next(); | 
|---|
| 89 | if (row && (*row)[0]) | 
|---|
| 90 | rc=kTRUE; | 
|---|
| 91 |  | 
|---|
| 92 | delete res; | 
|---|
| 93 | return rc; | 
|---|
| 94 | } | 
|---|
| 95 |  | 
|---|
| 96 | int Process(MSQLServer &serv, TString fname, Int_t runno, Bool_t cal, Bool_t dummy) | 
|---|
| 97 | { | 
|---|
| 98 | TEnv env(fname); | 
|---|
| 99 |  | 
|---|
| 100 | //build query | 
|---|
| 101 | TString query="UPDATE DataCheck SET "; | 
|---|
| 102 |  | 
|---|
| 103 | //array with part of column names | 
|---|
| 104 | TString values[9] = { "Events" , "HasSignal" , "HasPedestal" , "PositionSignal" , "PositionFWHM" , "PositionAsym" , "HeightSignal" , "HeightFWHM" , "HeightAsym" }; | 
|---|
| 105 |  | 
|---|
| 106 | //get values from the file add them to query | 
|---|
| 107 | TString str; | 
|---|
| 108 | for (Int_t i=0 ; i<9 ; i++) | 
|---|
| 109 | { | 
|---|
| 110 | str = env.GetValue(values[i], ""); | 
|---|
| 111 | if (str.IsNull()) | 
|---|
| 112 | continue; | 
|---|
| 113 |  | 
|---|
| 114 | if (cal) | 
|---|
| 115 | values[i]+="InterLaced"; | 
|---|
| 116 | values[i]+="='"; | 
|---|
| 117 | values[i]+=str; | 
|---|
| 118 |  | 
|---|
| 119 | if (i!=0) | 
|---|
| 120 | query+=", "; | 
|---|
| 121 | query+=" f"; | 
|---|
| 122 | query+=values[i]; | 
|---|
| 123 | query+="' "; | 
|---|
| 124 |  | 
|---|
| 125 | cout << "value: " << values[i] << endl; | 
|---|
| 126 |  | 
|---|
| 127 | } | 
|---|
| 128 |  | 
|---|
| 129 | query+=Form(" WHERE fRunNumber=%d", runno); | 
|---|
| 130 |  | 
|---|
| 131 | cout << "Q: " << query << endl; | 
|---|
| 132 | //insert information into db | 
|---|
| 133 | TSQLResult *res = serv.Query(query); | 
|---|
| 134 | if (!res) | 
|---|
| 135 | { | 
|---|
| 136 | cout << "ERROR - Query failed: " << query << endl; | 
|---|
| 137 | return 2; | 
|---|
| 138 | } | 
|---|
| 139 | delete res; | 
|---|
| 140 | return 1; | 
|---|
| 141 | } | 
|---|
| 142 |  | 
|---|
| 143 | int fillsinope(Int_t runno, TString datapath, Bool_t dummy=kTRUE) | 
|---|
| 144 | { | 
|---|
| 145 | TEnv env("sql.rc"); | 
|---|
| 146 |  | 
|---|
| 147 | MSQLServer serv(env); | 
|---|
| 148 | if (!serv.IsConnected()) | 
|---|
| 149 | { | 
|---|
| 150 | cout << "ERROR - Connection to database failed." << endl; | 
|---|
| 151 | return 0; | 
|---|
| 152 | } | 
|---|
| 153 |  | 
|---|
| 154 | cout << "fillsignal" << endl; | 
|---|
| 155 | cout << "----------" << endl; | 
|---|
| 156 | cout << endl; | 
|---|
| 157 | cout << "Connected to " << serv.GetName() << endl; | 
|---|
| 158 | cout << "Run: " << runno << endl; | 
|---|
| 159 | cout << endl; | 
|---|
| 160 |  | 
|---|
| 161 | //get date of run from database | 
|---|
| 162 | TString query(Form("SELECT DATE_FORMAT(ADDDATE(fRunStart, Interval 13 HOUR), '%%Y/%%m/%%d') FROM RunData WHERE fRunNumber=%d", | 
|---|
| 163 | runno)); | 
|---|
| 164 |  | 
|---|
| 165 | TSQLResult *res = serv.Query(query); | 
|---|
| 166 | if (!res) | 
|---|
| 167 | { | 
|---|
| 168 | cout << "ERROR - Query failed: " << query << endl; | 
|---|
| 169 | return 2; | 
|---|
| 170 | } | 
|---|
| 171 |  | 
|---|
| 172 | TSQLRow *row = 0; | 
|---|
| 173 | row = res->Next(); | 
|---|
| 174 | TString date=(*row)[0]; | 
|---|
| 175 | cout << "date: " << date << endl; | 
|---|
| 176 | delete res; | 
|---|
| 177 |  | 
|---|
| 178 | //insert entry for the run into the database in the table DataCheck, if it is not yet existing | 
|---|
| 179 | if (!ExistStr(serv, "fRunNumber", "DataCheck", Form("%d", runno))) | 
|---|
| 180 | { | 
|---|
| 181 | query=Form("INSERT DataCheck SET fRunNumber=%d", runno); | 
|---|
| 182 |  | 
|---|
| 183 | res = serv.Query(query); | 
|---|
| 184 | if (!res) | 
|---|
| 185 | { | 
|---|
| 186 | cout << "ERROR - Query failed: " << query << endl; | 
|---|
| 187 | return 2; | 
|---|
| 188 | } | 
|---|
| 189 | } | 
|---|
| 190 |  | 
|---|
| 191 | //get filenames of sinope output files | 
|---|
| 192 | TString fname(Form("%s/sinope/%s/sinope-dat%08d.txt", | 
|---|
| 193 | datapath.Data(), date.Data(), runno)); | 
|---|
| 194 | cout << "file: " << fname << endl; | 
|---|
| 195 | TString fnamecal(Form("%s/sinope/%s/sinope-cal%08d.txt", | 
|---|
| 196 | datapath.Data(), date.Data(), runno)); | 
|---|
| 197 | cout << "file-cal: " << fnamecal << endl; | 
|---|
| 198 |  | 
|---|
| 199 | Int_t rc=0; | 
|---|
| 200 | //process dat-file | 
|---|
| 201 | rc=Process(serv, fname, runno, kFALSE, dummy); | 
|---|
| 202 | if (rc==2) | 
|---|
| 203 | return rc; | 
|---|
| 204 |  | 
|---|
| 205 | //process cal-file | 
|---|
| 206 | rc=Process(serv, fnamecal, runno, kTRUE, dummy); | 
|---|
| 207 | return rc; | 
|---|
| 208 | } | 
|---|