source: trunk/Mars/fact/processing/numevents.C@ 16810

Last change on this file since 16810 was 16671, checked in by Daniela Dorner, 12 years ago
implemented fOnTimeAfterCuts, made macro compile,fixed some small bugs
  • Property svn:executable set to *
File size: 8.3 KB
Line 
1#include <iostream>
2
3#include <TCanvas.h>
4#include <TString.h>
5#include <TTree.h>
6#include <TFile.h>
7#include <TSystem.h>
8#include <TSQLResult.h>
9#include <TSQLRow.h>
10
11#include "MSQLMagic.h"
12#include "MAlphaFitter.h"
13#include "MHThetaSq.h"
14#include "MStatusArray.h"
15
16
17using namespace std;
18
19//missing:
20// remove sourcekey in paths
21// create tables in DB
22
23int numevents(TString night="20130418", TString inpath="/daq/analysis/1/", TString table= "AnalysisResultsRunLP", Bool_t dummy=kTRUE, Bool_t pernight=kFALSE)
24{
25
26 //connect to mysql server
27 //MSQLServer serv("sql.rc");
28 MSQLMagic serv("sql.rc");
29 if (!serv.IsConnected())
30 {
31 cout << "ERROR - Connection to database failed." << endl;
32 return 0;
33 }
34 serv.SetIsDummy(dummy);
35
36 //select runs, where star finished, format the list nicely
37 TString query;
38 query = " SELECT CONCAT(fNight, '_', LPAD(fRunID, 3, 0)), SUBSTRING(fNight, 1,4), SUBSTRING(fNight, 5,2), SUBSTRING(fNight, 7,2), ";
39 query += " fNight, fRunID FROM RunInfo";
40
41 //and only data runs
42 query+=" WHERE fRunTypeKEY=1 ";
43 query+=" AND fNight="+night;
44 //query+=" AND fRunID=128 ";
45 if (pernight)
46 query+=" GROUP BY fNight";
47
48 cout << "Q: " << query << endl;
49
50 //post the query
51 TSQLResult *res = serv.Query(query);
52 if (!res)
53 {
54 cout << "ERROR - no result from query " << query << endl;
55 return 2;
56 }
57
58 //allocate variables to use in the loop
59 TString night2;
60 TString runid;
61 TString run;
62 TString year;
63 TString month;
64 TString day;
65 TString ganymed_fname;
66 TString star_fname;
67 TString where;
68 TString vars;
69
70 //the variables to save into the db
71 Int_t fNumEvtsAfterCleaning = 0;
72 Int_t fNumEvtsAfterQualCuts = 0;
73 Int_t fNumEvtsAfterBgCuts = 0;
74 Float_t fNumBgEvts = 0;
75 Float_t fNumSigEvts = 0;
76 Float_t fNumExcEvts = 0;
77 Float_t fNumIslandsMean = 0;
78 Float_t fOnTimeAfterCuts = 0;
79
80 //loop over the data files
81 TSQLRow *row=0;
82
83 /*
84 //cout the data names
85 cout<<"#evts_after_cleaning" << " "
86 <<"evts_after_qual-cuts" << " "
87 <<"evts_after_bg-cuts" << " "
88 <<"mean_evts_from_off_regions"<< " "
89 <<"evts_from_on_region" << " "
90 <<"calculated_excess_evts_in_file" << " " << endl;
91 */
92
93 while ((row=res->Next()))
94 {
95 //use the results from the query
96 night2=(*row)[4];
97 runid=(*row)[5];
98 run=(*row)[0];
99 year=(*row)[1];
100 month=(*row)[2];
101 day=(*row)[3];
102
103 // to be fixed: path for nightly ganymeds
104 if (!pernight)
105 ganymed_fname=inpath+"/ganymed_run/"+year+"/"+month+"/"+day+"/"+run+"-analysis.root";
106 else
107 ganymed_fname=inpath+"/ganymed_night/"+night2+"-analysis.root";
108 //cout << "gf => " << ganymed_fname << endl;
109
110 if (!pernight)
111 {
112 star_fname=inpath+"/star/"+year+"/"+month+"/"+day+"/"+run+"_I.root";
113 //cout << "sf => " << star_fname << endl;
114
115 //check star file status
116 if (gSystem->AccessPathName(star_fname))
117 {
118 cout << "ERROR - file " << star_fname << " does not exist." << endl;
119 continue;
120 }
121
122 //try to open the star file
123 TFile star_file(star_fname, "READ");
124 if (!star_file.IsOpen())
125 {
126 cout << "ERROR - Could not open file " << star_fname << endl;
127 continue;
128 }
129
130 //Get the events tree
131 TTree *star_tree = (TTree *)star_file.Get("Events");
132 if (!star_tree)
133 {
134 cout << "ERROR - Could not read tree " << star_fname << endl;
135 continue;
136 }
137
138 //the number of events after cleaning
139 fNumEvtsAfterCleaning = star_tree->GetEntries();
140
141 //get the mean number of islands in the _I.root file on runbasis
142 star_tree->Draw("MImagePar.fNumIslands>>IslandHisto","","");
143 TH1F *HistJo = (TH1F*)gDirectory->Get("IslandHisto");
144 fNumIslandsMean = HistJo->GetMean();
145 }
146
147 //check ganymed file status
148 if (gSystem->AccessPathName(ganymed_fname))
149 {
150 cout << "ERROR - file " << ganymed_fname << " does not exist." << endl;
151 continue;
152 }
153
154 //try to open the ganymed file
155 TFile ganymed_file(ganymed_fname, "READ");
156 if (!ganymed_file.IsOpen())
157 {
158 cout << "ERROR - Could not open file " << ganymed_fname << endl;
159 continue;
160 }
161
162 //Get the status display contents
163 MStatusArray arr;
164 if (arr.Read()<=0)
165 {
166 cout << "ERROR - could not read MStatusArray, file " << ganymed_fname << endl;
167 continue;
168 }
169
170 TH1D *vstime = (TH1D*)arr.FindObjectInCanvas("OnTime", "TH1D", "OnTime");
171 if (!vstime)
172 {
173 cout << "WARNING - Reading of Theta failed." << endl;
174 continue;
175 }
176 fOnTimeAfterCuts = vstime->Integral();
177
178 //Get number of events after quality cuts
179 //from the number of entries in a histogram
180 TH1F *precut =
181 (TH1F*)arr.FindCanvas("PreCut")->FindObject("SizeSame");
182 if (!precut)
183 {
184 cout << "WARNING - Reading of PreCut canvas failed." << endl;
185 continue;
186 }
187 fNumEvtsAfterQualCuts = precut->GetEntries();
188
189 //Get number of events after background rejection cuts
190 //from the number of entries in a histogram
191 TH1F *postcut =
192 (TH1F*)arr.FindCanvas("PostCut")->FindObject("SizeSame");
193 if (!postcut)
194 {
195 cout << "WARNING - Reading of PostCut canvas failed." << endl;
196 continue;
197 }
198 fNumEvtsAfterBgCuts = postcut->GetEntries();
199
200 //Get signal, bg and excess events
201 MHThetaSq *halphaon = (MHThetaSq*)arr.FindObjectInCanvas("MHThetaSq", "Hist");
202 if (!halphaon)
203 {
204 cout << "WARNING - Reading of MHThetaSq failed, file " << ganymed_fname << endl;
205 continue;
206 }
207 const MAlphaFitter &fit = halphaon->GetAlphaFitter();
208 if (!&fit)
209 {
210 cout << "WARNING - Reading of MAlphaFitter failed, file " << ganymed_fname << endl;
211 continue;
212 }
213
214 //check if the excess events number is small and close to 0
215 //due to the computer precision error when subtracting sig and bg events
216 fNumBgEvts = fit.GetEventsBackground();
217 fNumSigEvts = fit.GetEventsSignal();
218 fNumExcEvts = fit.GetEventsExcess();
219 fNumExcEvts = fabs(fNumExcEvts)<1e-5 ? 0 : fNumExcEvts;
220
221 /*
222 cout<< fNumEvtsAfterCleaning<< " "
223 << fNumEvtsAfterQualCuts<< " "
224 << fNumEvtsAfterBgCuts << " "
225 << fNumBgEvts << " "
226 << fNumSigEvts << " "
227 << fNumExcEvts << endl;
228 */
229
230 //inserting or updating the information in the database
231 vars = Form("fRunID=%s, fNight=%s,", runid.Data(), night2.Data());
232 vars += Form(" fNumEvtsAfterQualCuts=%d, "
233 " fNumEvtsAfterBgCuts=%d, "
234 " fNumBgEvts=%6.1f, "
235 " fNumSigEvts=%6.1f, "
236 " fNumExcEvts=%6.1f, "
237 " fOnTimeAfterCuts=%7.2f ",
238 fNumEvtsAfterQualCuts,
239 fNumEvtsAfterBgCuts,
240 fNumBgEvts,
241 fNumSigEvts,
242 fNumExcEvts,
243 fOnTimeAfterCuts
244 );
245 if (!pernight)
246 vars += Form(", fNumIslandsMean=%6.2f, fNumEvtsAfterCleaning=%d ",
247 fNumIslandsMean,
248 fNumEvtsAfterCleaning
249 );
250
251
252 if (pernight)
253 where = Form("fNight=%s", night2.Data());
254 else
255 where = Form("fRunID=%s AND fNight=%s", runid.Data(), night2.Data());
256
257 cout << "vars: " << vars << endl;
258 cout << "where: " << where << endl;
259
260 if (!serv.InsertUpdate(table, vars, where))
261 {
262 cout << "ERROR - insert/update to DB failed (vars:" << vars << " where: " << where << ")." << endl;
263 return 2;
264 }
265
266 }
267
268
269 return 1;
270}
Note: See TracBrowser for help on using the repository browser.