Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-05-26 22:39:30

0001 #include "Fireworks/Calo/interface/FWHeatmapProxyBuilderTemplate.h"
0002 #include "Fireworks/Core/interface/FWEventItem.h"
0003 #include "Fireworks/Core/interface/FWGeometry.h"
0004 #include "Fireworks/Core/interface/BuilderUtils.h"
0005 #include "DataFormats/CaloRecHit/interface/CaloClusterFwd.h"
0006 #include "DataFormats/Common/interface/ValueMap.h"
0007 
0008 #include "TEveBoxSet.h"
0009 #include "TEveStraightLineSet.h"
0010 
0011 class FWCaloClusterProxyBuilder : public FWHeatmapProxyBuilderTemplate<reco::CaloCluster> {
0012 public:
0013   FWCaloClusterProxyBuilder(void) {}
0014   ~FWCaloClusterProxyBuilder(void) override {}
0015 
0016   REGISTER_PROXYBUILDER_METHODS();
0017 
0018   FWCaloClusterProxyBuilder(const FWCaloClusterProxyBuilder &) = delete;                   // stop default
0019   const FWCaloClusterProxyBuilder &operator=(const FWCaloClusterProxyBuilder &) = delete;  // stop default
0020 
0021 private:
0022   edm::Handle<edm::ValueMap<std::pair<float, float>>> TimeValueMapHandle;
0023   double timeLowerBound, timeUpperBound;
0024   long layer;
0025   double saturation_energy;
0026   bool heatmap;
0027   bool z_plus;
0028   bool z_minus;
0029   bool enableTimeFilter;
0030 
0031   void setItem(const FWEventItem *iItem) override;
0032 
0033   void build(const FWEventItem *iItem, TEveElementList *product, const FWViewContext *vc) override;
0034   void build(const reco::CaloCluster &iData,
0035              unsigned int iIndex,
0036              TEveElement &oItemHolder,
0037              const FWViewContext *) override;
0038 };
0039 
0040 void FWCaloClusterProxyBuilder::setItem(const FWEventItem *iItem) {
0041   FWHeatmapProxyBuilderTemplate::setItem(iItem);
0042   if (iItem) {
0043     iItem->getConfig()->assertParam("Cluster(0)/RecHit(1)", false);
0044     iItem->getConfig()->assertParam("EnableTimeFilter", false);
0045     iItem->getConfig()->assertParam("TimeLowerBound(ns)", 0.01, 0.0, 75.0);
0046     iItem->getConfig()->assertParam("TimeUpperBound(ns)", 0.01, 0.0, 75.0);
0047   }
0048 }
0049 
0050 void FWCaloClusterProxyBuilder::build(const FWEventItem *iItem, TEveElementList *product, const FWViewContext *vc) {
0051   iItem->getEvent()->getByLabel(edm::InputTag("hgcalLayerClusters", "timeLayerCluster"), TimeValueMapHandle);
0052   if (TimeValueMapHandle.isValid()) {
0053     timeLowerBound = std::min(item()->getConfig()->value<double>("TimeLowerBound(ns)"),
0054                               item()->getConfig()->value<double>("TimeUpperBound(ns)"));
0055     timeUpperBound = std::max(item()->getConfig()->value<double>("TimeLowerBound(ns)"),
0056                               item()->getConfig()->value<double>("TimeUpperBound(ns)"));
0057   } else {
0058     std::cerr << "Warning: couldn't locate 'timeLayerCluster' ValueMap in root file." << std::endl;
0059   }
0060 
0061   layer = item()->getConfig()->value<long>("Layer");
0062   saturation_energy = item()->getConfig()->value<double>("EnergyCutOff");
0063   heatmap = item()->getConfig()->value<bool>("Heatmap");
0064   z_plus = item()->getConfig()->value<bool>("Z+");
0065   z_minus = item()->getConfig()->value<bool>("Z-");
0066   enableTimeFilter = item()->getConfig()->value<bool>("EnableTimeFilter");
0067 
0068   FWHeatmapProxyBuilderTemplate::build(iItem, product, vc);
0069 }
0070 
0071 void FWCaloClusterProxyBuilder::build(const reco::CaloCluster &iData,
0072                                       unsigned int iIndex,
0073                                       TEveElement &oItemHolder,
0074                                       const FWViewContext *) {
0075   if (enableTimeFilter && TimeValueMapHandle.isValid()) {
0076     const float time = TimeValueMapHandle->get(iIndex).first;
0077     if (time < timeLowerBound || time > timeUpperBound)
0078       return;
0079   }
0080 
0081   std::vector<std::pair<DetId, float>> clusterDetIds = iData.hitsAndFractions();
0082 
0083   bool h_hex(false);
0084   TEveBoxSet *hex_boxset = new TEveBoxSet();
0085   if (!heatmap)
0086     hex_boxset->UseSingleColor();
0087   hex_boxset->SetPickable(true);
0088   hex_boxset->Reset(TEveBoxSet::kBT_Hex, true, 64);
0089   hex_boxset->SetAntiFlick(true);
0090 
0091   bool h_box(false);
0092   TEveBoxSet *boxset = new TEveBoxSet();
0093   if (!heatmap)
0094     boxset->UseSingleColor();
0095   boxset->SetPickable(true);
0096   boxset->Reset(TEveBoxSet::kBT_FreeBox, true, 64);
0097   boxset->SetAntiFlick(true);
0098 
0099   for (std::vector<std::pair<DetId, float>>::iterator it = clusterDetIds.begin(), itEnd = clusterDetIds.end();
0100        it != itEnd;
0101        ++it) {
0102     const uint8_t type = ((it->first >> 28) & 0xF);
0103 
0104     const float *corners = item()->getGeom()->getCorners(it->first);
0105     if (corners == nullptr)
0106       continue;
0107 
0108     // HGCal
0109     if (iData.algo() == 8 || (type >= 8 && type <= 10)) {
0110       if (heatmap && hitmap->find(it->first) == hitmap->end())
0111         continue;
0112 
0113       const bool z = (it->first >> 25) & 0x1;
0114 
0115       // discard everything thats not at the side that we are intersted in
0116       if (((z_plus & z_minus) != 1) && (((z_plus | z_minus) == 0) || !(z == z_minus || z == !z_plus)))
0117         continue;
0118 
0119       const float *parameters = item()->getGeom()->getParameters(it->first);
0120       const float *shapes = item()->getGeom()->getShapePars(it->first);
0121 
0122       if (parameters == nullptr || shapes == nullptr)
0123         continue;
0124 
0125       const int total_points = parameters[0];
0126       const bool isScintillator = (total_points == 4);
0127 
0128       uint8_t ll = layer;
0129       if (layer > 0) {
0130         if (layer > 28) {
0131           if (type == 8) {
0132             continue;
0133           }
0134           ll -= 28;
0135         } else {
0136           if (type != 8) {
0137             continue;
0138           }
0139         }
0140 
0141         if (ll != ((it->first >> (isScintillator ? 17 : 20)) & 0x1F))
0142           continue;
0143       }
0144 
0145       // seed and cluster position
0146       if (iData.seed().rawId() == it->first.rawId()) {
0147         TEveStraightLineSet *marker = new TEveStraightLineSet;
0148         marker->SetLineWidth(1);
0149 
0150         // center of RecHit
0151         const float center[3] = {corners[total_points * 3 + 0],
0152                                  corners[total_points * 3 + 1],
0153                                  corners[total_points * 3 + 2] + shapes[3] * 0.5f};
0154 
0155         // draw 3D cross
0156         const float crossScale = 1.0f + fmin(iData.energy(), 5.0f);
0157         marker->AddLine(center[0] - crossScale, center[1], center[2], center[0] + crossScale, center[1], center[2]);
0158         marker->AddLine(center[0], center[1] - crossScale, center[2], center[0], center[1] + crossScale, center[2]);
0159         marker->AddLine(center[0], center[1], center[2] - crossScale, center[0], center[1], center[2] + crossScale);
0160 
0161         oItemHolder.AddElement(marker);
0162 
0163         TEveStraightLineSet *position_marker = new TEveStraightLineSet;
0164         position_marker->SetLineWidth(2);
0165         position_marker->SetLineColor(kOrange);
0166         auto const &pos = iData.position();
0167         const float position_crossScale = crossScale * 0.5;
0168         position_marker->AddLine(
0169             pos.x() - position_crossScale, pos.y(), pos.z(), pos.x() + position_crossScale, pos.y(), pos.z());
0170         position_marker->AddLine(
0171             pos.x(), pos.y() - position_crossScale, pos.z(), pos.x(), pos.y() + position_crossScale, pos.z());
0172 
0173         oItemHolder.AddElement(position_marker);
0174       }
0175 
0176       const float energy =
0177           fmin((item()->getConfig()->value<bool>("Cluster(0)/RecHit(1)") ? hitmap->at(it->first)->energy()
0178                                                                          : iData.energy()) /
0179                    saturation_energy,
0180                1.0f);
0181       const uint8_t colorFactor = gradient_steps * energy;
0182 
0183       // Scintillator
0184       if (isScintillator) {
0185         const int total_vertices = 3 * total_points;
0186 
0187         std::vector<float> pnts(24);
0188         for (int i = 0; i < total_points; ++i) {
0189           pnts[i * 3 + 0] = corners[i * 3];
0190           pnts[i * 3 + 1] = corners[i * 3 + 1];
0191           pnts[i * 3 + 2] = corners[i * 3 + 2];
0192 
0193           pnts[(i * 3 + 0) + total_vertices] = corners[i * 3];
0194           pnts[(i * 3 + 1) + total_vertices] = corners[i * 3 + 1];
0195           pnts[(i * 3 + 2) + total_vertices] = corners[i * 3 + 2] + shapes[3];
0196         }
0197         boxset->AddBox(&pnts[0]);
0198         if (heatmap) {
0199           energy ? boxset->DigitColor(gradient[0][colorFactor], gradient[1][colorFactor], gradient[2][colorFactor])
0200                  : boxset->DigitColor(64, 64, 64);
0201         }
0202 
0203         h_box = true;
0204       }
0205       // Silicon
0206       else {
0207         constexpr int offset = 9;
0208 
0209         float centerX = (corners[6] + corners[6 + offset]) / 2;
0210         float centerY = (corners[7] + corners[7 + offset]) / 2;
0211         float radius = fabs(corners[6] - corners[6 + offset]) / 2;
0212         hex_boxset->AddHex(TEveVector(centerX, centerY, corners[2]), radius, shapes[2], shapes[3]);
0213         if (heatmap) {
0214           energy ? hex_boxset->DigitColor(gradient[0][colorFactor], gradient[1][colorFactor], gradient[2][colorFactor])
0215                  : hex_boxset->DigitColor(64, 64, 64);
0216         }
0217 
0218         h_hex = true;
0219       }
0220     }
0221     // Not HGCal
0222     else {
0223       h_box = true;
0224 
0225       std::vector<float> pnts(24);
0226       fireworks::energyTower3DCorners(corners, (*it).second, pnts);
0227       boxset->AddBox(&pnts[0]);
0228     }
0229   }
0230 
0231   if (h_hex) {
0232     hex_boxset->RefitPlex();
0233 
0234     hex_boxset->CSCTakeAnyParentAsMaster();
0235     if (!heatmap) {
0236       hex_boxset->CSCApplyMainColorToMatchingChildren();
0237       hex_boxset->CSCApplyMainTransparencyToMatchingChildren();
0238       hex_boxset->SetMainColor(item()->modelInfo(iIndex).displayProperties().color());
0239       hex_boxset->SetMainTransparency(item()->defaultDisplayProperties().transparency());
0240     }
0241     oItemHolder.AddElement(hex_boxset);
0242   }
0243 
0244   if (h_box) {
0245     boxset->RefitPlex();
0246 
0247     boxset->CSCTakeAnyParentAsMaster();
0248     if (!heatmap) {
0249       boxset->CSCApplyMainColorToMatchingChildren();
0250       boxset->CSCApplyMainTransparencyToMatchingChildren();
0251       boxset->SetMainColor(item()->modelInfo(iIndex).displayProperties().color());
0252       boxset->SetMainTransparency(item()->defaultDisplayProperties().transparency());
0253     }
0254     oItemHolder.AddElement(boxset);
0255   }
0256 }
0257 
0258 REGISTER_FWPROXYBUILDER(FWCaloClusterProxyBuilder, reco::CaloCluster, "Calo Cluster", FWViewType::kISpyBit);