Classes | Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
calib::AttenCache Class Reference

#include "/cvmfs/nova-development.opensciencegrid.org/novasoft/releases/N20-10-28/Calibrator/func/AttenCache.h"

Classes

class  Key_t
 

Public Member Functions

 AttenCache (bool is_data, int brightness_bin)
 
virtual ~AttenCache ()
 
const AttenCurveForData (const geo::GeometryBase *geom, geo::OfflineChan chan)
 
const AttenCurveForMC (const geo::GeometryBase *geom, geo::OfflineChan chan, bool maskUncalibratedChannels)
 
void SetCSVSource (const std::string &path)
 
void UseCSVsFromUPS (bool use=true)
 
void SetTag (const std::string &tag)
 
std::string GetTag ()
 
void SetUseEpochs (bool b)
 
bool UseEpochs ()
 
void SetEpoch (const std::string &epoch)
 
void SetEpochTag (const std::string &tag)
 
std::string Epoch ()
 
std::string EpochTag ()
 
void GetEpochFromDB (int idet, bool forceData=false)
 
void ReadEpochsFromCSV (bool readEpochs=true)
 
void SetRun (int run)
 
int Run ()
 
void SetGainSetting (int g)
 
int GainSetting ()
 
void Reinit ()
 

Protected Member Functions

void Add (AttenCurve *res)
 
const AttenCurveGet (Key_t key) const
 NULL if entry isn't in the cache. Do not delete the returned pointer. More...
 
std::string GetCSVPath (novadaq::cnv::DetId det, bool points, bool forceData=false)
 
void LoadFromDatabase (const geo::GeometryBase *geom)
 
void LoadFromDatabaseUncalibratedMask (const geo::GeometryBase *geom)
 
Key_t TranslateChannelData (geo::OfflineChan chan, const geo::GeometryBase *geom) const
 Deal with averaged constants etc. More...
 
Key_t TranslateChannelMC (geo::OfflineChan chan, const geo::GeometryBase *geom) const
 Deal with merged views, muon catcher, etc. More...
 
int GetView (int plane, const geo::GeometryBase *geom) const
 Get the view for the current combination of view and plane (x,y,mux, or muy) More...
 
const geo::PlaneGeoRepresentativePlane (const geo::GeometryBase *geom, int view, bool &inMuonCatcher) const
 

Protected Attributes

std::string fCSVSourceDir
 
bool fUseCSVsFromUPS
 
bool fUseEpochs
 
int fRun
 
std::string fTag
 
std::string fEpoch
 
std::string fEpochTag
 
bool fReadEpochsFromCSV
 
bool fInitializedData
 
bool fInitializedMC
 
bool fInitializedUncalibratedMask
 
int fGainSetting
 
int fBrightnessBin
 
bool fIsData
 this is a cache for data calibrations or not More...
 
std::map< Key_t, AttenCurve * > fMapData
 
std::map< Key_t, AttenCurve * > fMapMC
 
std::set< Key_tfMapUncalibratedMask
 
std::set< intfDiblocks
 Diblocks with calibrations. More...
 

Detailed Description

Definition at line 22 of file AttenCache.h.

Constructor & Destructor Documentation

calib::AttenCache::AttenCache ( bool  is_data,
int  brightness_bin 
)

Definition at line 22 of file AttenCache.cxx.

23  : fUseEpochs(false),
24  fReadEpochsFromCSV(false),
25  fInitializedData(false), fInitializedMC(false),
27  fGainSetting(0),
28  fBrightnessBin(brightness_bin),
29  fIsData(is_data)
30  {
31  }
bool fIsData
this is a cache for data calibrations or not
Definition: AttenCache.h:123
bool fInitializedUncalibratedMask
Definition: AttenCache.h:118
calib::AttenCache::~AttenCache ( )
virtual

Definition at line 34 of file AttenCache.cxx.

References fMapData, fMapMC, and it.

35  {
36  for(auto it: fMapMC) delete it.second;
37  for(auto it: fMapData) delete it.second;
38  }
set< int >::iterator it
std::map< Key_t, AttenCurve * > fMapMC
Definition: AttenCache.h:126
std::map< Key_t, AttenCurve * > fMapData
Definition: AttenCache.h:125

Member Function Documentation

void calib::AttenCache::Add ( AttenCurve res)
protected

Definition at line 41 of file AttenCache.cxx.

References calib::AttenCurve::chan, calib::AttenCurve::det, fIsData, fMapData, and fMapMC.

Referenced by LoadFromDatabase().

42  {
43  if(fIsData){
44  fMapData[Key_t(res->det, res->chan)] = res;
45  }
46  else{
47  fMapMC[Key_t(res->det, res->chan)] = res;
48  }
49  }
bool fIsData
this is a cache for data calibrations or not
Definition: AttenCache.h:123
std::map< Key_t, AttenCurve * > fMapMC
Definition: AttenCache.h:126
std::map< Key_t, AttenCurve * > fMapData
Definition: AttenCache.h:125
std::string calib::AttenCache::Epoch ( )
inline

Definition at line 51 of file AttenCache.h.

51 {return fEpoch;}
std::string fEpoch
Definition: AttenCache.h:110
std::string calib::AttenCache::EpochTag ( )
inline

Definition at line 52 of file AttenCache.h.

52 { return fEpochTag; }
std::string fEpochTag
Definition: AttenCache.h:111
const AttenCurve * calib::AttenCache::ForData ( const geo::GeometryBase geom,
geo::OfflineChan  chan 
)

Return an AttenCurve object matching your parameters, or NULL if there isn't one in the database. Do not delete the resulting object, they are cached internally.

Definition at line 52 of file AttenCache.cxx.

References fInitializedData, Get(), LoadFromDatabase(), and TranslateChannelData().

Referenced by calib::Calibrator::GetAttenCurve().

54  {
55  // TODO - this won't behave right if we ask for more than one detector
56  if(!fInitializedData){
57  fInitializedData = true;
58  LoadFromDatabase(geom);
59  }
60 
61  return Get(TranslateChannelData(chan, geom));
62  }
Key_t TranslateChannelData(geo::OfflineChan chan, const geo::GeometryBase *geom) const
Deal with averaged constants etc.
Definition: AttenCache.cxx:528
const AttenCurve * Get(Key_t key) const
NULL if entry isn&#39;t in the cache. Do not delete the returned pointer.
Definition: AttenCache.cxx:88
void LoadFromDatabase(const geo::GeometryBase *geom)
Definition: AttenCache.cxx:199
const AttenCurve * calib::AttenCache::ForMC ( const geo::GeometryBase geom,
geo::OfflineChan  chan,
bool  maskUncalibratedChannels 
)

Definition at line 65 of file AttenCache.cxx.

References fInitializedMC, fInitializedUncalibratedMask, fMapUncalibratedMask, Get(), LoadFromDatabase(), LoadFromDatabaseUncalibratedMask(), TranslateChannelData(), and TranslateChannelMC().

67  {
68  // TODO - this won't behave right if we ask for more than one detector
69  if(!fInitializedMC){
70  fInitializedMC = true;
71  LoadFromDatabase(geom);
72  }
73 
74  if(!fInitializedUncalibratedMask && maskUncalibratedChannels){
77  }
78 
79  if(maskUncalibratedChannels &&
80  fMapUncalibratedMask.count(TranslateChannelData(chan, geom))){
81  return 0;
82  }
83 
84  return Get(TranslateChannelMC(chan, geom));
85  }
Key_t TranslateChannelMC(geo::OfflineChan chan, const geo::GeometryBase *geom) const
Deal with merged views, muon catcher, etc.
Definition: AttenCache.cxx:545
std::set< Key_t > fMapUncalibratedMask
Definition: AttenCache.h:127
bool fInitializedUncalibratedMask
Definition: AttenCache.h:118
Key_t TranslateChannelData(geo::OfflineChan chan, const geo::GeometryBase *geom) const
Deal with averaged constants etc.
Definition: AttenCache.cxx:528
const AttenCurve * Get(Key_t key) const
NULL if entry isn&#39;t in the cache. Do not delete the returned pointer.
Definition: AttenCache.cxx:88
void LoadFromDatabaseUncalibratedMask(const geo::GeometryBase *geom)
Definition: AttenCache.cxx:401
void LoadFromDatabase(const geo::GeometryBase *geom)
Definition: AttenCache.cxx:199
int calib::AttenCache::GainSetting ( )
inline

Definition at line 62 of file AttenCache.h.

Referenced by calib::Calibrator::postBeginRun().

62 {return fGainSetting;}
const AttenCurve * calib::AttenCache::Get ( Key_t  key) const
protected

NULL if entry isn't in the cache. Do not delete the returned pointer.

Definition at line 88 of file AttenCache.cxx.

References fIsData, fMapData, fMapMC, and it.

Referenced by ForData(), ForMC(), and TranslateChannelMC().

89  {
90  if(fIsData){
91  auto it = fMapData.find(key);
92  if(it != fMapData.end()) return it->second;
93  }
94  else{
95  auto it = fMapMC.find(key);
96  if(it != fMapMC.end()) return it->second;
97  }
98 
99  return 0;
100  }
set< int >::iterator it
bool fIsData
this is a cache for data calibrations or not
Definition: AttenCache.h:123
std::map< Key_t, AttenCurve * > fMapMC
Definition: AttenCache.h:126
std::map< Key_t, AttenCurve * > fMapData
Definition: AttenCache.h:125
std::string calib::AttenCache::GetCSVPath ( novadaq::cnv::DetId  det,
bool  points,
bool  forceData = false 
)
protected

Definition at line 125 of file AttenCache.cxx.

References ana::assert(), calib::doesDirectoryExist(), fBrightnessBin, fCSVSourceDir, fEpoch, fGainSetting, fIsData, genie::utils::style::Format(), fReadEpochsFromCSV, fRun, fTag, fUseCSVsFromUPS, fUseEpochs, calib::getCSVFilenameByParsingDirectory(), calib::getDetString(), cet::getenv(), GetEpochFromDB(), LOG_DEBUG, LOG_ERROR, and art::to_string().

Referenced by LoadFromDatabase(), and LoadFromDatabaseUncalibratedMask().

128  {
129  // necessary components of path
130  const char* ups_path = getenv("CALIBCSVS_CSV_PATH");
131  assert(ups_path);
132  const char* pointsStr = points ? "points" : "consts";
133 
134  const char* detStr = calib::getDetString(det);
135  //(det == novadaq::cnv::kFARDET) ? "fd" : "nd";
136  const char* mcStr = (fIsData || forceData) ? "data" : "mc";
137 
138  // extra specifiers depending on epoch, gain, and brightness settings
139  std::string gainStr, epStr, fbStr;
140  fbStr = (forceData || fIsData || fBrightnessBin<0) ? "" : ".fb"+std::to_string(fBrightnessBin);
141 
142  //RJN check that directory exists
143  std::string dirString=TString::Format("%s/%s",ups_path, fTag.c_str()).Data();
144  if(!doesDirectoryExist(dirString)) {
145  LOG_ERROR("AttenCache") << dirString << " is not a directory is there a mismatch between the CALIBCSVS_CSV_PATH and the calibration tag in Calibration.fcl ?\nCALIBCSVS_CSV_PATH is" << ups_path << "\nTag is " << fTag << "\n" ;
146  abort();
147  // assert(!"Can't find the right calibration directory");
148  }
149 
150 
151  if(fReadEpochsFromCSV) {
152  //Need to read the epochs and then assign things correctly
153  //Currently this won't work unles fUseCSVsFromUPS is set RJN should fix this soon.
154  std::string prePattern=TString::Format("%s/%s/calib_atten_%s.%s.%s.%s%s",
155  ups_path, fTag.c_str(), // -Path
156  pointsStr, detStr, mcStr, fTag.c_str(), // -CSV types
157  gainStr.c_str()).Data();
158  std::string postPattern=TString::Format("%s.csv",fbStr.c_str() ).Data();
159 
160  LOG_DEBUG ("AttenCache") << "In GetCSVPath prePattern "
161  << prePattern.c_str()
162  << "\t postPattern " << postPattern.c_str() << "\n";
163 
164  return getCSVFilenameByParsingDirectory(fRun,prePattern,postPattern);
165 
166 
167  }
168  else if (fUseEpochs) {
169  GetEpochFromDB(det,forceData);
170  assert(!fEpoch.empty());
171  epStr="."+fEpoch; gainStr="";
172  }
173  else {
174  epStr="";
175  if (!fGainSetting) gainStr="";
176  else gainStr=TString::Format(".g%d",fGainSetting);
177  }
178 
179 
180  // if configured to provide own path
181  if(!fUseCSVsFromUPS){
182  return TString::Format("%s/calib_atten_%s%s.csv",
183  fCSVSourceDir.c_str(),
184  pointsStr,
185  fbStr.c_str() ).Data();
186 
187  }
188 
189 
190  // Return final path |----| last 3 could be empty
191  return TString::Format("%s/%s/calib_atten_%s.%s.%s.%s%s%s%s.csv",
192  ups_path, fTag.c_str(), // -Path
193  pointsStr, detStr, mcStr, fTag.c_str(), // -CSV types
194  gainStr.c_str(), epStr.c_str(), // -Extra specifiers which
195  fbStr.c_str() ).Data(); // could be empty
196  }
#define LOG_DEBUG(stream)
Definition: Messenger.h:149
bool doesDirectoryExist(std::string dirSting)
Return true if the path in dirString points to a directory.
Definition: CalibUtil.cxx:759
std::string fEpoch
Definition: AttenCache.h:110
std::string fTag
Definition: AttenCache.h:109
bool fIsData
this is a cache for data calibrations or not
Definition: AttenCache.h:123
std::string getCSVFilenameByParsingDirectory(int fCurrentRun, std::string prePattern, std::string postPattern)
Definition: CalibUtil.cxx:706
std::string getenv(std::string const &name)
void GetEpochFromDB(int idet, bool forceData=false)
Definition: AttenCache.cxx:103
std::string fCSVSourceDir
Definition: AttenCache.h:105
::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
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
std::string to_string(ModuleType mt)
Definition: ModuleType.h:32
#define LOG_ERROR(stream)
Definition: Messenger.h:129
void calib::AttenCache::GetEpochFromDB ( int  idet,
bool  forceData = false 
)

Definition at line 103 of file AttenCache.cxx.

References ana::assert(), nova::dbi::Row::Col(), fEpoch, fEpochTag, fIsData, fRun, nova::dbi::Column::Get(), nova::dbi::Table::GetColIndex(), nova::dbi::Table::GetRow(), nova::dbi::kDataOnly, nova::dbi::kMCOnly, nova::dbi::Table::Load(), nova::dbi::Table::NRow(), nova::dbi::Table::SetDataTypeMask(), nova::dbi::Table::SetDetector(), nova::dbi::Table::SetMaxTSVld(), nova::dbi::Table::SetMinTSVld(), nova::dbi::Table::SetTag(), and nova::dbi::Table::SetVerbosity().

Referenced by GetCSVPath().

104  {
105  nova::dbi::Table tbl("Calibration/tables/CalibAttenEpochs.xml");
106  if (fIsData || forceData)
107  tbl.SetDataTypeMask(nova::dbi::kDataOnly);
108  else
109  tbl.SetDataTypeMask(nova::dbi::kMCOnly);
110  tbl.SetTag(fEpochTag);
111  tbl.SetDetector(idet);
112  tbl.SetMinTSVld(fRun);
113  tbl.SetMaxTSVld(fRun);
114  tbl.SetVerbosity(100);
115  assert(tbl.Load());
116  assert(tbl.NRow() == 1);
117 
118  int epochIdx = tbl.GetColIndex("epoch");
119  tbl.GetRow(0)->Col(epochIdx).Get(fEpoch);
120  }
std::string fEpoch
Definition: AttenCache.h:110
bool fIsData
this is a cache for data calibrations or not
Definition: AttenCache.h:123
std::string fEpochTag
Definition: AttenCache.h:111
assert(nhit_max >=nhit_nbins)
std::string calib::AttenCache::GetTag ( )
inline

Definition at line 42 of file AttenCache.h.

42 {return fTag;}
std::string fTag
Definition: AttenCache.h:109
int calib::AttenCache::GetView ( int  plane,
const geo::GeometryBase geom 
) const
protected

Get the view for the current combination of view and plane (x,y,mux, or muy)

Definition at line 476 of file AttenCache.cxx.

References geo::GeometryBase::DetId(), geo::GeometryBase::FirstPlaneInMuonCatcher(), novadaq::cnv::kNEARDET, geo::GeometryBase::Plane(), NDAPDHVSetting::plane, RepresentativePlane(), and geo::PlaneGeo::View().

Referenced by TranslateChannelData(), and TranslateChannelMC().

477  {
478  if(geom->DetId() == novadaq::cnv::kNEARDET){
479  const int firstPlane = geom->FirstPlaneInMuonCatcher();
480  const int isMuonCatcher = (firstPlane <= plane);
481  return geom->Plane(plane)->View()+(2*isMuonCatcher);
482  }
483 
484  return geom->Plane(plane)->View();
485  }
const PlaneGeo * Plane(unsigned int i) const
View_t View() const
Which coordinate does this plane measure.
Definition: PlaneGeo.h:53
novadaq::cnv::DetId DetId() const
Prefer ds::DetectorService::DetId() instead.
Definition: GeometryBase.h:243
Near Detector in the NuMI cavern.
const unsigned int FirstPlaneInMuonCatcher() const
Returns the index of the first plane contained in the muon catcher.
void calib::AttenCache::LoadFromDatabase ( const geo::GeometryBase geom)
protected

Definition at line 199 of file AttenCache.cxx.

References Add(), calib::AttenCurve::atten_length, calib::AttenCurve::background, geo::OfflineChan::Cell(), geo::PlaneGeo::Cell(), calib::AttenCurve::cell_length, calib::AttenCurve::center_offset, om::cerr, calib::AttenCurve::chan, febshutoff_auto::chan, calib::AttenCurve::chisq, calib::AttenCurve::coeff_exp, calib::AttenCurve::coeff_high, calib::AttenCurve::coeff_low, geo::GeometryBase::DetId(), calib::AttenCurve::edge_high, calib::AttenCurve::edge_low, allTimeWatchdog::endl, fclose(), fDiblocks, geo::GeometryBase::FirstPlaneInMuonCatcher(), fIsData, fMapData, fMapMC, fReadEpochsFromCSV, geo::OfflineChan::FromDBValidityChan(), GetCSVPath(), novadaq::cnv::DetInfo::GetName(), geo::CellGeo::HalfL(), MECModelEnuComparisons::i, calib::AttenCurve::initialized, makeTrainCVSamples::int, calib::AttenCurve::interp_pts, it, novadaq::cnv::kNEARDET, make_syst_table_plots::line, LOG_VERBATIM, getGoodRuns4SAM::n, geo::OfflineChan::Plane(), geo::GeometryBase::Plane(), NDAPDHVSetting::plane, RepresentativePlane(), calib::AttenCurve::Uninitialized(), geo::PlaneGeo::View(), and w.

Referenced by ForData(), and ForMC().

200  {
201  // For the FD this will make a cache ~80MB. That's OK, but denser LOWESS
202  // points would increase this rapidly.
203 
204  LOG_VERBATIM("AttenCache") << "Initializing attenuation cache for "
206  << "\t fReadEpochsFromCSV:\t" << fReadEpochsFromCSV << "\n";
207 
208  const std::string constsCSVSource = GetCSVPath(geom->DetId(), false);
209  LOG_VERBATIM("AttenCache") << "Loading attenuation cache consts from "
210  << constsCSVSource;
211 
212  FILE* fconsts = fopen(constsCSVSource.c_str(), "r");
213  if(!fconsts){
214  std::cerr << "Couldn't open " << constsCSVSource << std::endl;
215  if( constsCSVSource.find(".fb") != std::string::npos )
216  std::cerr << " Note: cannot model fiber brightness while using"
217  << " UPS CVS constants tagged v9 or before." << std::endl;
218  abort();
219  }
220 
221  char hdr[4096];
222  fscanf(fconsts, "%s", hdr);
223  // Expect the file to start with this exact header
224  if(hdr != std::string("#coeff_exp,atten_length,background,edge_low,edge_high,coeff_low,coeff_high,chisq,channel,tv")){
225  std::cerr << "Bad header in " << constsCSVSource
226  << " : '" << hdr << "'" << std::endl;
227  abort();
228  }
229 
230  unsigned int line=0;
231  while(!feof(fconsts)){
232  line++;
233  AttenCurve* res = AttenCurve::Uninitialized(geom->DetId(),
234  geo::OfflineChan(-1, -1));
235 
236  int channel, tv;
237  const int n = fscanf(fconsts, "%f,%f,%f,%f,%f,%f,%f,%f,%d,%d",
238  &res->coeff_exp, &res->atten_length, &res->background,
239  &res->edge_low, &res->edge_high,
240  &res->coeff_low, &res->coeff_high,
241  &res->chisq,
242  &channel, &tv);
243  if(n == EOF){
244  delete res;
245  break;
246  }
247 
248  if(n != 10){
249  std::cerr << "Bad row in " << constsCSVSource
250  << "\n Read " << n << " columns on line "
251  << line+1 << std::endl;
252  abort();
253  }
254 
255  res->chan = geo::OfflineChan::FromDBValidityChan(channel);
256 
257  bool inMuonCatcher = false;
258  const geo::PlaneGeo* geoplane = 0;
259 
260  const int plane = res->chan.Plane();
261  if(fIsData && plane < 1000){
262  // Normal data just has regular channel numbers
263  geoplane = geom->Plane(plane);
264  if(geom->DetId() == novadaq::cnv::kNEARDET &&
265  plane >= int(geom->FirstPlaneInMuonCatcher())){
266  inMuonCatcher = true;
267  }
268  }
269  else{
270  // Otherwise is merged views or MC
271  geoplane = RepresentativePlane(geom, plane, inMuonCatcher);
272  }
273 
274  if(!geoplane) continue;
275  const geo::CellGeo* geocell = geoplane->Cell(res->chan.Cell());
276  if(!geocell) continue;
277 
278  res->cell_length = 2*geocell->HalfL();
279 
280  if(inMuonCatcher){
281  // Find a plane from the same view that's not in the muon catcher, to
282  // find out how much the muon catcher cell must be displaced.
283  int i = 0;
284  const geo::PlaneGeo* regularPlane = geom->Plane(i);
285  while(regularPlane->View() != geoplane->View())
286  regularPlane = geom->Plane(++i);
287  const geo::CellGeo* regularCell = regularPlane->Cell(res->chan.Cell());
288 
289  // Assume that the cells are aligned on the readout side
290  res->center_offset = regularCell->HalfL()-geocell->HalfL();
291  }
292 
293  res->initialized = true;
294 
295  Add(res);
296 
297  // Figure out which diblock, mark it calibrated
298  const int db = plane/64+1;
299  fDiblocks.insert(db);
300  } // end while
301 
302  fclose(fconsts);
303 
304 
305  // Now fetch the LOWESS points and add them to the curves we've just
306  // created.
307 
308  const std::string pointsCSVSource = GetCSVPath(geom->DetId(), true);
309  mf::LogInfo("AttenCache") << "Loading attenuation cache points from "
310  << pointsCSVSource;
311 
312  FILE* fpoints = fopen(pointsCSVSource.c_str(), "r");
313  if(!fpoints){
314  std::cerr << "Couldn't open " << pointsCSVSource << std::endl;
315  abort();
316  }
317 
318  // Expect the file to start with this exact header
319  fscanf(fpoints, "%s", hdr);
320 
321  // Expect the file to start with this exact header
322  if(hdr != std::string("#w,factor,channel,tv")){
323  std::cerr << "Bad header in " << pointsCSVSource
324  << " : '" << hdr << "'" << std::endl;
325  abort();
326  }
327 
328  int totPoints = 0;
329  line = 0;
330  while(!feof(fpoints)){
331  line++;
332  float w, factor;
333  int channel, tv;
334 
335  const int n = fscanf(fpoints, "%f,%f,%d,%d", &w, &factor, &channel, &tv);
336  if(n == EOF) break;
337 
338  if(n != 4){
339  std::cerr << "Bad row in " << pointsCSVSource
340  << "\n Read " << n << " columns on line "
341  << line+1 << std::endl;
342  abort();
343  }
344 
345  ++totPoints;
346 
348 
349  AttenCurve* curve;
350 
351  if(fIsData){
352  auto it = fMapData.find(Key_t(geom->DetId(), chan));
353  if(it == fMapData.end()){
354  std::cerr << "Couldn't find appropriate curve for atten point. "
355  << "Chan = " << chan << std::endl;
356  std::abort();
357  }
358  curve = it->second;
359  }
360  else{
361  auto it = fMapMC.find(Key_t(geom->DetId(), chan));
362  if(it == fMapMC.end()){
363  std::cerr << "Couldn't find appropriate curve for attenp point. "
364  << "Chan = " << chan << std::endl;
365  std::abort();
366  }
367  curve = it->second;
368  }
369 
370  const unsigned int interpIdx = channel%1000;
371  if(curve->interp_pts.size() <= interpIdx)
372  curve->interp_pts.resize(interpIdx+1);
373  curve->interp_pts[interpIdx] = {w, factor};
374  } // end while
375 
376  fclose(fpoints);
377 
378  // Save a little memory
379  for(auto it: fMapData) it.second->interp_pts.shrink_to_fit();
380  for(auto it: fMapMC) it.second->interp_pts.shrink_to_fit();
381 
382  // Check that the number of points we got from the DB is the same as the
383  // total in all the AttenCurves. If these numbers don't match, it means
384  // there were duplicates or gaps.
385  int pointsCount = 0;
386  if(fIsData){
387  for(auto it: fMapData) pointsCount += it.second->interp_pts.size();
388  }
389  else{
390  for(auto it: fMapMC) pointsCount += it.second->interp_pts.size();
391  }
392 
393  if(pointsCount != totPoints){
394  std::cerr << "Mismatch between DB and AttenCurve counts. "
395  << pointsCount << " vs "<< totPoints << std::endl;
396  std::abort();
397  }
398  }
static std::string GetName(int id)
const geo::PlaneGeo * RepresentativePlane(const geo::GeometryBase *geom, int view, bool &inMuonCatcher) const
Definition: AttenCache.cxx:489
double HalfL() const
Definition: CellGeo.cxx:198
std::string GetCSVPath(novadaq::cnv::DetId det, bool points, bool forceData=false)
Definition: AttenCache.cxx:125
set< int >::iterator it
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
static AttenCurve * Uninitialized(int det, geo::OfflineChan chan)
Return a new AttenCurve objects with fields uninitialized.
Definition: AttenCurve.cxx:25
const CellGeo * Cell(int icell) const
Definition: PlaneGeo.h:48
void Add(AttenCurve *res)
Definition: AttenCache.cxx:41
OStream cerr
Definition: OStream.cxx:7
const PlaneGeo * Plane(unsigned int i) const
fclose(fg1)
bool fIsData
this is a cache for data calibrations or not
Definition: AttenCache.h:123
View_t View() const
Which coordinate does this plane measure.
Definition: PlaneGeo.h:53
std::set< int > fDiblocks
Diblocks with calibrations.
Definition: AttenCache.h:129
novadaq::cnv::DetId DetId() const
Prefer ds::DetectorService::DetId() instead.
Definition: GeometryBase.h:243
Geometry information for a single readout plane.
Definition: PlaneGeo.h:36
Near Detector in the NuMI cavern.
std::map< Key_t, AttenCurve * > fMapMC
Definition: AttenCache.h:126
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
A (plane, cell) pair.
Definition: OfflineChan.h:17
#define LOG_VERBATIM(category)
Encapsulate the cell geometry.
Definition: CellGeo.h:25
Float_t w
Definition: plot.C:20
static OfflineChan FromDBValidityChan(int chan)
Definition: OfflineChan.cxx:13
std::map< Key_t, AttenCurve * > fMapData
Definition: AttenCache.h:125
const unsigned int FirstPlaneInMuonCatcher() const
Returns the index of the first plane contained in the muon catcher.
void calib::AttenCache::LoadFromDatabaseUncalibratedMask ( const geo::GeometryBase geom)
protected

Definition at line 401 of file AttenCache.cxx.

References om::cerr, febshutoff_auto::chan, nova::dbi::Row::Channel(), nova::dbi::Row::Col(), geo::GeometryBase::DetId(), allTimeWatchdog::endl, fCSVSourceDir, fDiblocks, fMapUncalibratedMask, geo::OfflineChan::FromDBValidityChan(), fTag, fUseCSVsFromUPS, nova::dbi::Column::Get(), GetCSVPath(), novadaq::cnv::DetInfo::GetName(), nova::dbi::kDataOnly, geo::OfflineChan::Plane(), and check_grl::row.

Referenced by ForMC().

402  {
403  mf::LogInfo("AttenCache")
404  << "Initializing data attentuation fit chisq values for "
406  << " from database to mask uncalibrated channels...\n";
407 
408  nova::dbi::Table tblConsts("Calibration/tables/CalibAttenConstsVld.xml");
409  tblConsts.SetDetector(geom->DetId());
410  tblConsts.SetDataTypeMask(nova::dbi::kDataOnly);
411 
412  if(!fTag.empty()) tblConsts.SetTag(fTag);
413  tblConsts.SetVerbosity(100);
414 
415  tblConsts.SetMinTSVld(1);
416  tblConsts.SetMaxTSVld(100);
417 
418  bool ok = true;
419 
420  if(!fCSVSourceDir.empty()){
421  mf::LogInfo("AttenCache")
422  << "Trying to get data constants from the MC CSV file, "
423  << "no good way to handle this if you want to use masking. Aborting."
424  << std::endl;
425  std::abort();
426  }
427  else{
428  if(fUseCSVsFromUPS){
429  // Always ask for data, because we're masking the MC to match real data
430  // conditions.
431  const std::string constsCSVSource = GetCSVPath(geom->DetId(), false, true);
432  mf::LogInfo("AttenCache") << "Loading attenuation cache from "
433  << constsCSVSource << std::endl;
434  ok = ok && (tblConsts.LoadFromCSV(constsCSVSource) > 0);
435  mf::LogInfo("AttenCache") << "Got " << tblConsts.NRow() << " rows from CSV" << std::endl;
436  }
437  else{
438  ok = ok && tblConsts.Load();
439  }
440  }
441 
442  if(!ok){
443  // No safe way to handle this
444  mf::LogError("AttenCache") << "Unable to load attenuation tables. Aborting"
445  << std::endl;
446  // I've definitely seen mf lose races with shutdown
447  std::cerr << "Unable to load attenuation tables. Aborting"
448  << std::endl;
449  std::abort();
450  }
451 
452  const int chisqCol = tblConsts.GetColIndex("chisq");
453 
454  const int Nconsts = tblConsts.NRow();
455  for(int nconsts = 0; nconsts < Nconsts; ++nconsts){
456  nova::dbi::Row* row = tblConsts.GetRow(nconsts);
457 
458  double chisq;
459  ok = ok && row->Col(chisqCol).Get(chisq);
460 
461  if(!ok) std::abort();
462 
464 
465  if(chisq <= 0 || chisq > 0.2){
466  fMapUncalibratedMask.insert(Key_t(geom->DetId(), chan));
467  }
468 
469  // Figure out which diblock, mark it calibrated
470  const int db = chan.Plane()/64+1;
471  fDiblocks.insert(db);
472  } // end for nconsts
473  }
static std::string GetName(int id)
std::string GetCSVPath(novadaq::cnv::DetId det, bool points, bool forceData=false)
Definition: AttenCache.cxx:125
Column & Col(int i)
Find index of column with name. Suitable for passing to Col.
Definition: Row.h:63
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
OStream cerr
Definition: OStream.cxx:7
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
bool Get(T &val) const
Definition: Column.h:85
std::set< Key_t > fMapUncalibratedMask
Definition: AttenCache.h:127
std::string fTag
Definition: AttenCache.h:109
std::set< int > fDiblocks
Diblocks with calibrations.
Definition: AttenCache.h:129
novadaq::cnv::DetId DetId() const
Prefer ds::DetectorService::DetId() instead.
Definition: GeometryBase.h:243
unsigned short Plane() const
Definition: OfflineChan.h:31
std::string fCSVSourceDir
Definition: AttenCache.h:105
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
A (plane, cell) pair.
Definition: OfflineChan.h:17
unsigned long long Channel()
Definition: Row.h:68
static OfflineChan FromDBValidityChan(int chan)
Definition: OfflineChan.cxx:13
void calib::AttenCache::ReadEpochsFromCSV ( bool  readEpochs = true)
inline

Definition at line 55 of file AttenCache.h.

Referenced by calib::Calibrator::postBeginRun().

55 {fReadEpochsFromCSV=readEpochs;}
void calib::AttenCache::Reinit ( )

Definition at line 574 of file AttenCache.cxx.

References fDiblocks, fInitializedData, fInitializedMC, fInitializedUncalibratedMask, fMapData, fMapMC, fMapUncalibratedMask, and it.

Referenced by calib::Calibrator::postBeginRun().

575  {
576  fInitializedData = false;
577  fInitializedMC = false;
579 
580  // delete the pointers one by one from the data and MC maps
581  for(auto it : fMapData) delete it.second;
582  for(auto it : fMapMC ) delete it.second;
583 
584  fMapData.clear();
585  fMapMC.clear();
586  fMapUncalibratedMask.clear();
587  fDiblocks.clear();
588  }
set< int >::iterator it
std::set< Key_t > fMapUncalibratedMask
Definition: AttenCache.h:127
bool fInitializedUncalibratedMask
Definition: AttenCache.h:118
std::set< int > fDiblocks
Diblocks with calibrations.
Definition: AttenCache.h:129
std::map< Key_t, AttenCurve * > fMapMC
Definition: AttenCache.h:126
std::map< Key_t, AttenCurve * > fMapData
Definition: AttenCache.h:125
const geo::PlaneGeo * calib::AttenCache::RepresentativePlane ( const geo::GeometryBase geom,
int  view,
bool &  inMuonCatcher 
) const
protected

Definition at line 489 of file AttenCache.cxx.

References om::cerr, geo::GeometryBase::DetId(), allTimeWatchdog::endl, geo::GeometryBase::FirstPlaneInMuonCatcher(), geo::GeometryBase::GetPlanesByView(), novadaq::cnv::kNEARDET, geo::GeometryBase::NextPlaneOtherView(), geo::GeometryBase::Plane(), TranslateChannelData(), and geo::PlaneGeo::View().

Referenced by GetView(), and LoadFromDatabase().

492  {
493  // Unfortunately a bunch of different conventions competing here
494 
495  // View-merged MC, or average constants data
496  if(view == 0 || view == 1 || view == 1000 || view == 1001){
497  inMuonCatcher = false;
498  return geom->Plane(*geom->GetPlanesByView(geo::View_t(view)).begin());
499  }
500 
501  // View-merged MC, or some scheme I'm not sure is still in use, or
502  // averaged-constants MC
503  if(view == 2 || view == 3 || view == 10 || view == 11 ||
504  view == 1002 || view == 1003){
505 
506  if(geom->DetId() != novadaq::cnv::kNEARDET){
507  std::cerr << "Muon catcher constants in non-ND detector" << std::endl;
508  std::abort();
509  }
510 
511  inMuonCatcher = true;
512  view = view%2;
513 
514  const int mcp = geom->FirstPlaneInMuonCatcher();
515  const geo::PlaneGeo* geoplane = geom->Plane(mcp);
516  if(geoplane->View() != geo::View_t(view))
517  geoplane = geom->Plane(geom->NextPlaneOtherView(mcp, +1));
518 
519  return geoplane;
520  }
521 
522  std::cerr << "Bad encoded view " << view << std::endl;
523  std::abort();
524  }
enum geo::_plane_proj View_t
Enumerate the possible plane projections.
OStream cerr
Definition: OStream.cxx:7
const PlaneGeo * Plane(unsigned int i) const
View_t View() const
Which coordinate does this plane measure.
Definition: PlaneGeo.h:53
novadaq::cnv::DetId DetId() const
Prefer ds::DetectorService::DetId() instead.
Definition: GeometryBase.h:243
Geometry information for a single readout plane.
Definition: PlaneGeo.h:36
Near Detector in the NuMI cavern.
const std::set< unsigned int > & GetPlanesByView(View_t v=kXorY) const
const unsigned int NextPlaneOtherView(unsigned int p, int d=+1) const
const unsigned int FirstPlaneInMuonCatcher() const
Returns the index of the first plane contained in the muon catcher.
int calib::AttenCache::Run ( )
inline

Definition at line 59 of file AttenCache.h.

59 { return fRun; }
void calib::AttenCache::SetCSVSource ( const std::string &  path)
inline

Definition at line 36 of file AttenCache.h.

References path.

Referenced by calib::Calibrator::postBeginRun().

36 { fCSVSourceDir = path; }
std::string fCSVSourceDir
Definition: AttenCache.h:105
const std::string path
Definition: plot_BEN.C:43
void calib::AttenCache::SetEpoch ( const std::string &  epoch)
inline

Definition at line 48 of file AttenCache.h.

Referenced by calib::Calibrator::postBeginRun().

48 {fEpoch = epoch;}
std::string fEpoch
Definition: AttenCache.h:110
void calib::AttenCache::SetEpochTag ( const std::string &  tag)
inline

Definition at line 49 of file AttenCache.h.

References getGoodRuns4SAM::tag.

Referenced by calib::Calibrator::postBeginRun().

void calib::AttenCache::SetGainSetting ( int  g)
inline
void calib::AttenCache::SetRun ( int  run)
inline
void calib::AttenCache::SetTag ( const std::string &  tag)
inline

Definition at line 40 of file AttenCache.h.

References getGoodRuns4SAM::tag.

Referenced by calib::Calibrator::postBeginRun().

void calib::AttenCache::SetUseEpochs ( bool  b)
inline

Definition at line 44 of file AttenCache.h.

References b.

Referenced by calib::Calibrator::postBeginRun().

44 { fUseEpochs = b; }
const hit & b
Definition: hits.cxx:21
AttenCache::Key_t calib::AttenCache::TranslateChannelData ( geo::OfflineChan  chan,
const geo::GeometryBase geom 
) const
protected

Deal with averaged constants etc.

Definition at line 528 of file AttenCache.cxx.

References geo::OfflineChan::Cell(), febshutoff_auto::chan, geo::GeometryBase::DetId(), fDiblocks, geom(), GetView(), geo::OfflineChan::Plane(), TranslateChannelMC(), and POTSpillRate::view.

Referenced by ForData(), ForMC(), and RepresentativePlane().

530  {
531  const int db = chan.Plane()/64+1;
532  if(!fDiblocks.count(db)){
533  // No calibration for this diblock, use the averages instead.
534  // Stored at channel 1,000,000+1000*view+cell
535  const int view = GetView(chan.Plane(), geom);
536  return Key_t(geom->DetId(),
537  geo::OfflineChan(1000+int(view), chan.Cell()));
538  }
539 
540  return Key_t(geom->DetId(), chan);
541  }
int GetView(int plane, const geo::GeometryBase *geom) const
Get the view for the current combination of view and plane (x,y,mux, or muy)
Definition: AttenCache.cxx:476
std::set< int > fDiblocks
Diblocks with calibrations.
Definition: AttenCache.h:129
novadaq::cnv::DetId DetId() const
Prefer ds::DetectorService::DetId() instead.
Definition: GeometryBase.h:243
unsigned short Plane() const
Definition: OfflineChan.h:31
unsigned short Cell() const
Definition: OfflineChan.h:32
A (plane, cell) pair.
Definition: OfflineChan.h:17
void geom(int which=0)
Definition: geom.C:163
AttenCache::Key_t calib::AttenCache::TranslateChannelMC ( geo::OfflineChan  chan,
const geo::GeometryBase geom 
) const
protected

Deal with merged views, muon catcher, etc.

Definition at line 545 of file AttenCache.cxx.

References geo::OfflineChan::Cell(), geo::GeometryBase::DetId(), geo::GeometryBase::FirstPlaneInMuonCatcher(), geom(), Get(), GetView(), makeTrainCVSamples::int, novadaq::cnv::kNEARDET, geo::OfflineChan::Plane(), geo::GeometryBase::Plane(), and runNovaSAM::ret.

Referenced by ForMC(), and TranslateChannelData().

547  {
548  // If it's a muon catcher plane, check to see if there is a special entry
549  // for it
550  if(geom->DetId() == novadaq::cnv::kNEARDET &&
551  chan.Plane() >= int(geom->FirstPlaneInMuonCatcher())){
552  const int viewInt = GetView(chan.Plane(), geom);
553  const Key_t ret(geom->DetId(), geo::OfflineChan(viewInt, chan.Cell()));
554  if(Get(ret)) return ret;
555  }
556  // If not, fall back to the non-muon-catcher constants
557 
558  // This is the way we encode the MC parameters. Plane represents the view.
559  // TODO, we should move to plane+1000 like the data?
560  return Key_t(geom->DetId(),
561  geo::OfflineChan(geom->Plane(chan.Plane())->View(),
562  chan.Cell()));
563  }
const PlaneGeo * Plane(unsigned int i) const
int GetView(int plane, const geo::GeometryBase *geom) const
Get the view for the current combination of view and plane (x,y,mux, or muy)
Definition: AttenCache.cxx:476
novadaq::cnv::DetId DetId() const
Prefer ds::DetectorService::DetId() instead.
Definition: GeometryBase.h:243
Near Detector in the NuMI cavern.
Definition: View.py:1
unsigned short Plane() const
Definition: OfflineChan.h:31
const AttenCurve * Get(Key_t key) const
NULL if entry isn&#39;t in the cache. Do not delete the returned pointer.
Definition: AttenCache.cxx:88
unsigned short Cell() const
Definition: OfflineChan.h:32
A (plane, cell) pair.
Definition: OfflineChan.h:17
void geom(int which=0)
Definition: geom.C:163
const unsigned int FirstPlaneInMuonCatcher() const
Returns the index of the first plane contained in the muon catcher.
void calib::AttenCache::UseCSVsFromUPS ( bool  use = true)
inline

Definition at line 38 of file AttenCache.h.

Referenced by calib::Calibrator::postBeginRun().

38 {fUseCSVsFromUPS = use;}
bool calib::AttenCache::UseEpochs ( )
inline

Definition at line 46 of file AttenCache.h.

Referenced by calib::Calibrator::postBeginRun().

46 {return fUseEpochs;}

Member Data Documentation

int calib::AttenCache::fBrightnessBin
protected

Definition at line 121 of file AttenCache.h.

Referenced by GetCSVPath().

std::string calib::AttenCache::fCSVSourceDir
protected

Definition at line 105 of file AttenCache.h.

Referenced by GetCSVPath(), and LoadFromDatabaseUncalibratedMask().

std::set<int> calib::AttenCache::fDiblocks
protected

Diblocks with calibrations.

Definition at line 129 of file AttenCache.h.

Referenced by LoadFromDatabase(), LoadFromDatabaseUncalibratedMask(), Reinit(), and TranslateChannelData().

std::string calib::AttenCache::fEpoch
protected

Definition at line 110 of file AttenCache.h.

Referenced by GetCSVPath(), and GetEpochFromDB().

std::string calib::AttenCache::fEpochTag
protected

Definition at line 111 of file AttenCache.h.

Referenced by GetEpochFromDB().

int calib::AttenCache::fGainSetting
protected

Definition at line 120 of file AttenCache.h.

Referenced by GetCSVPath().

bool calib::AttenCache::fInitializedData
protected

Definition at line 116 of file AttenCache.h.

Referenced by ForData(), and Reinit().

bool calib::AttenCache::fInitializedMC
protected

Definition at line 117 of file AttenCache.h.

Referenced by ForMC(), and Reinit().

bool calib::AttenCache::fInitializedUncalibratedMask
protected

Definition at line 118 of file AttenCache.h.

Referenced by ForMC(), and Reinit().

bool calib::AttenCache::fIsData
protected

this is a cache for data calibrations or not

Definition at line 123 of file AttenCache.h.

Referenced by Add(), Get(), GetCSVPath(), GetEpochFromDB(), and LoadFromDatabase().

std::map<Key_t, AttenCurve*> calib::AttenCache::fMapData
protected

Definition at line 125 of file AttenCache.h.

Referenced by Add(), Get(), LoadFromDatabase(), Reinit(), and ~AttenCache().

std::map<Key_t, AttenCurve*> calib::AttenCache::fMapMC
protected

Definition at line 126 of file AttenCache.h.

Referenced by Add(), Get(), LoadFromDatabase(), Reinit(), and ~AttenCache().

std::set<Key_t> calib::AttenCache::fMapUncalibratedMask
protected

Definition at line 127 of file AttenCache.h.

Referenced by ForMC(), LoadFromDatabaseUncalibratedMask(), and Reinit().

bool calib::AttenCache::fReadEpochsFromCSV
protected

Definition at line 113 of file AttenCache.h.

Referenced by GetCSVPath(), and LoadFromDatabase().

int calib::AttenCache::fRun
protected

Definition at line 108 of file AttenCache.h.

Referenced by GetCSVPath(), and GetEpochFromDB().

std::string calib::AttenCache::fTag
protected

Definition at line 109 of file AttenCache.h.

Referenced by GetCSVPath(), and LoadFromDatabaseUncalibratedMask().

bool calib::AttenCache::fUseCSVsFromUPS
protected

Definition at line 106 of file AttenCache.h.

Referenced by GetCSVPath(), and LoadFromDatabaseUncalibratedMask().

bool calib::AttenCache::fUseEpochs
protected

Definition at line 107 of file AttenCache.h.

Referenced by GetCSVPath().


The documentation for this class was generated from the following files: