TimingCache.cxx
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////
2 /// \brief Hold drift constants from current run
3 /// \author bckhouse@caltech.edu
4 /// \date
5 /////////////////////////////////////////////////////////////////////////
8 
10 
11 #ifdef NOVACMAKE
12 #include "Database/cxx/include/Table.h"
13 #else
14 #include "Database/Table.h"
15 #endif
16 
17 #include <cassert>
18 
19 #include "TString.h"
20 
21 namespace calib
22 {
23  //......................................................................
25  : fInitializedMC(false), fInitializedData(false),
26  fUseEpochs(false),
27  fRun(0),
28  fCurDet(novadaq::cnv::kUNKNOWN_DET),
29  fMinTime(std::numeric_limits<uint32_t>::max()-1),
30  fMaxTime(std::numeric_limits<uint32_t>::min())
31  {
32  }
33 
34  //......................................................................
36  {
37  }
38 
39  //......................................................................
40  double TimingCache::GetOffset(int dcm, int diblock, std::uint32_t time, bool isData)
41  {
42 
43  if(!fInitializedData&&isData){
44  LoadFromDatabase(isData);
45  }
46  if(!fInitializedMC&&!isData){
47  LoadFromDatabase(isData);
48  }
49 
50  int key = (diblock*1000)+dcm;
51 
52  std::map<int, DelayRec>::iterator it;
53 
54  if(isData){
55  it = fMapData.find(key);
56  if(it == fMapData.end()){
57  mf::LogDebug("TimingCache") << "No timing offset for dcm "<<dcm;
58  std::abort();
59  }
60  }
61  else{
62  it = fMapMC.find(key);
63  if(it == fMapMC.end()){
64  mf::LogDebug("TimingCache") << "No timing offset for dcm "<<dcm;
65  std::abort();
66  }
67  }
68 
69  const DelayRec& rec = it->second;
70  return rec.dcmoffset;
71  }
72 
73  //......................................................................
74  double TimingCache::GetSigma(int dcm, int diblock, std::uint32_t time, bool isData)
75  {
76 
77  if(!fInitializedData&&isData){
78  LoadFromDatabase(isData);
79  }
80 
81  if(!fInitializedMC&&!isData){
82  LoadFromDatabase(isData);
83  }
84 
85  std::map<int, DelayRec>::iterator it;
86  int key = (diblock*1000)+dcm;
87 
88  if(isData){
89  it = fMapData.find(key);
90  if(it == fMapData.end()){
91  mf::LogDebug("TimingCache") << "No timing offset sigma for dcm "<<dcm;
92  std::abort();
93  }
94  }
95  else{
96  it = fMapMC.find(key);
97  if(it == fMapMC.end()){
98  mf::LogDebug("TimingCache") << "No timing offset sigma for dcm "<<dcm;
99  std::abort();
100  }
101  }
102 
103  const DelayRec& rec = it->second;
104  return rec.sigma;
105  }
106 
107  //......................................................................
108  void TimingCache::SetVldTime(novadaq::cnv::DetId det, std::uint32_t minTime, std::uint32_t maxTime)
109  {
110  if(det != fCurDet || minTime != fMinTime || maxTime != fMaxTime){
111  fInitializedData = false;
112  fInitializedMC = false;
113  fMapMC.clear();
114  fMapData.clear();
115 
116  fCurDet = det;
117  fMinTime= minTime;
118  fMaxTime = maxTime;
119  }
120  }
121 
122  //......................................................................
123  void TimingCache::GetEpochFromDB(int idet, bool isData)
124  {
125  nova::dbi::Table tbl("Calibration/tables/TimingEpochs.xml");
126  if (isData)
128  else
130  tbl.SetTag(fEpochTag);
131  tbl.SetDetector(idet);
132  tbl.SetMinTSVld(fRun);
133  tbl.SetMaxTSVld(fRun);
134  assert(tbl.Load());
135  assert(tbl.NRow() == 1);
136 
137  int epochIdx = tbl.GetColIndex("epoch");
138  tbl.GetRow(0)->Col(epochIdx).Get(fEpoch);
139 
140  }
141 
142  //......................................................................
144  {
145  // Even if it goes wrong there's no point trying again
146  if(isData){
147  fInitializedData = true;
148  }
149  else{
150  fInitializedMC = true;
151  }
152 
153  //if this is MC, move the mintime away from 0
154  if (!isData && fMinTime == 0){
155  fMinTime = 1;
156  fMaxTime = 2;
157  }
158  mf::LogInfo("TimingCache")
159  << "Initializing timing delay cache for "
160  << novadaq::cnv::DetInfo::GetName(fCurDet) << " from database...\n";
161 
162 
163  nova::dbi::Table tblConsts("Calibration/tables/CalibTimingConsts.xml");
164  tblConsts.SetDetector(fCurDet);
166  if(!fTag.empty()) tblConsts.SetTag(fTag);
167  tblConsts.SetVerbosity(100);
168 
169  tblConsts.SetMinTSVld(fMinTime);
170  tblConsts.SetMaxTSVld(fMaxTime);
171 
172  bool ok = true;
173 
174  if(fUseCSVsFromUPS || !fCSVSource.empty()){
175  std::string csvSource = fCSVSource;
176  if(fUseCSVsFromUPS){
177  const char* ups_path = getenv("CALIBCSVS_CSV_PATH");
178  assert(ups_path);
179  //RJN fix dteStr
180  const char* detStr = getDetString(fCurDet);
181  const char* mcStr = isData ? "data" : "mc";
182 
183 
184  if (fReadEpochsFromCSV){
185  //Wil now attempt to read the dcm_offsets filename
186  std::string prePattern=TString::Format("%s/%s/calib_dcm_delay_consts.%s.%s.%s", ups_path, fTag.c_str(),
187  detStr, mcStr, fTag.c_str()).Data();
188  std::string postPattern=TString::Format(".csv").Data();
189  csvSource=getCSVFilenameByParsingDirectory(fRun,prePattern,postPattern);
190  }
191  else if (fUseEpochs) {
192  GetEpochFromDB(fCurDet,isData);
193  assert(!fEpoch.empty());
194  csvSource = TString::Format("%s/%s/calib_dcm_delay_consts.%s.%s.%s.%s.csv",
195  ups_path, fTag.c_str(),
196  detStr, mcStr, fTag.c_str(),
197  fEpoch.c_str()).Data();
198  }
199  else {
200  csvSource = TString::Format("%s/%s/calib_dcm_delay_consts.%s.%s.%s.csv",
201  ups_path, fTag.c_str(),
202  detStr, mcStr, fTag.c_str()).Data();
203  }
204  }
205 
206  mf::LogInfo("TimingCache") << "Loading timing cache from "
207  << csvSource << std::endl;
208  ok = ok && (tblConsts.LoadFromCSV(csvSource) > 0);
209  }
210  else{
211  ok = ok && tblConsts.Load();
212  }
213 
214 
215  if(!ok){
216  // No safe way to handle this
217  mf::LogError("TimingCache") << "Unable to load timing delay tables. No timing delays will be set."
218  << std::endl;
219  std::abort();
220  }
221  else{
222  const int dcmoffsetCol = tblConsts.GetColIndex("dcmoffset");
223  const int sigmaCol = tblConsts.GetColIndex("sigma");
224  const int statsCol = tblConsts.GetColIndex("stats");
225 
226  const int Nconsts = tblConsts.NRow();
227  for(int nconsts = 0; nconsts < Nconsts; ++nconsts){
228  nova::dbi::Row* row = tblConsts.GetRow(nconsts);
229 
230  const int dcmkey = row->Channel();
231 
232  DelayRec rec;
233 
234  ok = ok && row->Col(dcmoffsetCol).Get(rec.dcmoffset);
235  ok = ok && row->Col(sigmaCol).Get(rec.sigma);
236  ok = ok && row->Col(statsCol).Get(rec.stats);
237 
238  //For data, abort if the value is bad,
239  if(isData && !ok) std::abort();
240 
241  if (ok&&isData) fMapData[dcmkey] = rec;
242  if (ok&&!isData) fMapMC[dcmkey] = rec;
243  } // end for nconsts
244  }
245 
246  }
247 
248 } // end namespace calib
249 ////////////////////////////////////////////////////////////////////////
void GetEpochFromDB(int idet, bool isData)
void SetTag(std::string s)
Definition: Table.h:290
static std::string GetName(int id)
int LoadFromCSV(std::string fname, int offset=0, int limit=-1)
Definition: Table.cpp:1703
void LoadFromDatabase(bool isData)
diblock
print "ROW IS " print row
Definition: geo2elec.py:31
Column & Col(int i)
Find index of column with name. Suitable for passing to Col.
Definition: Row.h:63
novadaq::cnv::DetId fCurDet
Definition: TimingCache.h:67
set< int >::iterator it
std::map< int, DelayRec > fMapData
Definition: TimingCache.h:70
void SetVldTime(novadaq::cnv::DetId det, std::uint32_t minTime, std::uint32_t maxTime)
Row *const GetRow(int i)
Definition: Table.cpp:492
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
caf::StandardRecord * rec
Definition: tutCAFMacro.C:20
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
bool Get(T &val) const
Definition: Column.h:85
std::string fCSVSource
Definition: TimingCache.h:57
virtual ~TimingCache()
Definition: TimingCache.cxx:35
CDPStorage service.
std::string getCSVFilenameByParsingDirectory(int fCurrentRun, std::string prePattern, std::string postPattern)
Definition: CalibUtil.cxx:706
int GetColIndex(std::string cname)
Definition: Table.cpp:952
void SetDataTypeMask(int mask)
Definition: Table.h:74
void SetMinTSVld(time_t t)
Definition: Table.h:284
std::string getenv(std::string const &name)
bool SetDetector(std::string det)
Definition: Table.cpp:839
std::string fEpochTag
Definition: TimingCache.h:62
std::string fTag
Definition: TimingCache.h:59
static float min(const float a, const float b, const float c)
Definition: absgeo.cxx:45
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
const char * getDetString(novadaq::cnv::DetId det)
Utility function to get detector name as a string for file manipulations.
Definition: CalibUtil.cxx:29
MaybeLogger_< ELseverityLevel::ELsev_success, false > LogDebug
void SetMaxTSVld(time_t t)
Definition: Table.h:285
std::uint32_t fMinTime
Definition: TimingCache.h:68
unsigned long long Channel()
Definition: Row.h:68
std::string fEpoch
Definition: TimingCache.h:61
assert(nhit_max >=nhit_nbins)
std::map< int, DelayRec > fMapMC
Definition: TimingCache.h:71
double GetOffset(int dcm, int diblock, std::uint32_t time, bool isData)
Definition: TimingCache.cxx:40
void Format(TGraph *gr, int lcol, int lsty, int lwid, int mcol, int msty, double msiz)
Definition: Style.cxx:154
std::uint32_t fMaxTime
Definition: TimingCache.h:68
void SetVerbosity(int i)
Definition: Table.h:98
T max(sqlite3 *const db, std::string const &table_name, std::string const &column_name)
Definition: statistics.h:68
int NRow()
Definition: Table.h:101
double GetSigma(int dcm, int diblock, std::uint32_t time, bool isData)
Definition: TimingCache.cxx:74