14 #include "Database/cxx/include/Table.h" 24 fReadEpochsFromCSV(false),
25 fInitializedData(false), fInitializedMC(false),
26 fInitializedUncalibratedMask(false),
28 fBrightnessBin(brightness_bin),
79 if(maskUncalibratedChannels &&
130 const char* ups_path =
getenv(
"CALIBCSVS_CSV_PATH");
132 const char* pointsStr = points ?
"points" :
"consts";
136 const char* mcStr = (
fIsData || forceData) ?
"data" :
"mc";
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" ;
155 ups_path,
fTag.c_str(),
156 pointsStr, detStr, mcStr,
fTag.c_str(),
157 gainStr.c_str()).Data();
160 LOG_DEBUG (
"AttenCache") <<
"In GetCSVPath prePattern " 161 << prePattern.c_str()
162 <<
"\t postPattern " << postPattern.c_str() <<
"\n";
171 epStr=
"."+
fEpoch; gainStr=
"";
185 fbStr.c_str() ).Data();
192 ups_path,
fTag.c_str(),
193 pointsStr, detStr, mcStr,
fTag.c_str(),
194 gainStr.c_str(), epStr.c_str(),
195 fbStr.c_str() ).Data();
204 LOG_VERBATIM(
"AttenCache") <<
"Initializing attenuation cache for " 209 LOG_VERBATIM(
"AttenCache") <<
"Loading attenuation cache consts from " 212 FILE* fconsts = fopen(constsCSVSource.c_str(),
"r");
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;
222 fscanf(fconsts,
"%s", hdr);
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
231 while(!feof(fconsts)){
237 const int n = fscanf(fconsts,
"%f,%f,%f,%f,%f,%f,%f,%f,%d,%d",
249 std::cerr <<
"Bad row in " << constsCSVSource
250 <<
"\n Read " << n <<
" columns on line " 257 bool inMuonCatcher =
false;
263 geoplane = geom->
Plane(plane);
266 inMuonCatcher =
true;
274 if(!geoplane)
continue;
276 if(!geocell)
continue;
285 while(regularPlane->
View() != geoplane->
View())
286 regularPlane = geom->
Plane(++i);
298 const int db = plane/64+1;
309 mf::LogInfo(
"AttenCache") <<
"Loading attenuation cache points from " 312 FILE* fpoints = fopen(pointsCSVSource.c_str(),
"r");
319 fscanf(fpoints,
"%s", hdr);
323 std::cerr <<
"Bad header in " << pointsCSVSource
330 while(!feof(fpoints)){
335 const int n = fscanf(fpoints,
"%f,%f,%d,%d", &w, &factor, &channel, &tv);
339 std::cerr <<
"Bad row in " << pointsCSVSource
340 <<
"\n Read " << n <<
" columns on line " 354 std::cerr <<
"Couldn't find appropriate curve for atten point. " 363 std::cerr <<
"Couldn't find appropriate curve for attenp point. " 370 const unsigned int interpIdx = channel%1000;
379 for(
auto it:
fMapData)
it.second->interp_pts.shrink_to_fit();
380 for(
auto it:
fMapMC)
it.second->interp_pts.shrink_to_fit();
387 for(
auto it: fMapData) pointsCount +=
it.second->interp_pts.size();
390 for(
auto it: fMapMC) pointsCount +=
it.second->interp_pts.size();
393 if(pointsCount != totPoints){
394 std::cerr <<
"Mismatch between DB and AttenCurve counts. " 395 << pointsCount <<
" vs "<< totPoints <<
std::endl;
404 <<
"Initializing data attentuation fit chisq values for " 406 <<
" from database to mask uncalibrated channels...\n";
409 tblConsts.SetDetector(geom->
DetId());
412 if(!
fTag.empty()) tblConsts.SetTag(
fTag);
413 tblConsts.SetVerbosity(100);
415 tblConsts.SetMinTSVld(1);
416 tblConsts.SetMaxTSVld(100);
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." 432 mf::LogInfo(
"AttenCache") <<
"Loading attenuation cache from " 434 ok = ok && (tblConsts.LoadFromCSV(constsCSVSource) > 0);
438 ok = ok && tblConsts.Load();
444 mf::LogError(
"AttenCache") <<
"Unable to load attenuation tables. Aborting" 447 std::cerr <<
"Unable to load attenuation tables. Aborting" 452 const int chisqCol = tblConsts.GetColIndex(
"chisq");
454 const int Nconsts = tblConsts.NRow();
455 for(
int nconsts = 0; nconsts < Nconsts; ++nconsts){
459 ok = ok && row->
Col(chisqCol).
Get(chisq);
461 if(!ok) std::abort();
465 if(chisq <= 0 || chisq > 0.2){
470 const int db = chan.
Plane()/64+1;
480 const int isMuonCatcher = (firstPlane <=
plane);
481 return geom->
Plane(plane)->
View()+(2*isMuonCatcher);
491 bool& inMuonCatcher)
const 496 if(view == 0 || view == 1 || view == 1000 || view == 1001){
497 inMuonCatcher =
false;
503 if(view == 2 || view == 3 || view == 10 || view == 11 ||
504 view == 1002 || view == 1003){
511 inMuonCatcher =
true;
531 const int db = chan.
Plane()/64+1;
568 if(fDet < rhs.
fDet)
return true;
569 if(fDet > rhs.
fDet)
return false;
570 return fChan < rhs.
fChan;
void SetTag(std::string s)
static std::string GetName(int id)
#define LOG_DEBUG(stream)
const geo::PlaneGeo * RepresentativePlane(const geo::GeometryBase *geom, int view, bool &inMuonCatcher) const
std::string GetCSVPath(novadaq::cnv::DetId det, bool points, bool forceData=false)
Key_t TranslateChannelMC(geo::OfflineChan chan, const geo::GeometryBase *geom) const
Deal with merged views, muon catcher, etc.
Column & Col(int i)
Find index of column with name. Suitable for passing to Col.
bool doesDirectoryExist(std::string dirSting)
Return true if the path in dirString points to a directory.
enum geo::_plane_proj View_t
Enumerate the possible plane projections.
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
static AttenCurve * Uninitialized(int det, geo::OfflineChan chan)
Return a new AttenCurve objects with fields uninitialized.
const CellGeo * Cell(int icell) const
void Add(AttenCurve *res)
bool operator<(const Key_t &rhs) const
float center_offset
Nonzero in short muon-catcher cells. Positive is closer to readout.
const PlaneGeo * Plane(unsigned int i) const
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
std::set< Key_t > fMapUncalibratedMask
AttenCache(bool is_data, int brightness_bin)
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)
bool fIsData
this is a cache for data calibrations or not
View_t View() const
Which coordinate does this plane measure.
std::string getCSVFilenameByParsingDirectory(int fCurrentRun, std::string prePattern, std::string postPattern)
int GetColIndex(std::string cname)
void SetDataTypeMask(int mask)
bool fInitializedUncalibratedMask
std::set< int > fDiblocks
Diblocks with calibrations.
Encapsulate the geometry of one entire detector (near, far, ndos)
void SetMinTSVld(time_t t)
std::string getenv(std::string const &name)
novadaq::cnv::DetId DetId() const
Prefer ds::DetectorService::DetId() instead.
void GetEpochFromDB(int idet, bool forceData=false)
Geometry information for a single readout plane.
bool SetDetector(std::string det)
Near Detector in the NuMI cavern.
Key_t TranslateChannelData(geo::OfflineChan chan, const geo::GeometryBase *geom) const
Deal with averaged constants etc.
std::map< Key_t, AttenCurve * > fMapMC
unsigned short Plane() const
std::string fCSVSourceDir
const AttenCurve * Get(Key_t key) const
NULL if entry isn't in the cache. Do not delete the returned pointer.
unsigned short Cell() const
const char * getDetString(novadaq::cnv::DetId det)
Utility function to get detector name as a string for file manipulations.
void SetMaxTSVld(time_t t)
const std::set< unsigned int > & GetPlanesByView(View_t v=kXorY) const
unsigned long long Channel()
assert(nhit_max >=nhit_nbins)
void Format(TGraph *gr, int lcol, int lsty, int lwid, int mcol, int msty, double msiz)
void LoadFromDatabaseUncalibratedMask(const geo::GeometryBase *geom)
#define LOG_VERBATIM(category)
The geometry of one entire detector (near, far, ipnd)
std::string to_string(ModuleType mt)
Encapsulate the cell geometry.
void LoadFromDatabase(const geo::GeometryBase *geom)
const AttenCurve * ForMC(const geo::GeometryBase *geom, geo::OfflineChan chan, bool maskUncalibratedChannels)
static OfflineChan FromDBValidityChan(int chan)
#define LOG_ERROR(stream)
std::map< Key_t, AttenCurve * > fMapData
const unsigned int NextPlaneOtherView(unsigned int p, int d=+1) const
const AttenCurve * ForData(const geo::GeometryBase *geom, geo::OfflineChan chan)
std::vector< Pt > interp_pts
const unsigned int FirstPlaneInMuonCatcher() const
Returns the index of the first plane contained in the muon catcher.