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

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