AbsCache.cxx
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 /// \brief Look up absolute attenuation constants
3 /// \author Christopher Backhouse - bckhouse@caltech.edu
4 ////////////////////////////////////////////////////////////////////////
5 
8 
9 #ifdef NOVACMAKE
10 #include "Database/cxx/include/Table.h"
11 #else
12 #include "Database/Table.h"
13 #endif
14 
16 
17 #include "TString.h"
18 
19 namespace calib
20 {
21  //......................................................................
23  : fGainSetting(0),
24  fInitializedData(false), fInitializedMC(false)
25  {
26  }
27 
28  //......................................................................
30  {
31  }
32 
33  //......................................................................
35  int diblock,
36  bool isData)
37  {
38  LoadFromVldDatabase(det, isData);
39 
40  const std::map<int, Record>& recs = isData ? fRecordsData : fRecordsMC;
41 
42  auto it = recs.find(diblock);
43  if(it == recs.end()) return -1;
44  const Record& rec = it->second;
45  return rec.meuGeV / rec.meu;
46  }
47 
48  //......................................................................
50  {
51  if(isData){
52  if(fInitializedData) return;
53  // Even if it goes wrong there's no point trying again
54  fInitializedData = true;
55  }
56  else{
57  if(fInitializedMC) return;
58  fInitializedMC = true;
59  }
60 
61 
62  mf::LogInfo("AbsCache")
63  << "Loading absolute energy constants for " << novadaq::cnv::DetInfo::GetName(det) << " from CSV... fReadEpochsFromCSV " << fReadEpochsFromCSV <<"\tisData\t" << isData << "\n" ;
64 
65  nova::dbi::Table tblConsts("Calibration/tables/CalibAbsConsts.xml");
66  tblConsts.SetDetector(det);
68  if(!fTag.empty()) tblConsts.SetTag(fTag);
69  tblConsts.SetVerbosity(100);
70 
71  tblConsts.SetMinTSVld(1);
72  tblConsts.SetMaxTSVld(100);
73 
74  bool ok = true;
75 
76  if(fUseCSVsFromUPS || !fCSVSource.empty()){
77  std::string csvSource = fCSVSource;
78  if(fUseCSVsFromUPS){
79  const char* ups_path = getenv("CALIBCSVS_CSV_PATH");
80  assert(ups_path);
81  const char* detStr = getDetString(det);
82  const char* mcStr = isData ? "data" : "mc";
83 
84  if(fReadEpochsFromCSV) {
85  //Wil now attempt to read the absolute calibration filename
86  std::string prePattern=TString::Format("%s/%s/calib_abs_consts.%s.%s.%s", ups_path, fTag.c_str(),
87  detStr, mcStr, fTag.c_str()).Data();
88  std::string postPattern=TString::Format(".csv").Data();
89  csvSource=getCSVFilenameByParsingDirectory(fRun,prePattern,postPattern);
90  }
91  else if (fUseEpochs) {
92  GetEpochFromDB(det,isData);
93  assert(!fEpoch.empty());
94  csvSource = TString::Format("%s/%s/calib_abs_consts.%s.%s.%s.%s.csv",
95  ups_path, fTag.c_str(), detStr,
96  mcStr, fTag.c_str(), fEpoch.c_str()).Data();
97  }
98  else {
99  if (!fGainSetting)
100  csvSource = TString::Format("%s/%s/calib_abs_consts.%s.%s.%s.csv",
101  ups_path, fTag.c_str(),
102  detStr, mcStr, fTag.c_str()).Data();
103  else {
104  csvSource = TString::Format("%s/%s/calib_abs_consts.%s.%s.%s.g%d.csv",
105  ups_path, fTag.c_str(), detStr, mcStr,
106  fTag.c_str(), fGainSetting).Data();
107  }
108  }
109  }
110 
111  mf::LogInfo("AbsCache") << "Loading absolute cache from "
112  << csvSource << std::endl;
113  ok = ok && (tblConsts.LoadFromCSV(csvSource) > 0);
114  }
115  else{
116  ok = ok && tblConsts.Load();
117  }
118 
119  if(!ok){
120  // No safe way to handle this
121  mf::LogError("AbsCache") << "Unable to load absolute tables. Aborting"
122  << std::endl;
123  // I've definitely seen mf lose races with shutdown
124  std::cerr << "Unable to load absolute tables. Aborting"
125  << std::endl;
126  abort();
127  }
128 
129  const int meuCol = tblConsts.GetColIndex("meu");
130  const int meuErrCol = tblConsts.GetColIndex("meu_err");
131  const int meuGeVCol = tblConsts.GetColIndex("meu_gev");
132 
133  const int Nconsts = tblConsts.NRow();
134  for(int nconsts = 0; nconsts < Nconsts; ++nconsts){
135  nova::dbi::Row* row = tblConsts.GetRow(nconsts);
136 
137  const int diblock = row->Channel();
138 
139  Record rec;
140 
141  ok = ok && row->Col(meuCol).Get(rec.meu);
142  ok = ok && row->Col(meuErrCol).Get(rec.meuErr);
143  ok = ok && row->Col(meuGeVCol).Get(rec.meuGeV);
144 
145  assert(ok);
146 
147  if(isData)
149  else
151  } // end for nconsts
152  }
153 
154  //......................................................................
155  void AbsCache::GetEpochFromDB(int idet, bool isData)
156  {
157  nova::dbi::Table tbl("Calibration/tables/CalibAbsEpochs.xml");
158  if (isData)
160  else
162  tbl.SetDetector(idet);
163  tbl.SetTag(fEpochTag);
164  tbl.SetMinTSVld(fRun);
165  tbl.SetMaxTSVld(fRun);
166  assert(tbl.Load());
167  assert(tbl.NRow() == 1);
168 
169 
170  //RJN does this table have a row for a default testbeam epoch??
171  int epochIdx = tbl.GetColIndex("epoch");
172  tbl.GetRow(0)->Col(epochIdx).Get(fEpoch);
173  }
174 
175 
176 
177 } // end namespace calib
178 ////////////////////////////////////////////////////////////////////////
void SetTag(std::string s)
Definition: Table.h:290
static std::string GetName(int id)
void GetEpochFromDB(int idet, bool isData)
Definition: AbsCache.cxx:155
int LoadFromCSV(std::string fname, int offset=0, int limit=-1)
Definition: Table.cpp:1703
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
set< int >::iterator it
Row *const GetRow(int i)
Definition: Table.cpp:492
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
bool fReadEpochsFromCSV
Definition: AbsCache.h:79
OStream cerr
Definition: OStream.cxx:7
caf::StandardRecord * rec
Definition: tutCAFMacro.C:20
void LoadFromVldDatabase(novadaq::cnv::DetId det, bool isData)
Definition: AbsCache.cxx:49
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
bool Get(T &val) const
Definition: Column.h:85
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
std::string fCSVSource
Definition: AbsCache.h:72
void SetDataTypeMask(int mask)
Definition: Table.h:74
bool fUseEpochs
Definition: AbsCache.h:70
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 fTag
Definition: AbsCache.h:75
double GetPECorrToGeVScale(novadaq::cnv::DetId det, int diblock, bool isData)
Definition: AbsCache.cxx:34
std::map< int, Record > fRecordsData
Map is from diblock number.
Definition: AbsCache.h:86
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
std::string fEpoch
Definition: AbsCache.h:76
bool fInitializedMC
Definition: AbsCache.h:84
virtual ~AbsCache()
Definition: AbsCache.cxx:29
int fGainSetting
Definition: AbsCache.h:74
const char * getDetString(novadaq::cnv::DetId det)
Utility function to get detector name as a string for file manipulations.
Definition: CalibUtil.cxx:29
void SetMaxTSVld(time_t t)
Definition: Table.h:285
unsigned long long Channel()
Definition: Row.h:68
assert(nhit_max >=nhit_nbins)
void Format(TGraph *gr, int lcol, int lsty, int lwid, int mcol, int msty, double msiz)
Definition: Style.cxx:154
bool fUseCSVsFromUPS
Definition: AbsCache.h:73
std::map< int, Record > fRecordsMC
Map is from diblock number.
Definition: AbsCache.h:87
std::string fEpochTag
Definition: AbsCache.h:77
void SetVerbosity(int i)
Definition: Table.h:98
int NRow()
Definition: Table.h:101
bool fInitializedData
Whether we have (attempted to) read from db or csv yet.
Definition: AbsCache.h:83