Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-08-15 02:03:41

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     iItem->getEvent()->getByLabel(edm::InputTag("hgcalMergeLayerClusters", "timeLayerCluster"), TimeValueMapHandle);
0059     std::cerr << __FILE__ << ":" << __LINE__
0060               << " couldn't locate 'hgcalLayerClusters:timeLayerCluster' ValueMap in input file. Trying to access "
0061                  "'hgcalMergeLayerClusters:timeLayerClusters' ValueMap"
0062               << std::endl;
0063     if (!TimeValueMapHandle.isValid()) {
0064       std::cerr << __FILE__ << ":" << __LINE__
0065                 << " couldn't locate 'hgcalMergeLayerClusters:timeLayerCluster' ValueMap in input file." << std::endl;
0066     }
0067   }
0068 
0069   layer = item()->getConfig()->value<long>("Layer");
0070   saturation_energy = item()->getConfig()->value<double>("EnergyCutOff");
0071   heatmap = item()->getConfig()->value<bool>("Heatmap");
0072   z_plus = item()->getConfig()->value<bool>("Z+");
0073   z_minus = item()->getConfig()->value<bool>("Z-");
0074   enableTimeFilter = item()->getConfig()->value<bool>("EnableTimeFilter");
0075 
0076   FWHeatmapProxyBuilderTemplate::build(iItem, product, vc);
0077 }
0078 
0079 void FWCaloClusterProxyBuilder::build(const reco::CaloCluster &iData,
0080                                       unsigned int iIndex,
0081                                       TEveElement &oItemHolder,
0082                                       const FWViewContext *) {
0083   if (enableTimeFilter && TimeValueMapHandle.isValid()) {
0084     const float time = TimeValueMapHandle->get(iIndex).first;
0085     if (time < timeLowerBound || time > timeUpperBound)
0086       return;
0087   }
0088 
0089   std::vector<std::pair<DetId, float>> clusterDetIds = iData.hitsAndFractions();
0090 
0091   bool h_hex(false);
0092   TEveBoxSet *hex_boxset = new TEveBoxSet();
0093   if (!heatmap)
0094     hex_boxset->UseSingleColor();
0095   hex_boxset->SetPickable(true);
0096   hex_boxset->Reset(TEveBoxSet::kBT_Hex, true, 64);
0097   hex_boxset->SetAntiFlick(true);
0098 
0099   bool h_box(false);
0100   TEveBoxSet *boxset = new TEveBoxSet();
0101   if (!heatmap)
0102     boxset->UseSingleColor();
0103   boxset->SetPickable(true);
0104   boxset->Reset(TEveBoxSet::kBT_FreeBox, true, 64);
0105   boxset->SetAntiFlick(true);
0106 
0107   for (std::vector<std::pair<DetId, float>>::iterator it = clusterDetIds.begin(), itEnd = clusterDetIds.end();
0108        it != itEnd;
0109        ++it) {
0110     const uint8_t type = ((it->first >> 28) & 0xF);
0111 
0112     const float *corners = item()->getGeom()->getCorners(it->first);
0113     if (corners == nullptr)
0114       continue;
0115 
0116     // HGCal
0117     if (iData.algo() == reco::CaloCluster::hgcal_em || iData.algo() == reco::CaloCluster::hgcal_had ||
0118         (type >= 8 && type <= 10)) {
0119       if (heatmap && hitmap->find(it->first) == hitmap->end())
0120         continue;
0121 
0122       const bool z = (it->first >> 25) & 0x1;
0123 
0124       // discard everything thats not at the side that we are intersted in
0125       if (((z_plus & z_minus) != 1) && (((z_plus | z_minus) == 0) || !(z == z_minus || z == !z_plus)))
0126         continue;
0127 
0128       const float *parameters = item()->getGeom()->getParameters(it->first);
0129       const float *shapes = item()->getGeom()->getShapePars(it->first);
0130 
0131       if (parameters == nullptr || shapes == nullptr)
0132         continue;
0133 
0134       const int total_points = parameters[0];
0135       const bool isScintillator = (total_points == 4);
0136 
0137       uint8_t ll = layer;
0138       if (layer > 0) {
0139         if (layer > 28) {
0140           if (type == 8) {
0141             continue;
0142           }
0143           ll -= 28;
0144         } else {
0145           if (type != 8) {
0146             continue;
0147           }
0148         }
0149 
0150         if (ll != ((it->first >> (isScintillator ? 17 : 20)) & 0x1F))
0151           continue;
0152       }
0153 
0154       // seed and cluster position
0155       if (iData.seed().rawId() == it->first.rawId()) {
0156         TEveStraightLineSet *marker = new TEveStraightLineSet;
0157         marker->SetLineWidth(1);
0158 
0159         // center of RecHit
0160         const float center[3] = {corners[total_points * 3 + 0],
0161                                  corners[total_points * 3 + 1],
0162                                  corners[total_points * 3 + 2] + shapes[3] * 0.5f};
0163 
0164         // draw 3D cross
0165         const float crossScale = 1.0f + fmin(iData.energy(), 5.0f);
0166         marker->AddLine(center[0] - crossScale, center[1], center[2], center[0] + crossScale, center[1], center[2]);
0167         marker->AddLine(center[0], center[1] - crossScale, center[2], center[0], center[1] + crossScale, center[2]);
0168         marker->AddLine(center[0], center[1], center[2] - crossScale, center[0], center[1], center[2] + crossScale);
0169 
0170         oItemHolder.AddElement(marker);
0171 
0172         TEveStraightLineSet *position_marker = new TEveStraightLineSet;
0173         position_marker->SetLineWidth(2);
0174         position_marker->SetLineColor(kOrange);
0175         auto const &pos = iData.position();
0176         const float position_crossScale = crossScale * 0.5;
0177         position_marker->AddLine(
0178             pos.x() - position_crossScale, pos.y(), pos.z(), pos.x() + position_crossScale, pos.y(), pos.z());
0179         position_marker->AddLine(
0180             pos.x(), pos.y() - position_crossScale, pos.z(), pos.x(), pos.y() + position_crossScale, pos.z());
0181 
0182         oItemHolder.AddElement(position_marker);
0183       }
0184 
0185       const float energy =
0186           fmin((item()->getConfig()->value<bool>("Cluster(0)/RecHit(1)") ? hitmap->at(it->first)->energy()
0187                                                                          : iData.energy()) /
0188                    saturation_energy,
0189                1.0f);
0190       const uint8_t colorFactor = gradient_steps * energy;
0191 
0192       // Scintillator
0193       if (isScintillator) {
0194         const int total_vertices = 3 * total_points;
0195 
0196         std::vector<float> pnts(24);
0197         for (int i = 0; i < total_points; ++i) {
0198           pnts[i * 3 + 0] = corners[i * 3];
0199           pnts[i * 3 + 1] = corners[i * 3 + 1];
0200           pnts[i * 3 + 2] = corners[i * 3 + 2];
0201 
0202           pnts[(i * 3 + 0) + total_vertices] = corners[i * 3];
0203           pnts[(i * 3 + 1) + total_vertices] = corners[i * 3 + 1];
0204           pnts[(i * 3 + 2) + total_vertices] = corners[i * 3 + 2] + shapes[3];
0205         }
0206         boxset->AddBox(&pnts[0]);
0207         if (heatmap) {
0208           energy ? boxset->DigitColor(gradient[0][colorFactor], gradient[1][colorFactor], gradient[2][colorFactor])
0209                  : boxset->DigitColor(64, 64, 64);
0210         }
0211 
0212         h_box = true;
0213       }
0214       // Silicon
0215       else {
0216         constexpr int offset = 9;
0217 
0218         float centerX = (corners[6] + corners[6 + offset]) / 2;
0219         float centerY = (corners[7] + corners[7 + offset]) / 2;
0220         float radius = fabs(corners[6] - corners[6 + offset]) / 2;
0221         hex_boxset->AddHex(TEveVector(centerX, centerY, corners[2]), radius, shapes[2], shapes[3]);
0222         if (heatmap) {
0223           energy ? hex_boxset->DigitColor(gradient[0][colorFactor], gradient[1][colorFactor], gradient[2][colorFactor])
0224                  : hex_boxset->DigitColor(64, 64, 64);
0225         }
0226 
0227         h_hex = true;
0228       }
0229     }
0230     // Not HGCal
0231     else {
0232       h_box = true;
0233 
0234       std::vector<float> pnts(24);
0235       fireworks::energyTower3DCorners(corners, (*it).second, pnts);
0236       boxset->AddBox(&pnts[0]);
0237     }
0238   }
0239 
0240   if (h_hex) {
0241     hex_boxset->RefitPlex();
0242 
0243     hex_boxset->CSCTakeAnyParentAsMaster();
0244     if (!heatmap) {
0245       hex_boxset->CSCApplyMainColorToMatchingChildren();
0246       hex_boxset->CSCApplyMainTransparencyToMatchingChildren();
0247       hex_boxset->SetMainColor(item()->modelInfo(iIndex).displayProperties().color());
0248       hex_boxset->SetMainTransparency(item()->defaultDisplayProperties().transparency());
0249     }
0250     oItemHolder.AddElement(hex_boxset);
0251   }
0252 
0253   if (h_box) {
0254     boxset->RefitPlex();
0255 
0256     boxset->CSCTakeAnyParentAsMaster();
0257     if (!heatmap) {
0258       boxset->CSCApplyMainColorToMatchingChildren();
0259       boxset->CSCApplyMainTransparencyToMatchingChildren();
0260       boxset->SetMainColor(item()->modelInfo(iIndex).displayProperties().color());
0261       boxset->SetMainTransparency(item()->defaultDisplayProperties().transparency());
0262     }
0263     oItemHolder.AddElement(boxset);
0264   }
0265 }
0266 
0267 REGISTER_FWPROXYBUILDER(FWCaloClusterProxyBuilder, reco::CaloCluster, "Calo Cluster", FWViewType::kISpyBit);