| 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): Daniela Dorner, 01/2005 <mailto:dorner@astro.uni-wuerzburg.de> | 
|---|
| 19 | ! | 
|---|
| 20 | !   Copyright: MAGIC Software Development, 2000-2006 | 
|---|
| 21 | ! | 
|---|
| 22 | ! | 
|---|
| 23 | \* ======================================================================== */ | 
|---|
| 24 |  | 
|---|
| 25 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
| 26 | // | 
|---|
| 27 | // findcacofiles.C | 
|---|
| 28 | // =============== | 
|---|
| 29 | // | 
|---|
| 30 | // Macro to find caco files for runs which don't have a dedicated caco file. | 
|---|
| 31 | // Called by the script filesondisk | 
|---|
| 32 | // | 
|---|
| 33 | // Sometimes the DAQ aborts a run and starts itself a new one. In this cases | 
|---|
| 34 | // the camera controll doesn't start a new file, as the command to | 
|---|
| 35 | // start a new run was not sent by the central control. So the caco | 
|---|
| 36 | // information is stored in the previous caco file, which has a different | 
|---|
| 37 | // runnumber. To be able to merpp the information into the calibrated data | 
|---|
| 38 | // file, the runnumber of the file containing the information has to be found. | 
|---|
| 39 | // | 
|---|
| 40 | // findcacofiles.C searches in the database for runs which don't have a | 
|---|
| 41 | // dedicated caco file, and searches for each of these runs, if one of the 10 | 
|---|
| 42 | // previous runs has a dedicated caco file. In case one is found, it is | 
|---|
| 43 | // inserted into the database. | 
|---|
| 44 | // | 
|---|
| 45 | // Usage: | 
|---|
| 46 | //   .x findcacofiles.C+ | 
|---|
| 47 | // | 
|---|
| 48 | // Make sure, that database and password are corretly set in a resource | 
|---|
| 49 | // file called sql.rc and the resource file is found. | 
|---|
| 50 | // | 
|---|
| 51 | // Returns 0 in case of failure and 1 in case of success. | 
|---|
| 52 | // | 
|---|
| 53 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
| 54 |  | 
|---|
| 55 | #include <iostream> | 
|---|
| 56 | #include <iomanip> | 
|---|
| 57 | #include <fstream> | 
|---|
| 58 |  | 
|---|
| 59 | #include <TEnv.h> | 
|---|
| 60 | #include <TSystem.h> | 
|---|
| 61 |  | 
|---|
| 62 | #include "MSQLMagic.h" | 
|---|
| 63 | #include <TSQLRow.h> | 
|---|
| 64 | #include <TSQLResult.h> | 
|---|
| 65 |  | 
|---|
| 66 | using namespace std; | 
|---|
| 67 |  | 
|---|
| 68 |  | 
|---|
| 69 | int findcacofiles() | 
|---|
| 70 | { | 
|---|
| 71 | TEnv env("sql.rc"); | 
|---|
| 72 |  | 
|---|
| 73 | MSQLMagic serv(env); | 
|---|
| 74 | if (!serv.IsConnected()) | 
|---|
| 75 | { | 
|---|
| 76 | cout << "ERROR - Connection to database failed." << endl; | 
|---|
| 77 | return 0; | 
|---|
| 78 | } | 
|---|
| 79 | cout << "findcacofiles" << endl; | 
|---|
| 80 | cout << "-------------" << endl; | 
|---|
| 81 | cout << endl; | 
|---|
| 82 | cout << "Connected to " << serv.GetName() << endl; | 
|---|
| 83 | cout << endl; | 
|---|
| 84 |  | 
|---|
| 85 | //get runnumbers and dates from database | 
|---|
| 86 | TString query="SELECT RunProcessStatus.fRunNumber, "; | 
|---|
| 87 | query+=" DATE_FORMAT(ADDDATE(if(fRunStart='0000-00-00 00:00:00', fRunStop, fRunStart), INTERVAL +13 HOUR), '%Y/%m/%d') "; | 
|---|
| 88 | query+=" FROM RunProcessStatus "; | 
|---|
| 89 | query+=" LEFT JOIN RunData ON RunData.fRunNumber=RunProcessStatus.fRunNumber "; | 
|---|
| 90 | query+=" LEFT JOIN Source ON RunData.fSourceKEY=Source.fSourceKEY "; | 
|---|
| 91 | query+=" WHERE IsNull(fCaCoFileFound) AND fExcludedFDAKEY=1 "; | 
|---|
| 92 | query+=" AND RunProcessStatus.fRunNumber > 10000 AND NOT IsNull(fCCFileAvail)"; | 
|---|
| 93 | query+=" AND fTest='no'"; | 
|---|
| 94 |  | 
|---|
| 95 | TSQLResult *res = serv.Query(query); | 
|---|
| 96 | if (!res) | 
|---|
| 97 | { | 
|---|
| 98 | cout << "Error." << endl; | 
|---|
| 99 | return 0; | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 | Int_t counter=0; | 
|---|
| 103 | Int_t counter2=0; | 
|---|
| 104 | TSQLRow *row=0; | 
|---|
| 105 | while ((row = res->Next())) | 
|---|
| 106 | { | 
|---|
| 107 | //search nearest previous available CaCoFile | 
|---|
| 108 | Int_t run=atoi((*row)[0]); | 
|---|
| 109 | if (TString((*row)[1]).IsNull()) | 
|---|
| 110 | { | 
|---|
| 111 | cout << "For run " << (*row)[0] << " fRunStart and fRunStop are 0000-00-00 00:00:00. No CaCoFile can be determined. " << endl; | 
|---|
| 112 | continue; | 
|---|
| 113 | } | 
|---|
| 114 | cout << "CaCoFile missing for run " << (*row)[0] << " with date " << (*row)[1] << endl; | 
|---|
| 115 |  | 
|---|
| 116 | query ="SELECT MAX(fCaCoFileFound) FROM RunProcessStatus "; | 
|---|
| 117 | query+=" LEFT JOIN RunData ON RunData.fRunNumber=RunProcessStatus.fRunNumber "; | 
|---|
| 118 | query+=Form("WHERE DATE_FORMAT(ADDDATE(fRunStart, INTERVAL +13 HOUR), '%%Y/%%m/%%d')='%s' ", | 
|---|
| 119 | (*row)[1]); | 
|---|
| 120 | query+=Form("AND RunData.fRunNumber IN (%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d)", | 
|---|
| 121 | run, run-1, run-2, run-3, run-4, run-5, | 
|---|
| 122 | run-6, run-7, run-8, run-9, run-10); | 
|---|
| 123 |  | 
|---|
| 124 | TSQLResult *res2 = serv.Query(query); | 
|---|
| 125 | if (!res2) | 
|---|
| 126 | { | 
|---|
| 127 | cout << "Error." << endl; | 
|---|
| 128 | return 0; | 
|---|
| 129 | } | 
|---|
| 130 | TSQLRow *row2=0; | 
|---|
| 131 | row2 = res2->Next(); | 
|---|
| 132 | if ((*row2)[0]) | 
|---|
| 133 | { | 
|---|
| 134 | cout << "Found CaCoFile at run " << (*row2)[0] << endl; | 
|---|
| 135 | TString vals=Form("fCaCoFileAvail=Now(), fCaCoFileFound=%s", (*row2)[0]); | 
|---|
| 136 | TString where=Form("fRunNumber=%d", run); | 
|---|
| 137 |  | 
|---|
| 138 | //insert found runnumber | 
|---|
| 139 | if (!serv.Update("RunProcessStatus", vals,where)) | 
|---|
| 140 | return 0; | 
|---|
| 141 | counter2+=1; | 
|---|
| 142 | } | 
|---|
| 143 | else | 
|---|
| 144 | cout << " No caco file found for run " << run << endl; | 
|---|
| 145 |  | 
|---|
| 146 | delete res2; | 
|---|
| 147 | counter+=1; | 
|---|
| 148 | } | 
|---|
| 149 | cout << endl << counter << " missing caco files. " << endl; | 
|---|
| 150 | cout << counter2 << " caco files found and inserted. " << endl; | 
|---|
| 151 |  | 
|---|
| 152 | delete res; | 
|---|
| 153 | return 1; | 
|---|
| 154 | } | 
|---|
| 155 |  | 
|---|
| 156 |  | 
|---|