DriftCache.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 /////////////////////////////////////////////////////////////////////////
7 
9 
10 #ifdef NOVACMAKE
11 #include "Database/cxx/include/Table.h"
12 #else
13 #include "Database/Table.h"
14 #endif
15 
16 #include <cassert>
17 #include "cetlib_except/exception.h"
18 
19 namespace calib
20 {
21  //......................................................................
23  : fTableLoaded(false),
24  fUseEpochs(false),
25  fDet(0),
26  fFactorIdx(-1),
27  fRun(0),
28  fCSVFile(""),
29  fTag(""),
30  fEpochTag(""),
31  fEpoch(""),
32  fTbl(0)
33  {
34  }
35 
36  //......................................................................
38  {
39  if (fTbl) delete fTbl;
40  }
41 
42  //......................................................................
43  double DriftCache::GetFactor(int plane, int cell)
44  {
45  if (!fTableLoaded) Load();
46 
47  geo::OfflineChan chan(plane,cell);
48  int tchan = 12*plane + int(cell/32) + 1;
49 
50  //**********
51  nova::dbi::Row* row = fTbl->GetVldRow(tchan, fRun);
52  if (!row) {
53  std::cout << "DriftCache no factor found for channel " << tchan << std::endl;
54  std::abort();
55  }
56  double factor;
57  if(!row->Col(fFactorIdx).Get(factor)) std::abort();
58  return factor;
59  }
60 
61  //......................................................................
62  void DriftCache::SetRun(int det, int run)
63  {
64  fDet = det;
65 
66  fRun =run;
67 
68  if (fTbl) delete fTbl;
69 
70  fTbl = new nova::dbi::Table("Calibration/tables/DriftCorrVld.xml");
72  fTbl->SetVerbosity(100);
76  fTbl->SetTag(fTag);
77  fFactorIdx = fTbl->GetColIndex("corr");
78  fTableLoaded = false;
79 
80  }
81 
82  //......................................................................
84  {
85  // first get epoch
86  if (fUseEpochs) {
87  nova::dbi::Table tbl("Calibration/tables/CalibDriftEpochs.xml");
89  tbl.SetTag(fEpochTag);
90  tbl.SetDetector(fDet);
91  tbl.SetMinTSVld(fRun);
92  tbl.SetMaxTSVld(fRun);
93  tbl.SetVerbosity(100);
94  assert(tbl.Load());
95  assert(tbl.NRow() == 1);
96 
97  int epochIdx = tbl.GetColIndex("epoch");
98  tbl.GetRow(0)->Col(epochIdx).Get(fEpoch);
99  }
100  }
101 
102  //......................................................................
104  {
105 
106  // if configured to provide own file
107  if(!fCSVFile.empty()){
108  return fCSVFile.c_str();
109  }
110 
111  // necessary components of path
112  const char* ups_path = getenv("CALIBCSVS_CSV_PATH");
113  assert(ups_path);
114  const char* detStr = "fd";
115 
116  if (fUseEpochs) {
117  GetEpochFromDB();
118  assert(!fEpoch.empty());
119  }
120 
121  std::string fullPath = std::string(ups_path) + "/" + fTag + "/calib_drift." + detStr + "." + fTag + "." + fEpoch + ".csv";
122  // Return final path
123 
124  return fullPath;
125  }
126 
127  //......................................................................
129  {
130  if (fTableLoaded) return;
131 
132  mf::LogInfo("DriftCache")
133  << "Initializing drift cache ...\n";
134 
135  const std::string constsCSVSource = GetCSVPath();
136 
137  GetEpochFromDB(); // will access dB if configured to do so
138 
139  if (fTbl->LoadFromCSV(constsCSVSource) <= 0){
140  std::cout<< "DriftCache Load failed to read from CSV file " <<std::endl;
141  std::abort();
142  }
143  if (fTbl->NRow() == 0){
144  std::cout<< "DriftCache Load found 0 rows in CSV file " <<std::endl;
145  std::abort();
146  }
147 
148  fTableLoaded = true;
149  fTbl->FillChanRowMap();
150  }
151 } // end namespace calib
152 ////////////////////////////////////////////////////////////////////////
void SetTag(std::string s)
Definition: Table.h:290
int LoadFromCSV(std::string fname, int offset=0, int limit=-1)
Definition: Table.cpp:1703
Column & Col(int i)
Find index of column with name. Suitable for passing to Col.
Definition: Row.h:63
void SetRun(int detector, int run)
Definition: DriftCache.cxx:62
Row *const GetRow(int i)
Definition: Table.cpp:492
void FillChanRowMap()
Definition: Table.cpp:2355
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
nova::dbi::Table * fTbl
Definition: DriftCache.h:68
bool Get(T &val) const
Definition: Column.h:85
std::string GetCSVPath()
Definition: DriftCache.cxx:103
CDPStorage service.
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)
std::string fEpoch
Definition: DriftCache.h:66
std::string fTag
Definition: DriftCache.h:64
bool SetDetector(std::string det)
Definition: Table.cpp:839
std::string fEpochTag
Definition: DriftCache.h:65
Definition: run.py:1
OStream cout
Definition: OStream.cxx:6
nova::dbi::Row * GetVldRow(unsigned long long channel, time_t t)
Definition: Table.cpp:2399
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
std::string fCSVFile
Definition: DriftCache.h:62
struct Table Table
Definition: TexBuilder.h:2
A (plane, cell) pair.
Definition: OfflineChan.h:17
void SetMaxTSVld(time_t t)
Definition: Table.h:285
assert(nhit_max >=nhit_nbins)
virtual ~DriftCache()
Definition: DriftCache.cxx:37
void SetVerbosity(int i)
Definition: Table.h:98
double GetFactor(int plane, int cell)
Definition: DriftCache.cxx:43
int NRow()
Definition: Table.h:101