Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:27:17

0001 // -*- C++ -*-
0002 //
0003 // Package:     Core
0004 // Class  :     FWOverlapTableManager
0005 //
0006 // Implementation:
0007 //     [Notes on implementation]
0008 //
0009 // Original Author:
0010 //         Created:  Wed Jan  4 20:31:32 CET 2012
0011 //
0012 
0013 // system include files
0014 
0015 // user include files
0016 #include "Fireworks/Core/src/FWOverlapTableManager.h"
0017 #include "Fireworks/Core/src/FWOverlapTableView.h"
0018 #include "Fireworks/Core/src/FWEveDigitSetScalableMarker.h"
0019 #include "Fireworks/Core/interface/FWGeometryTableViewManager.h"
0020 #include "Fireworks/Core/interface/fwLog.h"
0021 
0022 #include "TEveQuadSet.h"
0023 #include "TGeoVolume.h"
0024 #include "TGeoMatrix.h"
0025 #include "TGeoShape.h"
0026 #include "TGeoBBox.h"
0027 #include "TGeoMatrix.h"
0028 #include "TEveUtil.h"
0029 #include "TObjString.h"
0030 #include "TGeoNode.h"
0031 #include "TGeoOverlap.h"
0032 #include "TGeoManager.h"
0033 #include "TPolyMarker3D.h"
0034 
0035 #include "TStopwatch.h"
0036 #include "TTimer.h"
0037 #include "TGeoPainter.h"
0038 #include "TPRegexp.h"
0039 
0040 FWOverlapTableManager::FWOverlapTableManager(FWOverlapTableView* v) : FWGeometryTableManagerBase(), m_browser(v) {}
0041 
0042 FWOverlapTableManager::~FWOverlapTableManager() {}
0043 
0044 std::vector<std::string> FWOverlapTableManager::getTitles() const {
0045   std::vector<std::string> returnValue;
0046   returnValue.reserve(numberOfColumns());
0047 
0048   returnValue.push_back("Name");
0049   returnValue.push_back("Color");
0050   returnValue.push_back("Opcty");
0051   returnValue.push_back("RnrSelf");
0052   returnValue.push_back("RnrChildren");
0053   returnValue.push_back("Overlap");
0054   returnValue.push_back("RnrMarker");
0055   return returnValue;
0056 }
0057 
0058 //---------------------------------------------------------------------------------
0059 void FWOverlapTableManager::importOverlaps(std::string iPath, double iPrecision) {
0060   m_entries.clear();
0061   m_mapNodeOverlaps.clear();
0062   m_browser->getMarker()->Reset(TEveQuadSet::kQT_FreeQuad, kFALSE, 32);
0063 
0064   TEveGeoManagerHolder mangeur(FWGeometryTableViewManager::getGeoMangeur());
0065   // gGeoManager->cd();
0066   NodeInfo topNodeInfo;
0067   topNodeInfo.m_node = gGeoManager->GetTopNode();
0068   topNodeInfo.m_level = 0;
0069   topNodeInfo.m_color = gGeoManager->GetTopNode()->GetVolume()->GetLineColor();
0070   topNodeInfo.m_transparency = gGeoManager->GetTopNode()->GetVolume()->GetTransparency();
0071   topNodeInfo.m_parent = -1;
0072   topNodeInfo.resetBit(kVisNodeSelf);
0073 
0074   m_entries.resize(gGeoManager->GetNNodes());
0075   m_entries[0] = topNodeInfo;
0076 
0077   m_entries.resize(gGeoManager->GetNNodes());
0078 
0079   TGeoVolume* topVol = topNodeInfo.m_node->GetVolume();
0080   Int_t icheck = 0;
0081   Int_t ncheck = 0;
0082   TStopwatch* timer;
0083   Int_t i;
0084   bool checkingOverlaps = false;
0085   TGeoManager* geom = topVol->GetGeoManager();
0086   ncheck = topNodeInfo.m_node->CountDaughters(kFALSE);
0087   timer = new TStopwatch();
0088   geom->ClearOverlaps();
0089   geom->SetCheckingOverlaps(kTRUE);
0090 
0091   int oldS = 0;
0092   timer->Start();
0093   geom->GetGeomPainter()->OpProgress(topVol->GetName(), icheck, ncheck, timer, kFALSE);
0094   //   topVol->CheckOverlaps(iPrecision);
0095   icheck++;
0096   TGeoIterator git(topVol);
0097   Entries_i eit = m_entries.begin();
0098   /*
0099      if (gGeoManager->GetListOfOverlaps()->GetEntriesFast()) {
0100      int newCnt =  gGeoManager->GetListOfOverlaps()->GetEntriesFast();
0101      for (int i=0; i<newCnt; ++i) {
0102      addOverlapEntry((TGeoOverlap*)gGeoManager->GetListOfOverlaps()->At(i), new TGeoHMatrix(*geom->GetCurrentMatrix()), topNode, next); 
0103      }
0104      oldS= newCnt;
0105      }*/
0106   eit++;
0107   TGeoNode* node;
0108   icheck = 1;
0109 
0110   int topNodeIdx = m_browser->getTopNodeIdx();
0111 
0112   while ((node = git())) {
0113     if (!eit->testBit(kOverlap))
0114       eit->resetBit(kVisNodeSelf);
0115     eit->m_node = node;
0116     eit->m_color = node->GetVolume()->GetLineColor();
0117     eit->m_transparency = node->GetVolume()->GetTransparency();
0118     eit->m_level = git.GetLevel();
0119     eit->m_parent = icheck;
0120 
0121     if ((topNodeIdx) == icheck || !topNodeIdx) {
0122       //  printf("start to check overlaps on topNodeIdx %s \n", eit->name());
0123       checkingOverlaps = true;
0124     } else if (checkingOverlaps && (eit->m_level <= m_entries[m_browser->getTopNodeIdx()].m_level)) {
0125       checkingOverlaps = false;
0126     }
0127     // parent index
0128     Entries_i pit = eit;
0129     do {
0130       --pit;
0131       --(eit->m_parent);
0132       if (pit->m_level < eit->m_level)
0133         break;
0134     } while (pit != m_entries.begin());
0135 
0136     // overlap bits
0137     if (checkingOverlaps) {
0138       if (!node->GetVolume()->IsSelected()) {
0139         geom->GetGeomPainter()->OpProgress(node->GetVolume()->GetName(), icheck + 1, ncheck, timer, kFALSE);
0140         node->GetVolume()->SelectVolume(kFALSE);
0141 
0142         node->GetVolume()->CheckOverlaps(iPrecision);
0143 
0144         if (oldS != gGeoManager->GetListOfOverlaps()->GetEntriesFast()) {
0145           // printf("mother %s overlaps \n", node->GetName());
0146 
0147           eit->setBit(kOverlapChild);
0148           eit->setBit(kVisNodeChld);
0149           eit->setBit(kVisMarker);
0150 
0151           TGeoHMatrix* motherm = new TGeoHMatrix(*geom->GetCurrentMatrix());
0152           {
0153             TGeoNode* ni = topNodeInfo.m_node;
0154             for (Int_t i = 1; i <= git.GetLevel(); i++) {
0155               ni = ni->GetDaughter(git.GetIndex(i));
0156               motherm->Multiply(ni->GetMatrix());
0157             }
0158           }
0159 
0160           int newCnt = gGeoManager->GetListOfOverlaps()->GetEntriesFast();
0161 
0162           for (int i = oldS; i < newCnt; ++i) {
0163             //                  printf("add %p %p \n", (void*)node->GetVolume(), (void*)m_entries[icheck].m_node->GetVolume());
0164             addOverlapEntry((TGeoOverlap*)gGeoManager->GetListOfOverlaps()->At(i), i, icheck, motherm);
0165           }
0166 
0167           oldS = newCnt;
0168         }
0169       }
0170     }
0171     eit++;
0172     icheck++;
0173   }
0174 
0175   m_browser->getMarker()->RefitPlex();
0176 
0177   topVol->SelectVolume(kTRUE);
0178   geom->SetCheckingOverlaps(kFALSE);
0179   //   geom->SortOverlaps();
0180   TObjArray* overlaps = geom->GetListOfOverlaps();
0181   Int_t novlps = overlaps->GetEntriesFast();
0182   TNamed* obj;
0183   for (i = 0; i < novlps; i++) {
0184     obj = (TNamed*)overlaps->At(i);
0185     obj->SetName(Form("ov%05d", i));
0186   }
0187   geom->GetGeomPainter()->OpProgress("Check overlaps:", icheck, ncheck, timer, kTRUE);
0188   Info("CheckOverlaps", "Number of illegal overlaps/extrusions : %d\n", novlps);
0189   delete timer;
0190 }
0191 
0192 //______________________________________________________________________________
0193 
0194 void FWOverlapTableManager::addOverlapEntry(TGeoOverlap* ovl, int ovlIdx, Int_t parentIdx, TGeoHMatrix* motherm) {
0195   // printf("add %s \n", ovl->GetTitle());
0196   // get doughter indices of overlaps
0197   /* 
0198       TPMERegexp re(" ", "o");
0199       re.Split(TString(ovl->GetTitle()));
0200       printf("add title %s \n", ovl->GetTitle());
0201    */
0202   int pcnt = parentIdx + 1;
0203   int dOff = 0;
0204   TGeoNode* mothern = m_entries[parentIdx].m_node;
0205 
0206   QuadId* quid = new QuadId(ovl, parentIdx);
0207 
0208   for (int i = 0; i < mothern->GetNdaughters(); ++i) {
0209     TGeoNode* n = mothern->GetDaughter(i);
0210 
0211     int cnt = pcnt + i + dOff;
0212 
0213     if (ovl->IsOverlap()) {
0214       if (n->GetVolume() == ovl->GetFirstVolume() && (*(ovl->GetFirstMatrix()) == *(n->GetMatrix()))) {
0215         // std::string x = re[0].Data();
0216         //if (x.find(n->GetName()) == std::string::npos) printf("ERROT \n");
0217 
0218         m_entries[cnt].setBit(kOverlap);
0219         m_entries[cnt].setBit(kVisNodeSelf);
0220         m_mapNodeOverlaps.insert(std::pair<int, int>(cnt, ovlIdx));
0221         int nno;
0222         n->GetOverlaps(nno);
0223         nno |= BIT(1);
0224         n->SetOverlaps(nullptr, nno);
0225         quid->m_nodes.push_back(cnt);
0226       }
0227     }
0228 
0229     if (n->GetVolume() == ovl->GetSecondVolume() && (*(ovl->GetSecondMatrix()) == *(n->GetMatrix()))) {
0230       //printf("-----------------------------------------------\n");
0231       // std::string x = re[2].Data();
0232       // if (x.find(n->GetName()) == std::string::npos) printf("ERROT \n");
0233 
0234       m_entries[cnt].setBit(kOverlap);
0235       m_entries[cnt].setBit(kVisNodeSelf);
0236 
0237       m_mapNodeOverlaps.insert(std::pair<int, int>(cnt, ovlIdx));
0238       int nno;
0239       n->GetOverlaps(nno);
0240       nno |= (ovl->IsOverlap() ? BIT(1) : BIT(2));
0241       n->SetOverlaps(nullptr, nno);
0242 
0243       quid->m_nodes.push_back(cnt);
0244     }
0245 
0246     FWGeometryTableManagerBase::getNNodesTotal(n, dOff);
0247   }
0248 
0249   TPolyMarker3D* pm = ovl->GetPolyMarker();
0250   for (int j = 0; j < pm->GetN(); ++j) {
0251     double pl[3];
0252     double pg[3];
0253     pm->GetPoint(j, pl[0], pl[1], pl[2]);
0254     motherm->LocalToMaster(pl, pg);
0255 
0256     float dx = TMath::Abs(ovl->GetOverlap());
0257     if (dx > 1e5) {
0258       fwLog(fwlog::kInfo) << Form("WARNING [%s], overlap size = %.1f \n", ovl->GetTitle(), dx);
0259       dx = 10;
0260     }
0261     float dy = dx, dz = 0;
0262     float fp[3];
0263     fp[0] = pg[0];
0264     fp[1] = pg[1];
0265     fp[2] = pg[2];
0266     float bb[12] = {fp[0] + dx,
0267                     fp[1] - dy,
0268                     fp[2] - dz,
0269                     fp[0] + dx,
0270                     fp[1] + dy,
0271                     fp[2] + dz,
0272                     fp[0] - dx,
0273                     fp[1] + dy,
0274                     fp[2] + dz,
0275                     fp[0] - dx,
0276                     fp[1] - dy,
0277                     fp[2] - dz};
0278     m_browser->getMarker()->AddQuad(&bb[0]);
0279     m_browser->getMarker()->QuadId(quid);
0280   }
0281 
0282   int aIdx = parentIdx;
0283   int aLev = m_entries[aIdx].m_level;
0284   int topNodeIdx = m_browser->getTopNodeIdx();
0285 
0286   while (aIdx > topNodeIdx) {
0287     aIdx--;
0288     if (m_entries[aIdx].m_level < aLev) {
0289       m_entries[aIdx].setBit(kOverlapChild);
0290       m_entries[aIdx].setBit(kVisNodeChld);
0291       //  printf("stamp %s \n", m_entries[aIdx].name());
0292       aLev--;
0293     }
0294   }
0295 }
0296 
0297 //_____________________________________________________________________________
0298 
0299 void FWOverlapTableManager::recalculateVisibility() {
0300   // printf("overlap recalcuate vis \n");
0301   m_row_to_index.clear();
0302   int i = m_browser->getTopNodeIdx();
0303   m_row_to_index.push_back(i);
0304 
0305   if (m_entries[i].testBit(kExpanded))
0306     recalculateVisibilityNodeRec(i);
0307 }
0308 
0309 void FWOverlapTableManager::recalculateVisibilityNodeRec(int pIdx) {
0310   TGeoNode* parentNode = m_entries[pIdx].m_node;
0311   int nD = parentNode->GetNdaughters();
0312   int dOff = 0;
0313   for (int n = 0; n != nD; ++n) {
0314     int idx = pIdx + 1 + n + dOff;
0315     NodeInfo& data = m_entries[idx];
0316 
0317     if (m_browser->listAllNodes() || data.testBitAny(kOverlap | kOverlapChild))
0318       m_row_to_index.push_back(idx);
0319 
0320     if ((m_browser->listAllNodes() || data.testBit(kOverlapChild)) && data.testBit(kExpanded))
0321       recalculateVisibilityNodeRec(idx);
0322 
0323     FWGeometryTableManagerBase::getNNodesTotal(parentNode->GetDaughter(n), dOff);
0324   }
0325 }
0326 
0327 //______________________________________________________________________________
0328 
0329 bool FWOverlapTableManager::nodeIsParent(const NodeInfo& data) const {
0330   return m_browser->listAllNodes() ? data.m_node->GetNdaughters() : data.testBit(kOverlapChild);
0331 }
0332 
0333 void FWOverlapTableManager::printOverlaps(int idx) const {
0334   TEveGeoManagerHolder gmgr(FWGeometryTableViewManager::getGeoMangeur());
0335   std::pair<std::multimap<int, int>::const_iterator, std::multimap<int, int>::const_iterator> ppp;
0336   ppp = m_mapNodeOverlaps.equal_range(idx);
0337   for (std::multimap<int, int>::const_iterator it2 = ppp.first; it2 != ppp.second; ++it2) {
0338     const TGeoOverlap* ovl = (const TGeoOverlap*)gGeoManager->GetListOfOverlaps()->At((*it2).second);
0339     if (ovl)
0340       ovl->Print();
0341   }
0342 }
0343 
0344 void FWOverlapTableManager::getOverlapTitles(int idx, TString& txt) const {
0345   TEveGeoManagerHolder gmgr(FWGeometryTableViewManager::getGeoMangeur());
0346   std::pair<std::multimap<int, int>::const_iterator, std::multimap<int, int>::const_iterator> ppp;
0347   ppp = m_mapNodeOverlaps.equal_range(idx);
0348   for (std::multimap<int, int>::const_iterator it2 = ppp.first; it2 != ppp.second; ++it2) {
0349     const TGeoOverlap* ovl = (const TGeoOverlap*)gGeoManager->GetListOfOverlaps()->At((*it2).second);
0350     {
0351       txt += "\n";
0352 
0353       if (ovl) {
0354         txt += Form("%s: %g, ", ovl->IsOverlap() ? "Ovl" : "Extr", ovl->GetOverlap());
0355         txt += ovl->GetTitle();
0356       }
0357     }
0358   }
0359 }
0360 //______________________________________________________________________________
0361 /*
0362   const char* FWOverlapTableManager::cellName(const NodeInfo& data) const
0363   {
0364   if (data.m_parent == -1)
0365   {
0366   int ne = 0;
0367   int no = 0;
0368   TGeoOverlap* ovl;
0369   TEveGeoManagerHolder gmgr( FWGeometryTableViewManager::getGeoMangeur());
0370   TIter next_ovl(gGeoManager->GetListOfOverlaps());
0371   while((ovl = (TGeoOverlap*)next_ovl())) 
0372   ovl->IsOverlap() ? no++ : ne++;
0373      
0374   return Form("%s Ovl[%d] Ext[%d]", data.m_node->GetName(), no, ne);
0375   }
0376   else
0377   {
0378   return data.name();
0379   }
0380   }*/
0381 
0382 //______________________________________________________________________________
0383 
0384 FWTableCellRendererBase* FWOverlapTableManager::cellRenderer(int iSortedRowNumber, int iCol) const {
0385   if (m_row_to_index.empty())
0386     return &m_renderer;
0387 
0388   int unsortedRow = m_row_to_index[iSortedRowNumber];
0389 
0390   if (unsortedRow < 0)
0391     printf("!!!!!!!!!!!!!!!! error %d %d \n", unsortedRow, iSortedRowNumber);
0392 
0393   // editor state
0394   //
0395   m_renderer.showEditor(unsortedRow == m_editTransparencyIdx && iCol == 2);
0396 
0397   // selection state
0398   //
0399   const NodeInfo& data = m_entries[unsortedRow];
0400 
0401   bool isSelected = data.testBit(kHighlighted) || data.testBit(kSelected);
0402   if (m_browser->listAllNodes())
0403     isSelected = isSelected || data.testBit(kOverlap);
0404 
0405   if (data.testBit(kSelected)) {
0406     m_highlightContext->SetBackground(0xc86464);
0407   } else if (data.testBit(kHighlighted)) {
0408     m_highlightContext->SetBackground(0x6464c8);
0409   } else if (m_browser->listAllNodes() && data.testBit(kOverlap)) {
0410     m_highlightContext->SetBackground(0xdddddd);
0411   }
0412 
0413   // set column content
0414   //
0415   if (iCol == 0) {
0416     if (unsortedRow == m_browser->getTopNodeIdx()) {
0417       int no = 0, ne = 0;
0418       TEveGeoManagerHolder gmgr(FWGeometryTableViewManager::getGeoMangeur());
0419       TIter next_ovl(gGeoManager->GetListOfOverlaps());
0420       const TGeoOverlap* ovl;
0421       while ((ovl = (TGeoOverlap*)next_ovl()))
0422         ovl->IsOverlap() ? no++ : ne++;
0423 
0424       m_renderer.setData(Form("%s Ovl[%d] Ext[%d]", data.m_node->GetName(), no, ne), isSelected);
0425     } else {
0426       m_renderer.setData(data.name(), isSelected);
0427     }
0428     m_renderer.setIsParent(nodeIsParent(data));
0429 
0430     m_renderer.setIsOpen(data.testBit(FWGeometryTableManagerBase::kExpanded));
0431 
0432     int level = data.m_level - m_levelOffset;
0433     if (nodeIsParent(data))
0434       m_renderer.setIndentation(20 * level);
0435     else
0436       m_renderer.setIndentation(20 * level + FWTextTreeCellRenderer::iconWidth());
0437   } else {
0438     m_renderer.setIsParent(false);
0439     m_renderer.setIndentation(0);
0440 
0441     if (iCol == 5) {
0442       if (data.testBit(kOverlap)) {
0443         std::string x;
0444         std::pair<std::multimap<int, int>::const_iterator, std::multimap<int, int>::const_iterator> ppp;
0445         ppp = m_mapNodeOverlaps.equal_range(unsortedRow);
0446 
0447         TEveGeoManagerHolder gmgr(FWGeometryTableViewManager::getGeoMangeur());
0448 
0449         for (std::multimap<int, int>::const_iterator it2 = ppp.first; it2 != ppp.second; ++it2) {
0450           const TGeoOverlap* ovl = (const TGeoOverlap*)gGeoManager->GetListOfOverlaps()->At((*it2).second);
0451           if (ovl)
0452             x += Form("%s: %g ", ovl->IsOverlap() ? "Ovl" : "Extr", ovl->GetOverlap());
0453           else
0454             x += "err";
0455         }
0456         m_renderer.setData(x, isSelected);
0457       } else {
0458         m_renderer.setData("", isSelected);
0459       }
0460     }
0461     if (iCol == 1) {
0462       m_colorBoxRenderer.setData(data.m_color, isSelected);
0463       return &m_colorBoxRenderer;
0464     } else if (iCol == 2) {
0465       m_renderer.setData(Form("%d", 100 - data.m_transparency), isSelected);
0466     } else if (iCol == 3) {
0467       m_renderer.setData(data.testBit(kVisNodeSelf) ? "On" : "-", isSelected);
0468 
0469     } else if (iCol == 4) {
0470       m_renderer.setData(data.testBit(kVisNodeChld) ? "On" : "-", isSelected);
0471 
0472     } else if (iCol == 6) {
0473       std::cerr << "This shoud not happen! \n";
0474     }
0475   }
0476   return &m_renderer;
0477 }
0478 
0479 //______________________________________________________________________________
0480 
0481 void FWOverlapTableManager::setDaughtersSelfVisibility(int selectedIdx, bool v) {
0482   int dOff = 0;
0483   TGeoNode* parentNode = m_entries[selectedIdx].m_node;
0484   int nD = parentNode->GetNdaughters();
0485   for (int n = 0; n != nD; ++n) {
0486     int idx = selectedIdx + 1 + n + dOff;
0487     NodeInfo& data = m_entries[idx];
0488 
0489     data.setBitVal(FWGeometryTableManagerBase::kVisNodeChld, v);
0490     data.setBitVal(FWGeometryTableManagerBase::kVisNodeSelf, v);
0491 
0492     FWGeometryTableManagerBase::getNNodesTotal(parentNode->GetDaughter(n), dOff);
0493   }
0494 }