source: trunk/MagicSoft/Mars/macros/sql/filldotraw.C@ 5806

Last change on this file since 5806 was 4879, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 4.8 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): 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// filldotraw.C
29// ============
30//
31// This macro is used to read a merpped raw data file or a raw data file
32// directly. The descision is taken by the file-name extension (".root" or
33// ".raw")
34//
35// Usage:
36// .x filldotraw.C("/data/MAGIC/Period014/filename.raw", kTRUE)
37//
38// The second argument is the 'dummy-mode'. If it is kTRUE dummy-mode is
39// switched on and nothing will be written into the database. This is usefull
40// for tests.
41//
42// Filling the database is done with 'UPADTE' for _all_ columns
43// matching the Run-Number!
44//
45// The macro can also be run without ACLiC but this is a lot slower...
46//
47// Remark: Running it from the commandline looks like this:
48// root -q -l -b filldotraw.C+\(\"filename\"\,kFALSE\) 2>&1 | tee filldotraw.log
49//
50// Make sure, that database and password are corretly set in a resource
51// file called sql.rc and the resource file is found.
52//
53// Returns 0 in case of failure and 1 in case of success.
54//
55/////////////////////////////////////////////////////////////////////////////
56
57#include <fstream>
58#include <iostream>
59
60#include <TEnv.h>
61#include <TFile.h>
62#include <TTree.h>
63
64#include <TSQLRow.h>
65#include <TSQLResult.h>
66
67#include "MSQLServer.h"
68#include "MRawRunHeader.h"
69
70using namespace std;
71
72Int_t MagicNumber(MSQLServer &serv, const MRawRunHeader &h)
73{
74 TString query(Form("SELECT fMagicNumberKEY FROM MyMagic.MagicNumber WHERE fMagicNumber=%d",
75 h.GetMagicNumber()));
76
77 TSQLResult *res = serv.Query(query);
78 if (!res)
79 {
80 cout << "ERROR - Query failed: " << query << endl;
81 return -1;
82 }
83
84 TSQLRow *row = res->Next();
85 if (!row)
86 {
87 cout << "ERROR - No result from query: " << query << endl;
88 return -1;
89 }
90
91 return atoi((*row)[0]);
92}
93
94Bool_t ReadRaw(TString fname, MRawRunHeader &h)
95{
96 ifstream fin(fname);
97 if (!fin)
98 {
99 cout << "ERROR - Couldn't open file " << fname << endl;
100 return kFALSE;
101 }
102
103 if (!h.ReadEvt(fin))
104 {
105 cout << "ERROR - Reading header from file " << fname << endl;
106 return kFALSE;
107 }
108 return kTRUE;
109}
110
111Bool_t ReadRoot(TString fname, MRawRunHeader *h)
112{
113 TFile file(fname, "READ");
114 if (file.IsZombie())
115 {
116 cout << "ERROR - Cannot open file " << fname << endl;
117 return kFALSE;
118 }
119
120 TTree *t = (TTree*)file.Get("RunHeaders");
121 if (!t)
122 {
123 cout << "ERROR - Tree RunHeaders not found." << endl;
124 return kFALSE;
125 }
126
127 t->SetBranchAddress("MRawRunHeader.", &h);
128 t->GetEntry(0);
129
130 return kTRUE;
131}
132
133Bool_t Process(MSQLServer &serv, TString fname, Bool_t dummy)
134{
135 MRawRunHeader h;
136
137 if (fname.EndsWith(".root"))
138 ReadRoot(fname, &h);
139 if (fname.EndsWith(".raw"))
140 ReadRaw(fname, h);
141
142 if (dummy)
143 {
144 h.Print("header");
145 return kTRUE;
146 }
147
148 const Int_t key = MagicNumber(serv, h);
149 if (key<0)
150 return kFALSE;
151
152 TString query(Form("UPDATE MyMagic.RunData SET fMagicNumberKEY=%d, fFormatVersion=%d WHERE fRunNumber=%d",
153 key, h.GetFormatVersion(), h.GetRunNumber()));
154
155 TSQLResult *res = serv.Query(query);
156 if (!res)
157 {
158 cout << "ERROR - Query failed: " << query << endl;
159 return kFALSE;
160 }
161
162 return kTRUE;
163}
164
165void filldotraw(TString fname, Bool_t dummy=kTRUE)
166{
167 TEnv env("sql.rc");
168
169 MSQLServer serv(env);
170 if (!serv.IsConnected())
171 {
172 cout << "ERROR - Connection to database failed." << endl;
173 return;
174 }
175
176 cout << "filldotraw" << endl;
177 cout << "----------" << endl;
178 cout << endl;
179 cout << "Connected to " << serv.GetName() << endl;
180 cout << "File: " << fname << endl;
181 cout << endl;
182
183 cout << (Process(serv, fname, dummy) ? "Done." : "failed!") << endl << endl;
184}
Note: See TracBrowser for help on using the repository browser.