1 | using namespace std;
|
---|
2 |
|
---|
3 | //missing:
|
---|
4 | // remove sourcekey in paths
|
---|
5 | // create tables in DB
|
---|
6 |
|
---|
7 | int 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 | return 0;
|
---|
37 |
|
---|
38 | //allocate variables to use in the loop
|
---|
39 | TString night;
|
---|
40 | TString runid;
|
---|
41 | TString run;
|
---|
42 | TString year;
|
---|
43 | TString month;
|
---|
44 | TString day;
|
---|
45 | TString ganymed_fname;
|
---|
46 | TString star_fname;
|
---|
47 | TString where;
|
---|
48 | TString vars;
|
---|
49 |
|
---|
50 | //the variables to save into the db
|
---|
51 | Int_t fNumEvtsAfterCleaning = 0;
|
---|
52 | Int_t fNumEvtsAfterQualCuts = 0;
|
---|
53 | Int_t fNumEvtsAfterBgCuts = 0;
|
---|
54 | Float_t fNumBgEvts = 0;
|
---|
55 | Float_t fNumSigEvts = 0;
|
---|
56 | Float_t fNumExcEvts = 0;
|
---|
57 | Float_t fNumIslandsMean = 0;
|
---|
58 |
|
---|
59 | //loop over the data files
|
---|
60 | TSQLRow *row=0;
|
---|
61 | Int_t counter=0;
|
---|
62 |
|
---|
63 | /*
|
---|
64 | //cout the data names
|
---|
65 | cout<<"#evts_after_cleaning" << " "
|
---|
66 | <<"evts_after_qual-cuts" << " "
|
---|
67 | <<"evts_after_bg-cuts" << " "
|
---|
68 | <<"mean_evts_from_off_regions"<< " "
|
---|
69 | <<"evts_from_on_region" << " "
|
---|
70 | <<"calculated_excess_evts_in_file" << " " << endl;
|
---|
71 | */
|
---|
72 |
|
---|
73 | while ((row=res->Next()))
|
---|
74 | {
|
---|
75 | //use the results from the query
|
---|
76 | night=(*row)[4];
|
---|
77 | runid=(*row)[5];
|
---|
78 | run=(*row)[0];
|
---|
79 | year=(*row)[1];
|
---|
80 | month=(*row)[2];
|
---|
81 | day=(*row)[3];
|
---|
82 |
|
---|
83 | // to be fixed: path for nightly ganymeds
|
---|
84 | if (!pernight)
|
---|
85 | ganymed_fname=inpath+"/ganymed_run/"+year+"/"+month+"/"+day+"/"+run+"-analysis.root";
|
---|
86 | else
|
---|
87 | ganymed_fname=inpath+"/ganymed_night/"+night+"-analysis.root";
|
---|
88 | //cout << "gf => " << ganymed_fname << endl;
|
---|
89 |
|
---|
90 | if (!pernight)
|
---|
91 | {
|
---|
92 | star_fname=inpath+"/star/"+year+"/"+month+"/"+day+"/"+run+"_I.root";
|
---|
93 | //cout << "sf => " << star_fname << endl;
|
---|
94 |
|
---|
95 | //check star file status
|
---|
96 | if (gSystem->AccessPathName(star_fname))
|
---|
97 | {
|
---|
98 | cout << "ERROR - file " << star_fname << " does not exist." << endl;
|
---|
99 | continue;
|
---|
100 | }
|
---|
101 |
|
---|
102 | //try to open the star file
|
---|
103 | TFile star_file(star_fname, "READ");
|
---|
104 | if (!star_file.IsOpen())
|
---|
105 | {
|
---|
106 | cout << "ERROR - Could not open file " << star_fname << endl;
|
---|
107 | continue;
|
---|
108 | }
|
---|
109 |
|
---|
110 | //Get the events tree
|
---|
111 | TTree *star_tree = (TTree *)star_file.Get("Events");
|
---|
112 | if (!star_tree)
|
---|
113 | {
|
---|
114 | cout << "ERROR - Could not read tree " << star_fname << endl;
|
---|
115 | continue;
|
---|
116 | }
|
---|
117 |
|
---|
118 | //the number of events after cleaning
|
---|
119 | fNumEvtsAfterCleaning = star_tree->GetEntries();
|
---|
120 |
|
---|
121 | //get the mean number of islands in the _I.root file on runbasis
|
---|
122 | star_tree->Draw("MImagePar.fNumIslands>>IslandHisto","","");
|
---|
123 | TH1F *HistJo = (TH1F*)gDirectory->Get("IslandHisto");
|
---|
124 | fNumIslandsMean = HistJo->GetMean();
|
---|
125 | }
|
---|
126 |
|
---|
127 | //check ganymed file status
|
---|
128 | if (gSystem->AccessPathName(ganymed_fname))
|
---|
129 | {
|
---|
130 | cout << "ERROR - file " << ganymed_fname << " does not exist." << endl;
|
---|
131 | continue;
|
---|
132 | }
|
---|
133 |
|
---|
134 | //try to open the ganymed file
|
---|
135 | TFile ganymed_file(ganymed_fname, "READ");
|
---|
136 | if (!ganymed_file.IsOpen())
|
---|
137 | {
|
---|
138 | cout << "ERROR - Could not open file " << ganymed_fname << endl;
|
---|
139 | continue;
|
---|
140 | }
|
---|
141 |
|
---|
142 | //Get the status display contents
|
---|
143 | MStatusArray arr;
|
---|
144 | if (arr.Read()<=0)
|
---|
145 | {
|
---|
146 | cout << "ERROR - could not read MStatusArray, file " << ganymed_fname << endl;
|
---|
147 | continue;
|
---|
148 | }
|
---|
149 |
|
---|
150 | //Get number of events after quality cuts
|
---|
151 | //from the number of entries in a histogram
|
---|
152 | TH1F *precut =
|
---|
153 | (TH1F*)arr.FindCanvas("PreCut")->FindObject("SizeSame");
|
---|
154 | if (!precut)
|
---|
155 | {
|
---|
156 | cout << "WARNING - Reading of PreCut canvas failed." << endl;
|
---|
157 | continue;
|
---|
158 | }
|
---|
159 | fNumEvtsAfterQualCuts = precut->GetEntries();
|
---|
160 |
|
---|
161 | //Get number of events after background rejection cuts
|
---|
162 | //from the number of entries in a histogram
|
---|
163 | TH1F *postcut =
|
---|
164 | (TH1F*)arr.FindCanvas("PostCut")->FindObject("SizeSame");
|
---|
165 | if (!postcut)
|
---|
166 | {
|
---|
167 | cout << "WARNING - Reading of PostCut canvas failed." << endl;
|
---|
168 | continue;
|
---|
169 | }
|
---|
170 | fNumEvtsAfterBgCuts = postcut->GetEntries();
|
---|
171 |
|
---|
172 | //Get signal, bg and excess events
|
---|
173 | MHThetaSq *halphaon = (MHThetaSq*)arr.FindObjectInCanvas("MHThetaSq", "Hist");
|
---|
174 | if (!halphaon)
|
---|
175 | {
|
---|
176 | cout << "WARNING - Reading of MHThetaSq failed, file " << ganymed_fname << endl;
|
---|
177 | continue;
|
---|
178 | }
|
---|
179 | const MAlphaFitter &fit = halphaon->GetAlphaFitter();
|
---|
180 | if (!&fit)
|
---|
181 | {
|
---|
182 | cout << "WARNING - Reading of MAlphaFitter failed, file " << ganymed_fname << endl;
|
---|
183 | continue;
|
---|
184 | }
|
---|
185 |
|
---|
186 | //check if the excess events number is small and close to 0
|
---|
187 | //due to the computer precision error when subtracting sig and bg events
|
---|
188 | fNumBgEvts = fit.GetEventsBackground();
|
---|
189 | fNumSigEvts = fit.GetEventsSignal();
|
---|
190 | fNumExcEvts = fit.GetEventsExcess();
|
---|
191 | fNumExcEvts = fabs(fNumExcEvts)<1e-5 ? 0 : fNumExcEvts;
|
---|
192 |
|
---|
193 | /*
|
---|
194 | cout<< fNumEvtsAfterCleaning<< " "
|
---|
195 | << fNumEvtsAfterQualCuts<< " "
|
---|
196 | << fNumEvtsAfterBgCuts << " "
|
---|
197 | << fNumBgEvts << " "
|
---|
198 | << fNumSigEvts << " "
|
---|
199 | << fNumExcEvts << endl;
|
---|
200 | */
|
---|
201 |
|
---|
202 | //inserting or updating the information in the database
|
---|
203 | vars = Form("fRunID=%s, fNight=%s,", runid.Data(), night.Data());
|
---|
204 | vars += Form(" fNumEvtsAfterQualCuts=%d, "
|
---|
205 | " fNumEvtsAfterBgCuts=%d, "
|
---|
206 | " fNumBgEvts=%5.1f, "
|
---|
207 | " fNumSigEvts=%5.1f, "
|
---|
208 | " fNumExcEvts=%5.1f, "
|
---|
209 | " fNumIslandsMean=%5.4f ",
|
---|
210 | fNumEvtsAfterQualCuts,
|
---|
211 | fNumEvtsAfterBgCuts,
|
---|
212 | fNumBgEvts,
|
---|
213 | fNumSigEvts,
|
---|
214 | fNumExcEvts,
|
---|
215 | fNumIslandsMean
|
---|
216 | );
|
---|
217 | if (!pernight)
|
---|
218 | vars += Form(", fNumEvtsAfterCleaning=%d ", fNumEvtsAfterCleaning);
|
---|
219 |
|
---|
220 |
|
---|
221 | if (pernight)
|
---|
222 | where = Form("fNight=%s", night.Data());
|
---|
223 | else
|
---|
224 | where = Form("fRunID=%s AND fNight=%s", runid.Data(), night.Data());
|
---|
225 |
|
---|
226 | cout << "vars: " << vars << endl;
|
---|
227 | cout << "where: " << where << endl;
|
---|
228 |
|
---|
229 | if (!serv.InsertUpdate(table, vars, where))
|
---|
230 | {
|
---|
231 | cout << "ERROR - insert/update to DB failed (vars:" << vars << " where: " << where << ")." << endl;
|
---|
232 | return 2;
|
---|
233 | }
|
---|
234 |
|
---|
235 | }
|
---|
236 |
|
---|
237 |
|
---|
238 | return 0;
|
---|
239 | }
|
---|