00001
00002
00003
00004
00005
00006
00007
00008
00009
00011
00012 #include "Fabrication/FabPlnInstallLookup.h"
00013
00014 #include "MessageService/MsgService.h"
00015 CVSID("$Id: FabPlnInstallLookup.cxx,v 1.14 2008/03/04 21:46:45 rhatcher Exp $");
00016
00017 #include "TSystem.h"
00018 #include <set>
00019 #include <map>
00020 using std::set;
00021 using std::map;
00022
00023 #include <ctype.h>
00024 #include <stdio.h>
00025
00026 ClassImp(FabPlnInstallLookup)
00027
00028 static FabPlnInstall* dummyPlnInstall = new FabPlnInstall();
00029 static VldTimeStamp farFuture(2037,12,31,23,59,59);
00030
00031
00032
00033 FabPlnInstallLookup::FabPlnInstallLookup(const VldContext& vldc)
00034 : fPlnInstallTbl(vldc), fSteelPlateTbl(vldc), fVldContext(vldc)
00035 {
00036
00037
00038
00039 const PlexPlaneId unsetPlnId;
00040 UInt_t n = fPlnInstallTbl.GetNumRows();
00041 for (UInt_t irow=0; irow < n; ++irow) {
00042 const FabPlnInstall* fab = fPlnInstallTbl.GetRow(irow);
00043 PlexPlaneId plnid = fab->GetPlaneId();
00044 if ( ! plnid.IsVetoShield() ) {
00045 plnid.SetIsSteel(kTRUE);
00046 if ( plnid == unsetPlnId ) {
00047 MSG("Fabrication",Msg::kWarning)
00048 << " row " << irow << " has bad PlexPlaneId"
00049 << endl
00050 << *fab
00051 << endl;
00052 continue;
00053 }
00054 if ( fFirstNormal == unsetPlnId ) {
00055 fFirstNormal = plnid;
00056 fLastNormal = plnid;
00057 }
00058 if ( plnid < fFirstNormal ) fFirstNormal = plnid;
00059 if ( plnid > fLastNormal ) fLastNormal = plnid;
00060 }
00061 }
00062
00063
00064
00065 }
00066
00067
00068 FabPlnInstallLookup::~FabPlnInstallLookup()
00069 {
00070
00071 }
00072
00073
00074 const FabPlnInstall* FabPlnInstallLookup::NextInstall(const VldTimeStamp& start,
00075 bool ignoreVeto) const
00076 {
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086 VldTimeStamp alttime = start;
00087 const FabPlnInstall* fab;
00088 const DbiValidityRec* dbivld;
00089
00090
00091 VldContext vldcStart(fVldContext.GetDetector(),
00092 fVldContext.GetSimFlag(),alttime);
00093 fPlnInstallTblAlt.NewQuery(vldcStart);
00094
00095 UInt_t n = fPlnInstallTblAlt.GetNumRows();
00096
00097 if ( n <= 0) {
00098
00099
00100
00101 VldContext vldcAlt(fVldContext.GetDetector(),
00102 fVldContext.GetSimFlag(),farFuture);
00103 MSG("Fabrication",Msg::kDebug)
00104 << "No planes yet installed, determine first using " << vldcAlt << endl;
00105 fPlnInstallTblAlt.NewQuery(vldcAlt);
00106 if (fPlnInstallTblAlt.GetNumRows() <= 0) {
00107
00108 MSG("Fabrication",Msg::kInfo)
00109 << "NextInstall() has no info for " << vldcAlt << endl;
00110 return 0;
00111 }
00112
00113
00114 dbivld = fPlnInstallTblAlt.GetValidityRec();
00115 alttime = dbivld->GetVldRange().GetTimeStart();
00116
00117
00118 n = fPlnInstallTblAlt.GetNumRows();
00119 for (UInt_t irow=0; irow < n; ++irow) {
00120 fab = fPlnInstallTblAlt.GetRow(irow);
00121 const VldTimeStamp& ts = fab->GetInstallDate();
00122 if (ts < alttime) alttime = ts;
00123 }
00124
00125 }
00126 else {
00127
00128
00129 dbivld = fPlnInstallTblAlt.GetValidityRec();
00130 alttime = dbivld->GetVldRange().GetTimeEnd();
00131
00132 if ( alttime > farFuture) {
00133
00134 MSG("Fabrication",Msg::kInfo)
00135 << "NextInstall() " << vldcStart << " detector complete?" << endl;
00136 return 0;
00137 }
00138 }
00139
00140
00141 VldContext vldcAlt(fVldContext.GetDetector(),
00142 fVldContext.GetSimFlag(),alttime);
00143 fPlnInstallTblAlt.NewQuery(vldcAlt);
00144
00145 n = fPlnInstallTblAlt.GetNumRows();
00146 for (UInt_t irow=0; irow < n; ++irow) {
00147 fab = fPlnInstallTblAlt.GetRow(irow);
00148 if ( ignoreVeto && fab->GetPlaneId().IsVetoShield() ) continue;
00149 const VldTimeStamp& ts = fab->GetInstallDate();
00150
00151
00152 if (ts >= alttime) return fab;
00153 }
00154
00155
00156 return 0;
00157
00158 }
00159
00160
00161 VldTimeStamp FabPlnInstallLookup::TimeNextInstall(const VldTimeStamp& start,
00162 bool ignoreVeto) const
00163 {
00164
00165
00166 const FabPlnInstall* fab = NextInstall(start,ignoreVeto);
00167 if (fab) return fab->GetInstallDate();
00168 else return farFuture;
00169
00170 }
00171
00172
00173 const FabPlnInstall*
00174 FabPlnInstallLookup::GetFabPlnInstall(const PlexPlaneId& plnid) const
00175 {
00176
00177
00178 if (plnid.GetDetector() != fVldContext.GetDetector()) return 0;
00179 return fPlnInstallTbl.GetRowByIndex(plnid.GetPlane());
00180 }
00181
00182
00183 std::vector<const FabSteelPlate*>
00184 FabPlnInstallLookup::GetFabSteelPlates(const PlexPlaneId& plnid) const
00185 {
00186
00187 std::vector<const FabSteelPlate*> vplates(8);
00188
00189 if (plnid.GetDetector() != fVldContext.GetDetector()) return vplates;
00190 const FabPlnInstall* plnrow = fPlnInstallTbl.GetRowByIndex(plnid.GetPlane());
00191 if ( ! plnrow ) return vplates;
00192
00193 UInt_t ntotplates = fSteelPlateTbl.GetNumRows();
00194 for (UInt_t indx=0; indx<8; ++indx) {
00195 std::string mfgid1 = plnrow->GetSteelSlabMfgId(indx);
00196
00197 for (UInt_t iplate = 0; iplate < ntotplates; ++iplate) {
00198 const FabSteelPlate* platerow = fSteelPlateTbl.GetRow(iplate);
00199 std::string mfgid2 = platerow->GetSteelSlabMfgId();
00200 if ( mfgid1 == mfgid2 ) vplates[indx] = platerow;
00201 }
00202 }
00203
00204 return vplates;
00205 }
00206
00207
00208 std::string FabPlnInstallLookup::GetScintMdlMfgId(const PlexScintMdlId& mdlid) const
00209 {
00210
00211
00212 const FabPlnInstall* fab = GetFabPlnInstall(mdlid);
00213 if (!fab) {
00214 MSG("Fabrication",Msg::kInfo)
00215 << "ScintMdl " << mdlid << " not known to be installed" << endl;
00216 fab = dummyPlnInstall;
00217 }
00218 if (mdlid.GetModule() > 7) {
00219 MSG("Fabrication",Msg::kWarning)
00220 << "FabPlnInstall doesn't support modules > 7; "
00221 << mdlid << " out-of-range" << endl;
00222 }
00223 return fab->GetScintMdlMfgId(mdlid.GetModule());
00224 }
00225
00226
00227 std::string FabPlnInstallLookup::GetSteelSlabMfgId(const PlexPlaneId& plnid,
00228 UShort_t indx) const
00229 {
00230
00231
00232 const FabPlnInstall* fab = GetFabPlnInstall(plnid);
00233 if (!fab) {
00234 MSG("Fabrication",Msg::kInfo)
00235 << "Plane " << plnid << " not known to be installed" << endl;
00236 fab = dummyPlnInstall;
00237 }
00238 if (indx > 7) {
00239 MSG("Fabrication",Msg::kWarning)
00240 << "FabPlnInstall doesn't support steel slab > 7; "
00241 << indx << " out-of-range" << endl;
00242 }
00243 return fab->GetSteelSlabMfgId(indx);
00244 }
00245
00246
00247 PlexScintMdlId FabPlnInstallLookup::WhereIsScintMdl(const std::string& mfgid) const
00248 {
00249
00250
00251 UInt_t n = fPlnInstallTbl.GetNumRows();
00252 for (UInt_t irow=0; irow < n; ++irow) {
00253 const FabPlnInstall* fab = fPlnInstallTbl.GetRow(irow);
00254 for (UInt_t imdl=0; imdl < 8; ++imdl) {
00255 if (fab->GetScintMdlMfgId(imdl) == mfgid)
00256 return PlexScintMdlId(fab->GetPlaneId(),imdl);
00257 }
00258 }
00259 MSG("Fabrication",Msg::kInfo)
00260 << "no placement info for ScintMdl " << mfgid << endl;
00261 return PlexScintMdlId();
00262 }
00263
00264
00265 std::pair<PlexPlaneId,UShort_t>
00266 FabPlnInstallLookup::WhereIsSteelSlab(const std::string& mfgid) const
00267 {
00268
00269
00270 UInt_t n = fPlnInstallTbl.GetNumRows();
00271 for (UInt_t irow=0; irow < n; ++irow) {
00272 const FabPlnInstall* fab = fPlnInstallTbl.GetRow(irow);
00273 for (UInt_t islab=0; islab < 8; ++islab) {
00274 if (fab->GetSteelSlabMfgId(islab) == mfgid)
00275 return pair<PlexPlaneId,UShort_t>(fab->GetPlaneId(),islab);
00276 }
00277 }
00278 MSG("Fabrication",Msg::kInfo)
00279 << "no placement info for steel slab " << mfgid << endl;
00280 return pair<PlexPlaneId,UShort_t>(PlexPlaneId(),0xffff);
00281 }
00282
00283
00284 void FabPlnInstallLookup::Print(Option_t* ) const
00285 {
00286
00287 UInt_t n = fPlnInstallTbl.GetNumRows();
00288 for (UInt_t irow=0; irow < n; ++irow) {
00289 const FabPlnInstall* fab = fPlnInstallTbl.GetRow(irow);
00290 if (fab) cout << *fab;
00291 }
00292 }
00293
00294
00295 unsigned int FabPlnInstallLookup::LookForOddEntries() const
00296 {
00297
00298
00299 cout << endl << " === Looking for odd entries === " << endl;
00300
00301 typedef set<UInt_t> uset_t;
00302 typedef set<UInt_t>::const_iterator usetitr_t;
00303
00304 typedef set<std::string> sset_t;
00305 typedef set<std::string>::iterator ssetitr_t;
00306 typedef pair<ssetitr_t,bool> ssetinsert_t;
00307
00308 sset_t scintset, slabset;
00309
00310
00311 uset_t slabPartNums;
00312 slabPartNums.insert(350949);
00313 slabPartNums.insert(350950);
00314 slabPartNums.insert(350951);
00315 slabPartNums.insert(350952);
00316 slabPartNums.insert(350911);
00317 slabPartNums.insert(350912);
00318 slabPartNums.insert(350913);
00319 slabPartNums.insert(350914);
00320
00321 unsigned int nerrors = 0;
00322
00323 PlexPlaneId plnid;
00324 const FabPlnInstall* fab;
00325 int ipln_expect = 0;
00326
00327 VldTimeStamp lastInstallDate((time_t)0,0);
00328 PlexPlaneId lastPlaneId;
00329
00330
00331 VldTimeStamp now;
00332 UInt_t year, month, day;
00333 now.GetDate(true,0,&year,&month,&day);
00334 VldTimeStamp today(year,month,day,0,0,0);
00335 VldTimeStamp tomorrow(year,month,day+1,0,0,0);
00336
00337
00338 UInt_t n = fPlnInstallTbl.GetNumRows();
00339 cout << "Total number of entries is: " << n << endl;
00340 cout << "Normal planes " << fFirstNormal << " to " << fLastNormal << endl;
00341
00342
00343
00344
00345 map<PlexPlaneId,const FabPlnInstall*> orderedmap;
00346
00347 for (UInt_t irow=0; irow < n; ++irow) {
00348 fab = fPlnInstallTbl.GetRow(irow);
00349 plnid = fab->GetPlaneId(); plnid.SetIsSteel(kFALSE);
00350 orderedmap[plnid] = fab;
00351 }
00352
00353
00354
00355 map<PlexPlaneId,const FabPlnInstall*>::const_iterator orderedItr;
00356 for (orderedItr = orderedmap.begin();
00357 orderedItr != orderedmap.end();
00358 orderedItr++) {
00359 plnid = orderedItr->first;
00360 fab = orderedItr->second;
00361
00362
00363
00364 if ( ! plnid.IsVetoShield() ) {
00365 if (ipln_expect != plnid.GetPlane()) {
00366 cout << "Was expecting plane " << ipln_expect
00367 << ", instead got " << plnid << endl;
00368 }
00369 }
00370 ipln_expect = plnid.GetPlane()+1;
00371
00372
00373
00374
00375 const VldTimeStamp& thisInstallDate = fab->GetInstallDate();
00376
00377 if (thisInstallDate == tomorrow || thisInstallDate == today)
00378 cout << plnid << " tagged as installed on "
00379 << thisInstallDate.AsString("sql") << "." << endl
00380 << " probably entered as 0000-00-00 in DB table" << endl;
00381 bool out_of_order = false;
00382 switch (plnid.GetDetector()) {
00383 case Detector::kFar:
00384 if ( plnid.IsVetoShield() && ! lastPlaneId.IsVetoShield() )
00385 out_of_order = false;
00386 else
00387 out_of_order = ( thisInstallDate < lastInstallDate );
00388 break;
00389 case Detector::kNear:
00390 out_of_order = ( thisInstallDate > lastInstallDate );
00391 break;
00392 default:
00393 out_of_order = false;
00394 break;
00395 }
00396 if (out_of_order)
00397 cout << "Plane " << plnid << " installed on "
00398 << thisInstallDate.AsString("sql")
00399 << " installed before previous " << endl
00400 << " " << lastPlaneId << " installed on "
00401 << lastInstallDate.AsString("sql") << "." << endl;
00402 lastPlaneId = plnid;
00403 lastInstallDate = thisInstallDate;
00404
00405 uset_t badscint, badsteel;
00406
00407 UInt_t nmdls = 8;
00408 UInt_t nslabs = 1;
00409 if (plnid.GetDetector() == Detector::kFar) {
00410 nslabs = ( plnid.IsVetoShield() ) ? 0 : 8;
00411 }
00412
00413 switch (plnid.GetPlaneCoverage()) {
00414 case PlaneCoverage::kNoActive: nmdls = 0; break;
00415 case PlaneCoverage::kNearPartial: nmdls = 3; break;
00416 case PlaneCoverage::kNearFull: nmdls = 5; break;
00417 case PlaneCoverage::kTotal: nmdls = 8;
00418 if (plnid.GetDetector()==Detector::kCalDet) nmdls = 1;
00419 break;
00420 case PlaneCoverage::kVScN:
00421 case PlaneCoverage::kVSCN:
00422 case PlaneCoverage::kVSeS:
00423 case PlaneCoverage::kVSES:
00424 case PlaneCoverage::kVScS:
00425 case PlaneCoverage::kVSCS:
00426 case PlaneCoverage::kVSeN:
00427 case PlaneCoverage::kVSEN:
00428 nmdls = 1; break;
00429 default:
00430
00431 break;
00432 }
00433
00434
00435
00436
00437 for (UInt_t imdl=0; imdl < 8; ++imdl) {
00438 string mfgid = fab->GetScintMdlMfgId(imdl);
00439
00440 if ( imdl >= nmdls ) {
00441
00442 bool ismissing = (strlen(mfgid.c_str()) == 0 ||
00443 strncmp(mfgid.c_str(),"n/a",3) == 0 );
00444 if (!ismissing) {
00445 nerrors++;
00446 cout << "On plane " << plnid << " mdl " << imdl
00447 << " should be 'n/a' but was '" << mfgid << "' " << endl;
00448 }
00449 continue;
00450 }
00451
00452 bool problem = false;
00453
00454 if (strlen(mfgid.c_str()) < 8) problem = true;
00455 PlexScintMdlId mdlid(plnid,imdl);
00456 const char* asstring = mdlid.AsString();
00457 const char mdltype = toupper(asstring[10]);
00458 if (strncmp(mfgid.c_str(),"CIT",3) == 0 ||
00459 strncmp(mfgid.c_str(),"UMN",3) == 0 ) {
00460 if (mfgid.c_str()[7] != mdltype) problem = true;
00461 }
00462 else problem = true;
00463
00464 if (problem) badscint.insert(imdl);
00465
00466 if (!problem) {
00467
00468 ssetinsert_t inpos = scintset.insert(mfgid);
00469 if (!inpos.second ) {
00470 nerrors++;
00471
00472
00473 cout << "Duplicate scint mfg-id '" << mfgid << "' in "
00474 << mdlid << " (" << imdl << ") previously in "
00475 << WhereIsScintMdl(mfgid) << endl;
00476 }
00477 }
00478 }
00479
00480
00481
00482
00483 for (UInt_t islab=0; islab < 8; ++islab) {
00484 string mfgid = fab->GetSteelSlabMfgId(islab);
00485
00486 if ( islab >= nslabs ) {
00487
00488 bool ismissing = (strlen(mfgid.c_str()) == 0 ||
00489 strncmp(mfgid.c_str(),"n/a",3) == 0 );
00490 if (!ismissing) {
00491 nerrors++;
00492 cout << "On plane " << plnid << " slab " << islab
00493 << " should be 'n/a' but was '" << mfgid << "' " << endl;
00494 }
00495 continue;
00496 }
00497
00498 Int_t nid = -1;
00499 Int_t nscan = sscanf(mfgid.c_str(),"%d",&nid);
00500 bool problem = false;
00501
00502 if (nscan != 1 || nid<100000 || nid>400000 ) problem = true;
00503 if ( problem && (nid>700000 && nid<=799999)) problem = false;
00504
00505 if (problem) badsteel.insert(islab);
00506
00507 if (!problem) {
00508
00509 if (slabPartNums.find(nid) != slabPartNums.end()) {
00510 nerrors++;
00511 cout << "Slab " << plnid <<"," << islab << " has mfg-id '"
00512 << mfgid << "' which is a part #" << endl;
00513 }
00514 else {
00515
00516 ssetinsert_t inpos = slabset.insert(mfgid);
00517 if (!inpos.second ) {
00518 nerrors++;
00519
00520 std::pair<PlexPlaneId,UShort_t> otherloc = WhereIsSteelSlab(mfgid);
00521 cout << "Duplicate slab mfg-id '" << mfgid << "' in "
00522 << plnid << "," << islab << " previously in "
00523 << otherloc.first << "," << otherloc.second << endl;
00524 }
00525 }
00526 }
00527 }
00528
00529 if (! (badscint.empty() && badsteel.empty()) ) {
00530 cout << "Potential problem on " << plnid.AsString("c") << ": " << endl;
00531 usetitr_t scintitr = badscint.begin();
00532 while (scintitr != badscint.end()) {
00533 nerrors++;
00534 PlexScintMdlId mdlid(plnid,*scintitr);
00535 cout << " suspicious scint mdl [" << mdlid.AsString()
00536 << "] mfg-id: '" << fab->GetScintMdlMfgId(*scintitr)
00537 << "'" << endl;
00538 scintitr++;
00539 }
00540 usetitr_t steelitr = badsteel.begin();
00541 while (steelitr != badsteel.end()) {
00542 nerrors++;
00543 cout << " suspicious steel slab [" << *steelitr
00544 << "] mfg-id: '" << fab->GetSteelSlabMfgId(*steelitr)
00545 << "'" << endl;
00546 steelitr++;
00547 }
00548 }
00549
00550 }
00551
00552 cout << "last plane seen was: " << plnid << endl << endl;
00553
00554 return nerrors;
00555 }
00556
00557