source: trunk/MagicSoft/Mars/datacenter/macros/findcacofiles.C@ 9410

Last change on this file since 9410 was 8996, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 5.1 KB
Line 
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 <TSystem.h>
60
61#include <TSQLRow.h>
62#include <TSQLResult.h>
63
64#include "MSQLMagic.h"
65
66using namespace std;
67
68
69int findcacofiles()
70{
71 MSQLMagic serv("sql.rc");
72 if (!serv.IsConnected())
73 {
74 cout << "ERROR - Connection to database failed." << endl;
75 return 0;
76 }
77 cout << "findcacofiles" << endl;
78 cout << "-------------" << endl;
79 cout << endl;
80 cout << "Connected to " << serv.GetName() << endl;
81 cout << endl;
82
83 //get runnumbers and dates from database
84 TString query="SELECT RunProcessStatus.fRunNumber, ";
85 query+=" DATE_FORMAT(ADDDATE(if(fRunStart='0000-00-00 00:00:00', fRunStop, fRunStart), INTERVAL +13 HOUR), '%Y/%m/%d') ";
86 query+=" FROM RunProcessStatus ";
87 query+=" LEFT JOIN RunData USING (fRunNumber) ";
88 query+=" LEFT JOIN Source USING (fSourceKEY) ";
89 query+=" WHERE IsNull(fCaCoFileFound) AND fExcludedFDAKEY=1 ";
90 query+=" AND RunProcessStatus.fRunNumber > 10000 AND NOT IsNull(fCCFileAvail)";
91 query+=" AND fTest='no'";
92
93 TSQLResult *res = serv.Query(query);
94 if (!res)
95 {
96 cout << "Error." << endl;
97 return 0;
98 }
99
100 Int_t counter=0;
101 Int_t counter2=0;
102 TSQLRow *row=0;
103 while ((row = res->Next()))
104 {
105 //search nearest previous available CaCoFile
106 Int_t run=atoi((*row)[0]);
107 if (TString((*row)[1]).IsNull())
108 {
109 cout << "For run " << (*row)[0] << " fRunStart and fRunStop are 0000-00-00 00:00:00. No CaCoFile can be determined. " << endl;
110 continue;
111 }
112 cout << "CaCoFile missing for run " << (*row)[0] << " with date " << (*row)[1] << endl;
113
114 query ="SELECT MAX(fCaCoFileFound) FROM RunProcessStatus ";
115 query+=" LEFT JOIN RunData USING (fRunNumber) ";
116 query+=Form("WHERE DATE_FORMAT(ADDDATE(fRunStart, INTERVAL +13 HOUR), '%%Y/%%m/%%d')='%s' ",
117 (*row)[1]);
118 query+=Form("AND RunData.fRunNumber IN (%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d)",
119 run, run-1, run-2, run-3, run-4, run-5,
120 run-6, run-7, run-8, run-9, run-10);
121
122 TSQLResult *res2 = serv.Query(query);
123 if (!res2)
124 {
125 cout << "Error." << endl;
126 return 0;
127 }
128 TSQLRow *row2=0;
129 row2 = res2->Next();
130 if ((*row2)[0])
131 {
132 cout << "Found CaCoFile at run " << (*row2)[0] << endl;
133 TString vals=Form("fCaCoFileAvail=Now(), fCaCoFileFound=%s", (*row2)[0]);
134 TString where=Form("fRunNumber=%d", run);
135
136 //insert found runnumber
137 if (!serv.Update("RunProcessStatus", vals,where))
138 return 0;
139 counter2+=1;
140 }
141 else
142 cout << " No caco file found for run " << run << endl;
143
144 delete res2;
145 counter+=1;
146 }
147 cout << endl << counter << " missing caco files. " << endl;
148 cout << counter2 << " caco files found and inserted. " << endl;
149
150 delete res;
151 return 1;
152}
153
154
Note: See TracBrowser for help on using the repository browser.