EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FairDbValidityRecBuilder.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file FairDbValidityRecBuilder.cxx
1 
2 #include "Detector.h"
3 #include "SimFlag.h"
4 #include "FairDbProxy.h"
5 #include "FairDbResult.h"
7 #include "FairDbValidityRec.h"
9 #include "ValContext.h"
10 
12 
14  const ValContext& vc,
15  const FairDb::Task& task,
16  Int_t selectDbNo /* Default: -1 */,
17  Bool_t findFullTimeWindow /* Default: true*/
18  )
19  : fGap(),
20  fIsExtendedContext(kFALSE),
21  fTask(task),
22  fVRecs(),
23  fAggNoToIndex()
24 {
25 
26  Detector::Detector_t det(vc.GetDetector());
27  SimFlag::SimFlag_t sim(vc.GetSimFlag());
28  ValTimeStamp ts(vc.GetTimeStamp());
29  SimFlag::SimFlag_t simTry(sim);
30  const string& tableName = proxy.GetTableName();
31  Int_t sumTimeWindows = 0;
32  Int_t numTimeWindows = 0;
33 
34 
35  this->MakeGapRec(vc, tableName, findFullTimeWindow);
36 
37 // Force aggregate -1 into first slot.
38  this->AddNewGap(-1);
39 
40  const ValTimeStamp curVTS = vc.GetTimeStamp();
41 
42 // Check to see if table exists.
43 
44  unsigned int numVRecIn = 0;
45 
46  if ( ! proxy.TableExists() ) {
47 
48  cout << "FairDbValidityRecBuilder::Query for table:"
49  << proxy.GetTableName()
50  << ", table does not exist!" << endl;
51  }
52 
53  else {
54 
55 // Loop over all databases in cascade until valid data found.
56 
57  UInt_t numDb = proxy.GetNumDb();
58  Bool_t foundData = kFALSE;
59 
60  for ( UInt_t dbNo = 0; dbNo < numDb && ! foundData; ++dbNo ) {
61 
62  // Skip if cascade entry does not have this table or selection in force and not the required one.
63  if ( ! proxy.TableExists(dbNo) ) { continue; }
64  if ( selectDbNo >= 0 && selectDbNo != (int) dbNo ) { continue; }
65 
66 // Loop over all associated SimFlags.
67 
70 
71  FairDbSimFlagAssociation::SimList_t::iterator listItr = simList.begin();
72  FairDbSimFlagAssociation::SimList_t::iterator listItrEnd = simList.end();
73  while ( listItr != listItrEnd && ! foundData ) {
74 
75  simTry = *listItr;
76  ++listItr;
77  ValContext vcTry(det,simTry,ts);
78 
79 // Apply validity query and build result set.
80 
81  FairDbResultSet* rs = proxy.QueryValidity(vcTry,fTask,dbNo);
82 
83 // Build a result from the result set and drop result set.
84 
86  FairDbResultNonAgg result(rs,&tr,0,kFALSE);
87  delete rs;
88 
89 // Loop over all entries in FairDbResult and, for each Aggregate,
90 // find effective validity range of best, or of gap if none.
91 
92 // Set earliest creation date to infinite past - the right value if in a gap.
93  ValTimeStamp earliestCreate(0);
94  UInt_t numRows = result.GetNumRows();
95  for (UInt_t row = 0; row < numRows; ++row) {
96  const FairDbValidityRec* vr = dynamic_cast<const FairDbValidityRec*>(
97  result.GetTableRow(row));
98 
99  Int_t aggNo = vr->GetAggregateNo();
100 
101 // If starting a new aggregate prime it as a gap.
102  Int_t index = this->IndexOfAggno(aggNo);
103  if ( index < 0 ) { index = this->AddNewGap(aggNo); }
104 
105 // Trim the validity record for the current aggregate
106 // number by this record and see if we have found valid
107 // data yet.
108 
109  FairDbValidityRec& curRec = fVRecs[index];
110  curRec.Trim(curVTS, *vr);
111  if ( ! curRec.IsGap() ) {
112  foundData = kTRUE;
113 
114 // Fill in entry's database number - its not stored in the
115 // database table!
116  curRec.SetDbNo(dbNo);
117  }
118 
119 // Find the earliest non-gap creation date that is used
120  if ( curRec.GetSeqNo() == vr->GetSeqNo()
121  && ( earliestCreate > vr->GetCreationDate()
122  || earliestCreate.GetSec() == 0 )
123  ) { earliestCreate = vr->GetCreationDate(); }
124 
125 // Count the number used and sum the time windows
126  ++numVRecIn;
127  const ValRange range = vr->GetValRange();
128  Int_t timeInterval = range.GetTimeEnd().GetSec()
129  - range.GetTimeStart().GetSec();
130  if ( timeInterval < 5 ) {
131 
132  cout << "Detected suspiciously small validity time interval in \n"
133  << "table " << tableName << " validity rec " << *vr << endl;
134  }
135  sumTimeWindows += timeInterval;
136  ++numTimeWindows;
137  }
138 
139 // If finding findFullTimeWindow then find bounding limits
140 // for the cascade and sim flag and trim all validity records
141 // and the default (gap) validity record.
142  if ( findFullTimeWindow ) {
143  ValTimeStamp start, end;
144  proxy.FindTimeBoundaries(vcTry,fTask,dbNo,earliestCreate,start,end);
145  cout << "Trimming validity records to "
146  << start << " .. " << end << endl;
147  std::vector<FairDbValidityRec>::iterator itr(fVRecs.begin()), itrEnd(fVRecs.end());
148  for( ; itr != itrEnd; ++itr ) { itr->AndTimeWindow(start,end); }
149  fGap.AndTimeWindow(start,end);
150  }
151  }
152 
153  }
154  }
155 
156 // If the query found no records in any database then
157 // the tables will still have something - the aggno = -1 gap
158 
159 // If the associated SimFlag is different to the original request make
160 // sure that all the FairDbValidityRec are valid for the request.
161  if ( sim != simTry ) {
162  cout << "Imposing SimFlag of " << sim << " on FairDbValidityRecs which matched " << simTry << endl;
163  for ( unsigned int irec = 0; irec < GetNumValidityRec(); ++irec ) {
164  FairDbValidityRec& vrec = const_cast<FairDbValidityRec&>(GetValidityRec(irec));
165  const ValRange& vr(vrec.GetValRange());
166  ValRange vr_mod(vr.GetDetectorMask(),sim,vr.GetTimeStart(),vr.GetTimeEnd(),vr.GetDataSource());
167  vrec.SetValRange(vr_mod);
168  }
169  }
170 
171  cout << "FairDbValidityRecBuilder:" << endl
172  << " Query: " << vc.AsString() << endl
173  << " using associated SimFlag: " << SimFlag::AsString(simTry)
174  << " for " << SimFlag::AsString(sim)
175  << " found " << numVRecIn
176  << " vrecs in database, for " << fVRecs.size()
177  << " aggregates:-." << endl;
178 
179  for ( unsigned int irec = 0; irec < GetNumValidityRec(); ++irec ) {
180  const FairDbValidityRec& vrec = GetValidityRec(irec);
181  if ( vrec.GetAggregateNo() != -2 ) {
182  cout << " " << irec << " " << GetValidityRec(irec) << endl;
183  }
184  }
185 
186 
187 
188 // Adjust the time gate if grossly wrong.
189  if ( numTimeWindows > 0 ) {
190  Int_t timeGateCalc = 3 * fVRecs.size() * sumTimeWindows/numTimeWindows;
191  // Limit to 100 days and allow for overflow.
192  if ( timeGateCalc > 100*24*60*60 || timeGateCalc < 0
193  ) { timeGateCalc = 100*24*60*60; }
194  Int_t timeGateCurr = FairDb::GetTimeGate(tableName);
195  if ( timeGateCurr < timeGateCalc/10
196  || timeGateCurr > timeGateCalc*10
197  ) {
198  FairDb::SetTimeGate(tableName,timeGateCalc);
199  if ( timeGateCalc != FairDb::GetTimeGate(tableName) ) {
200 
201  cout << "The ignored time gate setting was calculated with the following data:-"
202  << "\n Context: " << vc << " task " << task << " findFullTimeWindow " << findFullTimeWindow
203  << "\n Number of vrecs " << numTimeWindows
204  << " total time (secs) of all vrecs " << sumTimeWindows
205  << " Number of aggregates " << fVRecs.size() << endl;
206  }
207  }
208  }
209 
210  return;
211 
212 }
213 
215  const string& context,
216  const FairDb::Task& task)
217  : fGap(),
218  fIsExtendedContext(kTRUE),
219  fTask(task),
220  fVRecs(),
221  fAggNoToIndex()
222 {
223 
224  cout << "Creating FairDbValidityRecBuilder"
225  << " for extended context " << context << endl;
226 
227 // Prime fVRecs with a gap that will not be used but makes the result
228 // look like an aggregated result.
229 
230  cout<< "Initialising with gap record " << fGap << endl;
231  this->AddNewGap(-1);
232 
233 // Check to see if table exists.
234 
235  unsigned int numVRecIn = 0;
236 
237  if ( ! proxy.TableExists() ) {
238  cout << "FairDbValidityRecBuilder::Query for table:"
239  << proxy.GetTableName()
240  << ", table does not exist!" << endl;
241  }
242 
243  else if ( context == "" ) {
244  cout << "FairDbValidityRecBuilder::Null query for table:"
245  << proxy.GetTableName() << endl;
246  }
247 
248  else {
249 
250 // Loop over all databases in cascade until valid data found.
251 
252  UInt_t numDb = proxy.GetNumDb();
253  Bool_t foundData = kFALSE;
254 
255  for ( UInt_t dbNo = 0; dbNo < numDb && ! foundData; ++dbNo ) {
256 
257 // Apply validity query and build result set.
258 
259  FairDbResultSet* rs = proxy.QueryValidity(context,fTask,dbNo);
260 
261 // Build a result from the result set and drop result set.
262 
264  FairDbResultNonAgg result(rs,&tr,0,kFALSE);
265  delete rs;
266 
267 // Loop over all entries in FairDbResult and add them all to set.
268 
269  UInt_t numRows = result.GetNumRows();
270  for (UInt_t row = 0; row < numRows; ++row) {
271  const FairDbValidityRec* vr = dynamic_cast<const FairDbValidityRec*>(
272  result.GetTableRow(row));
273 
274 // Cannot use AddNewAgg - aggregate numbers may be duplicated.
275  Int_t index = fVRecs.size();
276  fVRecs.push_back(FairDbValidityRec(*vr));
277  fAggNoToIndex[vr->GetAggregateNo()] = index;
278  foundData = kTRUE;
279 
280 // Fill in entry's database number - its not stored in the
281 // database table!
282  fVRecs[index].SetDbNo(dbNo);
283 
284 // Count the number used.
285  ++numVRecIn;
286  }
287  }
288  }
289 
290  cout << "FairDbValidityRecBuilder:" << endl
291  << " Extended context query: " << context << endl
292  << " found " << numVRecIn
293  << " vrecs in database, for " << fVRecs.size()
294  << " records:-." << endl;
295 
296  for ( unsigned int irec = 0; irec < GetNumValidityRec(); ++irec ) {
297  cout << " " << irec << " " << GetValidityRec(irec) << endl;
298  }
299  return;
300 
301 
302 
303 }
304 
305 //.....................................................................
306 
308  const std::string tableName)
309  : fGap(),
310  fIsExtendedContext(kFALSE),
311  fTask(vr.GetTask()),
312  fVRecs(),
313  fAggNoToIndex()
314 {
315 
316  const ValRange& vrange(vr.GetValRange());
317  // This is the only way I can find to handle Detector and SimFlag!
318  ValContext vc( (Detector::Detector_t) vrange.GetDetectorMask(),
319  (SimFlag::SimFlag_t) vrange.GetSimMask(),
320  vrange.GetTimeStart());
321  this->MakeGapRec(vc,tableName);
322  this->AddNewAgg(vr,vr.GetAggregateNo());
323 
324 }
325 //.....................................................................
326 
328 {
329 
330 }
331 
332 
334 {
335  if ( this->IsExtendedContext() ) { return ""; }
336 
337  UInt_t seqLo = 0;
338  UInt_t seqHi = 0;
339  ValTimeStamp ts;
340  std::vector<FairDbValidityRec>::const_iterator itr = fVRecs.begin();
341  std::vector<FairDbValidityRec>::const_iterator end = fVRecs.end();
342 
343  for (; itr != end; ++itr) {
344  const FairDbValidityRec& vr = *itr;
345  if ( ! vr.IsGap() ) {
346  if ( seqLo == 0 ) {
347  seqLo = vr.GetSeqNo();
348  seqHi = vr.GetSeqNo();
349  ts = vr.GetCreationDate();
350  } else {
351  if ( seqLo < vr.GetSeqNo() ) { seqLo = vr.GetSeqNo(); }
352  if ( seqHi > vr.GetSeqNo() ) { seqHi = vr.GetSeqNo(); }
353  if ( ts < vr.GetCreationDate() ) { ts = vr.GetCreationDate(); }
354  }
355  }
356  }
357 
358  if ( seqLo == 0 ) { return ""; }
359 
360  return FairDbValidityRec::GetL2CacheName(seqLo,seqHi,ts);
361 
362 }
363 
364 //.....................................................................
365 
367 {
368 
369  // Make sure it really doesn't exist.
370  int index = this->IndexOfAggno(aggNo);
371  if ( index >=0 ) { return index; }
372 
373  // It doesn't so add it.
374  index = fVRecs.size();
375  fVRecs.push_back(vrec);
376  fAggNoToIndex[aggNo] = index;
377  return index;
378 
379 }
380 
381 //.....................................................................
382 
384 {
385 
386  UInt_t index = this->AddNewAgg(fGap,aggNo);
387  fVRecs[index].SetAggregateNo(aggNo);
388  return index;
389 
390 }
391 
392 //.....................................................................
393 
394 const FairDbValidityRec&
396 {
397 
398  return (rowNo < 0 || rowNo >= (int) fVRecs.size()) ? fGap : fVRecs[rowNo];
399 }
400 
401 //.....................................................................
402 
403 const FairDbValidityRec&
405 {
406 
407  std::vector<FairDbValidityRec>::const_iterator itr = fVRecs.begin();
408  std::vector<FairDbValidityRec>::const_iterator end = fVRecs.end();
409 
410  for (; itr != end; ++itr) if ( itr->GetSeqNo() == SeqNo ) { return *itr; }
411  return fGap;
412 }
413 
414 //.....................................................................
415 
417 {
418  std::map<Int_t,UInt_t>::const_iterator itr = fAggNoToIndex.find(aggNo);
419  if ( itr == fAggNoToIndex.end() ) { return -1; }
420  return itr->second;
421 
422 }
423 
424 //.....................................................................
425 
427  const string& tableName,
428  Bool_t findFullTimeWindow)
429 {
430 
431  Int_t timeGate = FairDb::GetTimeGate(tableName);
432  time_t contextSec = vc.GetTimeStamp().GetSec() - timeGate;
433  ValTimeStamp startGate(contextSec,0);
434  contextSec += 2*timeGate;
435  ValTimeStamp endGate(contextSec,0);
436  if ( findFullTimeWindow ) {
437  startGate = ValTimeStamp(0,0);
438  endGate = ValTimeStamp(0x7FFFFFFF,0);
439  }
440  ValRange gapVR(vc.GetDetector(), vc.GetSimFlag(), startGate, endGate, "Gap");
441  fGap = FairDbValidityRec(gapVR, fTask, -2, 0, 0, kTRUE);
442 
443 }
444