00001
00002
00003
00004
00005
00006
00007
00008
00009
00011
00012 #include "UgliGeometry/UgliDbiTables.h"
00013
00014 #include "Plex/PlexPlaneId.h"
00015 #include "Conventions/Munits.h"
00016 #include "TMath.h"
00017
00018 #include "MessageService/MsgService.h"
00019 CVSID("$Id: UgliDbiTables.cxx,v 1.15 2008/01/09 18:06:17 rhatcher Exp $");
00020
00021 #include "TSystem.h"
00022 #include "TStopwatch.h"
00023
00024 #include <set>
00025 #include <fstream>
00026 using namespace std;
00027
00028 ClassImp(UgliDbiTables)
00029
00030
00031
00032
00033 map<Detector::Detector_t,Bool_t> UgliDbiTables::fgAlgorithmic;
00034 map<Detector::Detector_t,Bool_t> UgliDbiTables::fgCutOnPlnInstall;
00035
00036
00037 void UgliDbiTablesInitDefaults()
00038 {
00039 static bool first = true;
00040 if (!first) return;
00041 first = false;
00042
00043 bool algdflt = false;
00044 if (gSystem->Getenv("ALG_UGLI")) {
00045 MSG("Ugli",Msg::kInfo)
00046 << "UgliDbiTables::InitDefaults saw ALG_UGLI"
00047 << endl;
00048 algdflt = true;
00049 }
00050
00051 UgliDbiTables::fgAlgorithmic[Detector::kUnknown ] = algdflt;
00052 UgliDbiTables::fgAlgorithmic[Detector::kNear ] = algdflt;
00053 UgliDbiTables::fgAlgorithmic[Detector::kFar ] = algdflt;
00054 UgliDbiTables::fgAlgorithmic[Detector::kCalDet ] = algdflt;
00055 UgliDbiTables::fgAlgorithmic[Detector::kTestStand] = algdflt;
00056 UgliDbiTables::fgAlgorithmic[Detector::kMapper ] = algdflt;
00057
00058 bool cutdflt = false;
00059 if (gSystem->Getenv("CUT_UGLI")) {
00060 MSG("Ugli",Msg::kInfo)
00061 << "UgliDbiTables::InitDefaults saw CUT_UGLI"
00062 << endl;
00063 cutdflt = true;
00064 }
00065
00066 UgliDbiTables::fgCutOnPlnInstall[Detector::kUnknown ] = cutdflt;
00067 UgliDbiTables::fgCutOnPlnInstall[Detector::kNear ] = cutdflt;
00068 UgliDbiTables::fgCutOnPlnInstall[Detector::kFar ] = cutdflt;
00069 UgliDbiTables::fgCutOnPlnInstall[Detector::kCalDet ] = cutdflt;
00070 UgliDbiTables::fgCutOnPlnInstall[Detector::kTestStand] = cutdflt;
00071 UgliDbiTables::fgCutOnPlnInstall[Detector::kMapper ] = cutdflt;
00072
00073 }
00074
00075
00076 void UgliDbiTables::SetAlgorithmic(Detector::Detector_t det, Bool_t flag)
00077 {
00078
00079
00080
00081
00082 UgliDbiTablesInitDefaults();
00083 if (!flag) fgAlgorithmic[det] = false;
00084 else {
00085 bool implemented = false;
00086 switch (det) {
00087 case Detector::kNear: break;
00088 case Detector::kFar: implemented = true; break;
00089 case Detector::kCalDet: implemented = true; break;
00090 default: break;
00091 }
00092 if (implemented) fgAlgorithmic[det] = true;
00093 else
00094 MSG("Ugli",Msg::kWarning)
00095 << "UgliDbiTables::SetAlgorithmic - unable to generate "
00096 << Detector::AsString(det) << " geometry "
00097 << endl;
00098 }
00099 }
00100
00101 Bool_t UgliDbiTables::IsAlgorithmic(Detector::Detector_t det)
00102 { UgliDbiTablesInitDefaults(); return fgAlgorithmic[det]; }
00103
00104
00105 void UgliDbiTables::SetCutOnPlnInstall(Detector::Detector_t det,
00106 Bool_t flag)
00107 {
00108
00109
00110
00111 UgliDbiTablesInitDefaults();
00112 fgCutOnPlnInstall[det] = flag;
00113 }
00114
00115 Bool_t UgliDbiTables::IsCutOnPlnInstall(Detector::Detector_t det)
00116 { UgliDbiTablesInitDefaults(); return fgCutOnPlnInstall[det]; }
00117
00118
00119
00120 UgliDbiTables::UgliDbiTables(const VldContext& vldc) :
00121 fAlgorithmic(false), fVldContext(vldc),
00122 fDetector(vldc.GetDetector()),
00123 fGeomTbl(vldc), fSteelTbl(vldc),
00124 fScintPlnStructTbl(vldc), fScintPlnTbl(vldc),
00125 fScintMdlStructTbl(vldc), fScintMdlTbl(vldc),
00126 fStripStructTbl(vldc), fStripTbl(vldc)
00127 {
00128
00129 MSG("Ugli",Msg::kDebug) << "UgliDbiTables normal vldc ctor" << endl;
00130 }
00131
00132
00133
00134 UgliDbiTables::UgliDbiTables(const VldContext& vldc, bool ) :
00135 fAlgorithmic(true), fVldContext(vldc), fDetector(vldc.GetDetector()),
00136 fScintPlnStructTbl(vldc),
00137 fScintMdlStructTbl(vldc),
00138 fStripStructTbl(vldc)
00139 {
00140
00141
00142 MSG("Ugli",Msg::kDebug) << "UgliDbiTables empty ctor" << endl;
00143 }
00144
00145
00146
00147 UgliDbiTables::~UgliDbiTables()
00148 {
00149
00150 MSG("Ugli",Msg::kDebug) << "UgliDbiTables default dtor" << endl;
00151 }
00152
00153
00154
00155 const UgliDbiGeometry* UgliDbiTables::GetDbiGeometry() const
00156 {
00157
00158
00159
00160 if (!fAlgorithmic) {
00161 int nrows = fGeomTbl.GetNumRows();
00162 if (nrows != 1) {
00163 MSG("Ugli",Msg::kError)
00164 << "UgliDbiGeometry unexpectedly had "
00165 << nrows << " rows " << endl
00166 << " for VldContext: " << fVldContext << endl;
00167 if (nrows < 1) return 0;
00168 }
00169 return fGeomTbl.GetRow(0);
00170 }
00171
00172
00173
00174 static UgliDbiGeometry* geo = 0;
00175 if (geo) { delete geo; geo = 0; }
00176
00177 const Float_t near_min[] = {-4.358,-2.591,-49.07};
00178 const Float_t near_max[] = { 4.786, 2.591, 19.63};
00179 const Float_t far_min[] = {-6.9, -5.8, -0.75};
00180 const Float_t far_max[] = { 6.9, 5.8, 81.55};
00181 const Float_t caldet_min[] = {-1.5, -1.5, 0.0};
00182 const Float_t caldet_max[] = { 1.5, 1.5, 14.0};
00183
00184 switch (fDetector) {
00185 case Detector::kNear:
00186 geo = new UgliDbiGeometry(Detector::kNear,false,near_min,near_max);
00187 break;
00188 case Detector::kFar:
00189 geo = new UgliDbiGeometry(Detector::kFar,false,far_min,far_max);
00190 break;
00191 case Detector::kCalDet:
00192 geo = new UgliDbiGeometry(Detector::kCalDet,true,caldet_min,caldet_max);
00193 break;
00194 default:
00195 MSG("Ugli",Msg::kFatal)
00196 << "no algorithmic UgliDbiGeometry for " << fVldContext << endl;
00197 break;
00198 }
00199 return geo;
00200 }
00201
00202
00203 unsigned int UgliDbiTables::GetNumSteelRows() const
00204 {
00205
00206
00207 #ifdef LIMITPLNS
00208 unsigned int nmax = 4;
00209 MSG("Ugli",Msg::kError)
00210 << " **************** force " << nmax << " planes " << endl;
00211 return nmax;
00212 #endif
00213
00214
00215
00216
00217 if (!fAlgorithmic) {
00218 int nrows = fSteelTbl.GetNumRows();
00219 if (nrows < 1) {
00220 MSG("Ugli",Msg::kError)
00221 << "UgliDbiSteelPln unexpectedly had "
00222 << nrows << " rows " << endl;
00223 if (nrows < 1) return 0;
00224 }
00225 return nrows;
00226 }
00227
00228
00229
00230 switch (fDetector) {
00231 case Detector::kNear: return PlexPlaneId::LastPlaneNearSpect()+1;
00232 case Detector::kFar: return PlexPlaneId::LastPlaneFarSM1()+1;
00233 case Detector::kCalDet: return 5*12 + 4 + 1;
00234 default:
00235 MSG("Ugli",Msg::kFatal)
00236 << "no algorithmic GetNumSteelRows for " << fVldContext << endl;
00237 }
00238 return 0;
00239 }
00240
00241
00242 const UgliDbiSteelPln* UgliDbiTables::GetDbiSteelPln(UInt_t irow) const
00243 {
00244
00245
00246
00247 if (!fAlgorithmic) return fSteelTbl.GetRow(irow);
00248
00249
00250
00251 return GetDbiSteelPlnByIndex(irow);
00252 }
00253
00254
00255 const UgliDbiSteelPln* UgliDbiTables::GetDbiSteelPlnByIndex(UInt_t ipln) const
00256 {
00257
00258
00259
00260 if (!fAlgorithmic) return fSteelTbl.GetRowByIndex(ipln);
00261
00262
00263
00264 static UgliDbiSteelPln* steelRow = 0;
00265 if (steelRow) { delete steelRow; steelRow = 0; }
00266
00267 PlexPlaneId plnid(fDetector,ipln,true);
00268 const float steelthick = 2.54 * Munits::cm;
00269 const float steelpitch = 5.94 * Munits::cm;
00270 const float pi = TMath::Pi();
00271 const float piby2 = 0.5*TMath::Pi();
00272 const float pi3by2 = 1.5*TMath::Pi();
00273
00274 switch (fDetector) {
00275
00276
00277
00278 case Detector::kFar:
00279 {
00280 int melt = 1000120;
00281 float totalz = steelthick +
00282 ((PlaneCoverage::kNoActive==plnid.GetPlaneCoverage()) ?
00283 0.0020 : 0.0130);
00284 const float zback0 = -0.0320 + steelpitch;
00285
00286 float zback = zback0 + (float)(ipln)*steelpitch;
00287 if ( ipln > PlexPlaneId::LastPlaneFarSM0() )
00288 zback += 1.5 * Munits::m;
00289 const float piby2 = 0.5*TMath::Pi();
00290 const float x0=0.0, y0=0.0;
00291 const float thetaX=piby2, phiX=0, thetaY=piby2, phiY=piby2,
00292 thetaZ=0, phiZ=0;
00293
00294 steelRow = new UgliDbiSteelPln(plnid,melt,steelthick,
00295 totalz,x0,y0,zback,
00296 thetaX,phiX,thetaY,phiY,thetaZ,phiZ);
00297 }
00298 break;
00299 case Detector::kCalDet:
00300 {
00301 int melt = 0;
00302 float thick = steelthick;
00303 float totalz = 3.84 * Munits::cm;
00304 float x0=0, y0=0;
00305 float zback = (8.68 * Munits::cm) + (float)ipln*steelpitch;
00306 float thetaX=piby2, phiX=0, thetaY=piby2, phiY=piby2, thetaZ=0, phiZ=0;
00307
00308 if (ipln >= 61) {
00309 thick = 0;
00310 totalz = 1.101 * Munits::cm;
00311 if (ipln&1) y0 = -1.600 * Munits::m;
00312 else y0 = -1.611 * Munits::m;
00313
00314 const float zbcosmic[] = { 0.5055, 1.5055, 2.0695, 3.0695};
00315 zback = zbcosmic[ipln-61];
00316 thetaX=pi; phiX=0; thetaY=piby2; phiY=0; thetaZ=piby2; phiZ=pi3by2;
00317 }
00318 steelRow = new UgliDbiSteelPln(plnid,melt,steelthick,
00319 totalz,x0,y0,zback,
00320 thetaX,phiX,thetaY,phiY,thetaZ,phiZ);
00321 }
00322 break;
00323 default:
00324 break;
00325 }
00326 if (!steelRow)
00327 MSG("Ugli",Msg::kFatal)
00328 << "no algorithmic UgliDbiSteelPln for " << fVldContext << endl;
00329
00330 return steelRow;
00331 }
00332
00333
00334 const UgliDbiScintPln* UgliDbiTables::GetDbiScintPlnByIndex(UInt_t ipln) const
00335 {
00336
00337
00338
00339 if (!fAlgorithmic) return fScintPlnTbl.GetRowByIndex(ipln);
00340
00341
00342
00343 static UgliDbiScintPln* scintPlnRow = 0;
00344 if (scintPlnRow) { delete scintPlnRow; scintPlnRow = 0; }
00345
00346 PlexPlaneId plnid(fDetector,ipln,false);
00347 const float scintthick = 1.1 * Munits::cm;
00348 const float nominalgap = 2.0 * Munits::mm;
00349
00350 const float piby4 = 0.25*TMath::Pi();
00351 const float pi3by2 = 1.5*TMath::Pi();
00352
00353 const float vrot = piby4;
00354 const float urot = -vrot;
00355
00356 switch (fDetector) {
00357
00358
00359
00360 case Detector::kFar:
00361 {
00362 float gap = nominalgap;
00363 const float x0=0.0, y0=0.0;
00364 float zrot = (PlaneView::kV==plnid.GetPlaneView()) ? vrot : urot ;
00365
00366 scintPlnRow = new UgliDbiScintPln(plnid,scintthick,gap,x0,y0,zrot);
00367 }
00368 break;
00369 case Detector::kCalDet:
00370 {
00371 float gap = nominalgap;
00372 const float x0=0.0, y0=0.0;
00373 float zrot = (ipln&1) ? pi3by2 : 0.0;
00374 if (ipln >= 61 ) {
00375 gap = 0.0;
00376 zrot = 0;
00377 }
00378 scintPlnRow = new UgliDbiScintPln(plnid,scintthick,gap,x0,y0,zrot);
00379 }
00380 break;
00381 default:
00382 break;
00383 }
00384 if (!scintPlnRow)
00385 MSG("Ugli",Msg::kFatal)
00386 << "no algorithmic UgliDbiScintPln for " << fVldContext << endl;
00387
00388 return scintPlnRow;
00389 }
00390
00391
00392 const UgliDbiScintMdl*
00393 UgliDbiTables::GetDbiScintMdlById(const PlexScintMdlId& mdlid) const
00394 {
00395 int mdl_indx = UgliDbiScintMdl::HashToIndex(mdlid);
00396
00397
00398
00399 if (!fAlgorithmic) return fScintMdlTbl.GetRowByIndex(mdl_indx);
00400
00401
00402
00403 static UgliDbiScintMdl* scintMdlRow = 0;
00404 if (scintMdlRow) { delete scintMdlRow; scintMdlRow = 0; }
00405
00406 unsigned int ipln = mdlid.GetPlane();
00407 unsigned int imdl = mdlid.GetModule();
00408
00409 switch (fDetector) {
00410
00411
00412
00413 case Detector::kFar:
00414 {
00415
00416
00417 const float farMdlWidth[8] =
00418 { 1.153, 1.153, 0.825, 0.825, 0.825, 0.825, 1.153, 1.153 };
00419 const float farMdlTPos[8] =
00420 { -3.378, -2.225, -1.237, -0.412, 0.412, 1.237, 2.225, 3.378 };
00421
00422 float width = farMdlWidth[imdl];
00423 float tpos = farMdlTPos[imdl];
00424 const float lpos = 0.0;
00425 const float zrot = 0.0;
00426
00427
00428 float clearE=3, clearW=3, wlsE=0, wlsW=0;
00429
00430 scintMdlRow = new UgliDbiScintMdl(mdlid,width,tpos,lpos,zrot,
00431 clearE,clearW,wlsE,wlsW);
00432 }
00433 break;
00434 case Detector::kCalDet:
00435 {
00436 const float width = 0.9835 * Munits::m;
00437 const float tpos=0.0, lpos=0.0, zrot=0.0;
00438 float clearE=0, clearW=6, wlsE=4, wlsW=0;
00439 if (ipln & 1) { clearE=6; clearW=0; wlsE=0; wlsW=4; }
00440 if (ipln >= 61 ) { clearE=6; clearW=6; wlsE=0; wlsW=0; }
00441 scintMdlRow = new UgliDbiScintMdl(mdlid,width,tpos,lpos,zrot,
00442 clearE,clearW,wlsE,wlsW);
00443 }
00444 break;
00445 default:
00446 break;
00447 }
00448 if (!scintMdlRow)
00449 MSG("Ugli",Msg::kFatal)
00450 << "no algorithmic UgliDbiScintMdl for " << fVldContext << endl;
00451
00452 return scintMdlRow;
00453 }
00454
00455
00456 const UgliDbiStrip*
00457 UgliDbiTables::GetDbiStripById(const PlexStripEndId& seid) const
00458 {
00459 int strip_indx = UgliDbiStrip::HashToIndex(seid);
00460
00461
00462
00463 if (!fAlgorithmic) return fStripTbl.GetRowByIndex(strip_indx);
00464
00465
00466
00467 static UgliDbiStrip* stripRow = 0;
00468 if (stripRow) { delete stripRow; stripRow = 0; }
00469
00470
00471 unsigned int istrip = seid.GetStrip();
00472 const float strip_width = 4.1 * Munits::cm;
00473 const float lpos = 0.0;
00474 const float zrot = 0.0;
00475
00476 switch (fDetector) {
00477
00478
00479
00480 case Detector::kFar:
00481 {
00482 const int nstrips_mdl[] = { 28, 28, 20, 20, 20, 20, 28, 28};
00483
00484
00485 PlexScintMdlId mdlid = seid.GetScintMdlId();
00486 int stripinmdl = seid.GetStripInMdl();
00487
00488 int nstrips = nstrips_mdl[mdlid.GetModule()];
00489 float toff = ((float)(nstrips)/2 - 0.5);
00490 float tpos = ((float)stripinmdl - toff)*strip_width;
00491
00492 stripRow = new UgliDbiStrip(seid,tpos,lpos,zrot);
00493
00494 }
00495 break;
00496 case Detector::kCalDet:
00497 {
00498 const int nstrips=24;
00499 const float toff = ((float)(nstrips)/2 - 0.5);
00500 float tpos = ((float)istrip - toff)*strip_width;
00501 stripRow = new UgliDbiStrip(seid,tpos,lpos,zrot);
00502 }
00503 break;
00504 default:
00505 break;
00506 }
00507 if (!stripRow)
00508 MSG("Ugli",Msg::kFatal)
00509 << "no algorithmic UgliDbiStrip for " << fVldContext << endl;
00510
00511 return stripRow;
00512 }
00513
00514
00515
00516 static int gVetoOnly = 0;
00517
00518 template<class T>
00519 bool alwaysTrue(const T* ) { return true; }
00520
00521 template<class T>
00522 bool vetoPln(const T* dbirow )
00523 {
00524 if ( gVetoOnly == 0 ) return true;
00525 bool isVeto = dbirow->GetPlaneId().IsVetoShield();
00526 bool keep = true;
00527 if ( gVetoOnly > 0 && ! isVeto ) keep = false;
00528 if ( gVetoOnly < 0 && isVeto ) keep = false;
00529
00530
00531 return keep;
00532 }
00533
00534 template<class T>
00535 bool vetoMdl(const T* dbirow )
00536 {
00537 if ( gVetoOnly == 0 ) return true;
00538 bool isVeto = dbirow->GetScintMdlId().IsVetoShield();
00539 bool keep = true;
00540 if ( gVetoOnly > 0 && ! isVeto ) keep = false;
00541 if ( gVetoOnly < 0 && isVeto ) keep = false;
00542
00543
00544 return keep;
00545 }
00546
00547 template<class T>
00548 bool vetoStrip(const T* dbirow )
00549 {
00550 if ( gVetoOnly == 0 ) return true;
00551 bool isVeto = dbirow->GetStripEndId().IsVetoShield();
00552 bool keep = true;
00553 if ( gVetoOnly > 0 && ! isVeto ) keep = false;
00554 if ( gVetoOnly < 0 && isVeto ) keep = false;
00555
00556
00557 return keep;
00558 }
00559
00560
00561
00562 template<class T>
00563 void WriteTableToCSVFile(const DbiResultPtr<T>& tbl,
00564 string basename,
00565 bool testFunc(const T*) )
00566 {
00567
00568
00569 string fnamedata = basename + ".csv";
00570 string fnamevld = basename + "VLD.csv";
00571
00572 cout << " ---------------- Write " << fnamedata << endl;
00573 TStopwatch timer;
00574 timer.Start();
00575
00576 ofstream datafile(fnamedata.c_str());
00577 ofstream vldfile(fnamevld.c_str());
00578
00579 string vldheaderline = "SEQNO INT PRIMARY KEY,TIMESTART DATETIME,TIMEEND DATETIME,DETECTORMASK TINYINT,SIMMASK TINYINT,TASK INT,AGGREGATENO INT,CREATIONDATE DATETIME,INSERTDATE DATETIME";
00580 vldfile << vldheaderline << endl;
00581
00582 bool doheader = true;
00583 std::set<const DbiValidityRec*> usedvld;
00584 for (UInt_t irow=0; irow < tbl.GetNumRows(); ++irow) {
00585 const T* uglirow = tbl.GetRow(irow);
00586 if ( ! uglirow ) {
00587 cout << "Empty row???" << endl;
00588 continue;
00589 }
00590
00591 if ( ! testFunc(uglirow) ) continue;
00592
00593 const DbiValidityRec* vldrec = tbl.GetValidityRec(uglirow);
00594 const char* opt = (doheader)? "H,":",";
00595 uglirow->FormatToOStream(datafile,opt,vldrec);
00596 doheader = false;
00597 if ( usedvld.find(vldrec) == usedvld.end() ) {
00598 VldRange vldr = vldrec->GetVldRange();
00599 vldfile
00600 << vldrec->GetSeqNo() << ','
00601 << '"' << vldr.GetTimeStart().AsString("sql") << "\","
00602 << '"' << vldr.GetTimeEnd().AsString("sql") << "\","
00603 << vldr.GetDetectorMask() << ','
00604 << vldr.GetSimMask() << ','
00605 << vldrec->GetTask() << ','
00606 << vldrec->GetAggregateNo() << ','
00607 << '"' << vldrec->GetCreationDate().AsString("sql") << "\","
00608 << '"' << vldrec->GetInsertDate().AsString("sql") << '"'
00609 << endl;
00610 usedvld.insert(vldrec);
00611 }
00612 }
00613
00614 timer.Stop();
00615 timer.Print();
00616
00617 }
00618
00619
00620
00621 void UgliDbiTables::WriteCSVFiles(int vetoOnly ) const
00622 {
00623
00624
00625
00626 gVetoOnly = vetoOnly;
00627
00628 WriteTableToCSVFile(fGeomTbl,"UGLIDBIGEOMETRY",alwaysTrue);
00629
00630 WriteTableToCSVFile(fSteelTbl,"UGLIDBISTEELPLN",vetoPln);
00631
00632 WriteTableToCSVFile(fScintPlnStructTbl,"UGLIDBISCINTPLNSTRUCT",alwaysTrue);
00633 WriteTableToCSVFile(fScintPlnTbl,"UGLIDBISCINTPLN",vetoPln);
00634
00635 WriteTableToCSVFile(fScintMdlStructTbl,"UGLIDBISCINTMDLSTRUCT",alwaysTrue);
00636 WriteTableToCSVFile(fScintMdlTbl,"UGLIDBISCINTMDL",vetoMdl);
00637
00638 WriteTableToCSVFile(fStripStructTbl,"UGLIDBISTRIPSTRUCT",alwaysTrue);
00639 WriteTableToCSVFile(fStripTbl,"UGLIDBISTRIP",vetoStrip);
00640
00641 }
00642
00643