NeutralMakers.cpp 28.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*****************************************************************************\
* (c) Copyright 2020 CERN for the benefit of the LHCb Collaboration           *
*                                                                             *
* This software is distributed under the terms of the GNU General Public      *
* Licence version 3 (GPL Version 3), copied verbatim in the file "COPYING".   *
*                                                                             *
* In applying this licence, CERN does not waive the privileges and immunities *
* granted to it by virtue of its status as an Intergovernmental Organization  *
* or submit itself to any jurisdiction.                                       *
\*****************************************************************************/

#include "CaloDet/DeCalorimeter.h"
#include "DetDesc/ConditionAccessorHolder.h"
#include "GaudiAlg/Transformer.h"

#include "Event/Particle.h"
CI Runner's avatar
CI Runner committed
17
#include "Event/Particle_v2.h"
18
19
20
#include "Event/ProtoParticle.h"
#include "Event/RecVertex.h"

21
#include "CaloFutureUtils/CaloMomentum.h"
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "Kernel/IParticlePropertySvc.h"
#include "Kernel/ParticleProperty.h"

// File containing definition AND implementation of neutral makers (photons, pi0s (...))
/** @class PhotonMaker
 *
 * @brief LHCb::Particle creation from LHCb::ProtoParticle neutral objects and PVs
 * It is based on "old" MergedPi0Maker, but respecting thread-safety during execution
 *
 * The main purpose of the algorithm is to create Photon Particles from ProtoParticles.
 * An extra input consisting on PVs is given in order to use it as photon vertex,
 * but it is only used if the "FirstPVasOrigin" Property is set to true
 *
 * List of Gaudi Properties:
 * "FirstPVasOrigin": If True, first PV on the list will used as photon origin, otherwise (0,0,0) is chosen
 * "ClusterCodeMasks": std::map to use custom masks on the clusters. Default (none) is good for most applications.
 * "Particle": ID for this particle. Default behaviour assigns photon ID and mass.
 * "ConfLevelCut": Minimum CL required
 * "PtCut": Minimum PT required
 *
 * <b>Example</b>: Create photon particles from neutral protoparticles and PVs,
 * but not using the first PVs as photon origins
 * @code {.py}
 * myphotonparticles = PhotonMaker(
        InputProtoParticles=my_neutral_protoparticles,
        Particle="gamma",
        InputPrimaryVertices=my_pvs,
        FistPVasOrigin=False)
 * @endcode
 *
 */

/** @class MergedPi0Maker
 *
 * @brief LHCb::Particle Merged Pi0 creation from LHCb::ProtoParticle neutral objects and PVs
 * It is based on "old" MergedPi0Maker, but respecting thread-safety during execution
 *
 * The main purpose of the algorithm is to create Merged Pi0s Particles from ProtoParticles
 * An extra input consisting on PVs is given in order to use it as photon vertex,
 * but it is only used if the "FirstPVasOrigin" Property is set to true
 *
 *
 * List of Gaudi Properties:
 * "FirstPVasOrigin": If True, first PV on the list will used as photon origin, otherwise (0,0,0) is chosen
 * "ClusterCodeMasks": std::map to use custom masks on the clusters. Default (none) is good for most applications.
 * "Particle": ID for this particle. Default behaviour assigns pi0 ID and mass.
 * "ConfLevelCut": Minimum CL required
 * "PtCut": Minimum PT required for the pi0
 * "GammaPtCut": Minimum PT of each photon
 * "GammaGammaDistCut": Maximum photon distance
 * "Chi2Cut": If positive, maximum Chi2
 * "MassWindow": Mass range allowed, centered at the mass of the requested particle
 *
 * <b>Example</b>: Create merged pi0 particles from neutral protoparticles and PVs,
 * but not using the first PVs as pi0 origins
 * @code {.py}
 * mymergedpi0particles = MergedPi0Maker(
        InputProtoParticles=my_neutral_protoparticles,
        Particle="pi0",
        InputPrimaryVertices=my_pvs,
        FistPVasOrigin=False)
 * @endcode
 *
 */

// Anonymous namespace for shared functions, enums of NeutralMakers
namespace {

  // Enumeration for diferent cases of ClusterCodes
  enum class ClusterCodes { Size, Position2nd, Shape, Isolated, None };

  // ====================
  // Index for the functions
  std::optional<int>  ClusterCode( const LHCb::ProtoParticle& proto, ClusterCodes type );
  Gaudi::XYZPoint     GetPoint( LHCb::RecVertices const& PVs, bool const setPV );
  Gaudi::SymMatrix3x3 GetPointErr( LHCb::RecVertices const& PVs, bool const setPV );
  ClusterCodes        to_ClusterCodes( std::string_view type );
  std::map<ClusterCodes, std::pair<double, double>>
100
101
102
103
                 ClusterCodeToEnum( std::map<std::string, std::pair<double, double>> const& clusterMasks, std::string const& AlgName );
  double         confLevelPhoton( const LHCb::ProtoParticle& proto );
  double         confLevelMergedPi0( const LHCb::ProtoParticle& proto );
  LHCb::Particle make_neutral( LHCb::Calo::Momentum& momentum );
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162

  // ====================
  // Get first PV point
  Gaudi::XYZPoint GetPoint( LHCb::RecVertices const& PVs, bool const setPV ) {
    if ( setPV && PVs.size() != 0 ) { return ( *PVs.begin() )->position(); }
    return Gaudi::XYZPoint();
  }

  // ====================
  // Get first PV point error
  Gaudi::SymMatrix3x3 GetPointErr( LHCb::RecVertices const& PVs, bool const setPV ) {
    if ( setPV && PVs.size() != 0 ) { return ( *PVs.begin() )->covMatrix(); }
    return Gaudi::SymMatrix3x3();
  }

  // ====================
  // Get Cluster Code
  std::optional<int> ClusterCode( const LHCb::ProtoParticle& proto, ClusterCodes type ) {
    int code = (int)proto.info( LHCb::ProtoParticle::additionalInfo::CaloClusterCode, 0. );
    int mult = abs( code ) / 10;
    int pos  = abs( code ) - mult * 10;
    int isol = ( code > 0 ) ? 1 : 0;
    int conf = pos % 2;
    switch ( type ) {
    case ClusterCodes::Size:
      return mult;
    case ClusterCodes::Position2nd:
      return pos;
    case ClusterCodes::Shape:
      return conf;
    case ClusterCodes::Isolated:
      return isol;
    default:
      return std::nullopt;
    }
  }

  // ====================
  // Conversion from string to ClusterCode
  ClusterCodes to_ClusterCodes( std::string_view type ) {
    if ( type == "Size" ) return ClusterCodes::Size;
    if ( type == "2ndPosition" ) return ClusterCodes::Position2nd;
    if ( type == "Shape" ) return ClusterCodes::Shape;
    if ( type == "Isolated" ) return ClusterCodes::Isolated;
    return ClusterCodes::None;
  }

  // ====================
  // Construct ClusterMasks using enum instead of strings
  // It simply construct another std::map using now the enum as keys
  std::map<ClusterCodes, std::pair<double, double>>
  ClusterCodeToEnum( std::map<std::string, std::pair<double, double>> const& clusterMasks,
                     std::string const&                                      AlgName ) {
    // Define returning std::map variable
    std::map<ClusterCodes, std::pair<double, double>> clusterMasks_enum;
    // Go through the map and convert strings to ClusterCodes
    for ( const auto& [type, window] : clusterMasks ) {
      auto cc = to_ClusterCodes( type );
      clusterMasks_enum.insert( {cc, window} );
163
      if ( cc == ClusterCodes::None ) Warning( AlgName.c_str(), "Unknown ClusterCode mask '%s'", type.c_str() );
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
    }
    return clusterMasks_enum;
  }

  // ====================
  // To evaluate CL
  double confLevelPhoton( const LHCb::ProtoParticle& proto ) {
    if ( proto.hasInfo( LHCb::ProtoParticle::additionalInfo::IsNotH ) )
      return std::clamp( proto.info( LHCb::ProtoParticle::additionalInfo::IsNotH, 0. ), 0., 1. );
    else
      return -1.0;
  }

  // ====================
  // To evaluate CL
  double confLevelMergedPi0( const LHCb::ProtoParticle& proto ) {
    if ( proto.hasInfo( LHCb::ProtoParticle::additionalInfo::IsPhoton ) )
      return std::clamp( 1 - proto.info( LHCb::ProtoParticle::additionalInfo::IsPhoton, +1. ), 0., 1. );
    else
      return -1.0;
  }
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202

  // ====================
  // Function to replace old CaloParticle class behaviour.
  // Creates neutral LHCb::Particle from CaloMomentum
  // The particle is set as if coming from first PV if requested ((0,0,0) otherwise)
  // Only suitable for non-composite particles (photons, mergedpi0) which is the case here
  LHCb::Particle make_neutral( LHCb::Calo::Momentum& momentum ) {
    // Build particle
    auto particle = LHCb::Particle();
    // Set particle momentum
    particle.setReferencePoint( momentum.referencePoint() );
    particle.setPosCovMatrix( momentum.pointCovMatrix() );
    particle.setMomentum( momentum.momentum() );
    particle.setMomCovMatrix( momentum.momCovMatrix() );
    particle.setPosMomCovMatrix( momentum.momPointCovMatrix() );

    return particle;
  }
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
} // namespace

// Classes are embedded in this namespace
// Other particle makers encouraged to do the same
namespace LHCb::Phys::ParticleMakers {
  // ============================================================
  // PhotonMaker definition
  class PhotonMaker : public Gaudi::Functional::Transformer<LHCb::Particles( LHCb::ProtoParticles const&,
                                                                             LHCb::RecVertices const& )> {
    // ====================
    // List of variables
  private:
    // ParticleProperty
    ServiceHandle<LHCb::IParticlePropertySvc> m_particlePropertySvc{
        this, "ParticlePropertySvc", "LHCb::ParticlePropertySvc", "To get neutral particle properties"};
    const LHCb::ParticleProperty* m_partProp;

    // Map using enum
    std::map<ClusterCodes, std::pair<double, double>> m_clusterMasks_enum;

    // Gaudi properties
    Gaudi::Property<bool> m_setPV{this, "FirstPVasOrigin", false};
    // Map of cluster masks (no mask by default)
    Gaudi::Property<std::map<std::string, std::pair<double, double>>> m_clusterMasks{
        this,
        "ClusterCodeMasks",
        {},
        [=]( const auto& ) { this->m_clusterMasks_enum = ClusterCodeToEnum( this->m_clusterMasks.value(), name() ); },
        Gaudi::Details::Property::ImmediatelyInvokeHandler{true}};
    // Id (string) of the particle
    Gaudi::Property<std::string> m_part{this, "Particle", "gamma"};
    // Filters
    Gaudi::Property<double> m_clCut{this, "ConfLevelCut", -99};
    Gaudi::Property<double> m_ptCut{this, "PtCut", 150.0};

    // Counters
    // Total of CL
    mutable Gaudi::Accumulators::StatCounter<double> m_confidenceLevelCounter{this, "Confidence Level"};
    // Total and selected photons
    mutable Gaudi::Accumulators::Counter<>     m_PhotonsCounter{this, "Created photons"};
    mutable Gaudi::Accumulators::StatCounter<> m_SelPhotonsCounter{this, "Selected photons"};
    mutable Gaudi::Accumulators::Counter<>     m_SkipPhotonsCounter{this, "Skipped photons"};
    // Count Errors
    mutable Gaudi::Accumulators::MsgCounter<MSG::ERROR> m_invalid_calomom{this, "Invalid CaloMomentum status"};
    mutable Gaudi::Accumulators::MsgCounter<MSG::ERROR> m_invalid_energy{this, "Negative energies are not allowed"};
    mutable Gaudi::Accumulators::MsgCounter<MSG::ERROR> m_not_neutral{this, "Track(s) found. Particle must be neutral"};
    mutable Gaudi::Accumulators::MsgCounter<MSG::ERROR> m_empty_hypotheses{this, "No hypotheses found"};

    // ====================
    // List of functions
  public:
    PhotonMaker( const std::string& name, ISvcLocator* pSvcLocator );

    StatusCode initialize() override;

    LHCb::Particles operator()( LHCb::ProtoParticles const& protos, LHCb::RecVertices const& PVs ) const override;

  private:
    // Transformer of proto photon into a photon
    std::optional<LHCb::Particle> make_photon( const LHCb::ProtoParticle& proto, Gaudi::XYZPoint const& point,
                                               Gaudi::SymMatrix3x3 const& pointErr ) const;

    // Debug printing
    void print_debug() const;
  };

  // PhotonMaker implementation
  DECLARE_COMPONENT( LHCb::Phys::ParticleMakers::PhotonMaker )
  PhotonMaker::PhotonMaker( const std::string& name, ISvcLocator* pSvcLocator )
      : Transformer( name, pSvcLocator,
                     {KeyValue{"InputProtoParticles", LHCb::ProtoParticleLocation::Neutrals},
                      KeyValue{"InputPrimaryVertices", LHCb::RecVertexLocation::Primary}},
                     {KeyValue{"Particles", ""}} ) {}

  // ===============================
  // Initialize algorithm
  StatusCode PhotonMaker::initialize() {
    return Transformer::initialize().andThen( [&] { m_partProp = m_particlePropertySvc->find( m_part.value() ); } );
  }

  // ===============================
  // Main execution
  LHCb::Particles PhotonMaker::operator()( LHCb::ProtoParticles const& protos, LHCb::RecVertices const& PVs ) const {

    LHCb::Particles particles;

    // Loop over protos
    for ( const LHCb::ProtoParticle* proto : protos ) {
      auto particle = make_photon( *proto, GetPoint( PVs, m_setPV.value() ), GetPointErr( PVs, m_setPV.value() ) );
      // Add particle if built correctly
      if ( !particle.has_value() ) { continue; }
      particles.add( new LHCb::Particle( std::move( particle ).value() ) );
    }

    // Add to stats of sel photons
    m_SelPhotonsCounter += particles.size();
    // === debug
    if ( msgLevel( MSG::DEBUG ) ) { print_debug(); }

    return particles;
  }

  // ====================
  // Part to build the photon from its protoparticle
  // It is separated from operator() to keep it as clean as possible
  std::optional<LHCb::Particle> PhotonMaker::make_photon( const LHCb::ProtoParticle& proto,
                                                          Gaudi::XYZPoint const&     point,
                                                          Gaudi::SymMatrix3x3 const& pointErr ) const {
    //  ---- skip invalid and charged
    if ( proto.track() ) {
      ++m_not_neutral;
      return {};
    }

    // ---- Check the hypothesis
    const auto& hypos = proto.calo();
    if ( hypos.empty() ) {
      ++m_empty_hypotheses;
      return {};
    }

    //  ---- Check the hypothesis. Nothing wrong if it's not matched to a photon, just skip
    const auto& hypo = hypos.front();
    if ( LHCb::CaloHypo::Hypothesis::Photon != hypo->hypothesis() ) { return {}; }

    // ---- skip negative energy
    if ( hypo->e() <= 0 ) {
      ++m_invalid_energy;
      return {};
    }

    // Add to total photons
    ++m_PhotonsCounter;

    // ---- apply mask on ClusterCode
    if ( m_clusterMasks_enum.size() != 0 ) {
      for ( const auto& [type, window] : m_clusterMasks_enum ) {
        std::optional<int> code = ClusterCode( proto, type );
        if ( code.has_value() && ( code.value() < (int)window.first || code.value() > (int)window.second ) ) return {};
      }
    }

    // == evaluate kinematical properties
346
    LHCb::Calo::Momentum momentum = LHCb::Calo::Momentum( &proto, point, pointErr );
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
    if ( momentum.status() ) {
      ++m_invalid_calomom;
      return {};
    }
    double pT = momentum.pt();
    double E  = momentum.e();
    double px = momentum.px();
    double py = momentum.py();
    double pz = momentum.pz();
    double p  = E;

    if ( m_partProp->mass() > 0 ) {
      p = std::sqrt( E * E - m_partProp->mass() * m_partProp->mass() );
      px *= ( p / E );
      py *= ( p / E );
      pz *= ( p / E );
      pT *= ( p / E );
    }
    if ( pT < m_ptCut ) { return {}; }

    // ---- apply CL filter (must be after pT cut to match neutralID definition range)
    double CL = confLevelPhoton( proto );
    if ( CL < m_clCut ) return {};
    m_confidenceLevelCounter += CL;

372
373
374
375
376
377
    // === set photon parameters (4-momentum, vertex and correlations)
    auto particle = std::make_optional<LHCb::Particle>( make_neutral( momentum ) );

    // Warning : covariant matrix should be modified accordingly -> to be included in CaloMomentum ...
    if ( m_partProp->mass() > 0 ) particle->setMomentum( Gaudi::LorentzVector( px, py, pz, E ) );

378
379
380
381
382
383
    // ===== create new particle and fill it
    particle->setParticleID( LHCb::ParticleID( m_partProp->pdgID().pid() ) );
    particle->setProto( &proto );

    // === set  mass and mass uncertainties
    particle->setMeasuredMass( m_partProp->mass() );
384
    particle->setMeasuredMassErr( 0 ); // the mass error is EXACTLY zero!
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546

    // === set confidence level
    particle->setConfLevel( CL );

    // === printout
    if ( msgLevel( MSG::VERBOSE ) ) {
      verbose() << "----- Single " << m_part << " found" << endmsg;
      verbose() << "Pt : " << momentum.pt() << endmsg;
      verbose() << "CL : " << CL << endmsg;
      verbose() << "Chi2 " << proto.info( LHCb::ProtoParticle::additionalInfo::CaloTrMatch, -999. ) << endmsg;
      verbose() << "CaloDeposit : " << proto.info( LHCb::ProtoParticle::additionalInfo::CaloDepositID, -999. )
                << endmsg;
      if ( m_partProp->mass() > 0. ) verbose() << " Mass : " << m_partProp->mass() << endmsg;
      verbose() << " " << endmsg;
    }

    return particle;
  }

  // ====================
  // Debug printing
  void PhotonMaker::print_debug() const {
    debug() << " " << endmsg;
    debug() << "-----------------------" << endmsg;
    debug() << " Filtered and created :" << endmsg;
    debug() << " --> " << m_SelPhotonsCounter.nEntries() << " photons (among " << m_PhotonsCounter.nEntries() << ")"
            << endmsg;
    debug() << " Skipped " << m_part.value() << " : " << m_SkipPhotonsCounter.nEntries() << endmsg;
    debug() << "-----------------------" << endmsg;
  }

  // ============================================================
  // MergedPi0Maker definition
  class MergedPi0Maker
      : public Gaudi::Functional::Transformer<LHCb::Particles( LHCb::ProtoParticles const&, LHCb::RecVertices const&,
                                                               DeCalorimeter const& ),
                                              LHCb::DetDesc::usesConditions<DeCalorimeter>> {
    // ====================
    // List of variables
  private:
    // ParticleProperty
    ServiceHandle<LHCb::IParticlePropertySvc> m_particlePropertySvc{
        this, "ParticlePropertySvc", "LHCb::ParticlePropertySvc", "To get neutral particle properties"};
    const LHCb::ParticleProperty* m_partProp;

    // Map using enum
    std::map<ClusterCodes, std::pair<double, double>> m_clusterMasks_enum;

    // Gaudi properties
    Gaudi::Property<bool> m_setPV{this, "FirstPVasOrigin", false};
    // Map of cluster masks (no mask by default)
    Gaudi::Property<std::map<std::string, std::pair<double, double>>> m_clusterMasks{
        this,
        "ClusterCodeMasks",
        {},
        [=]( const auto& ) { this->m_clusterMasks_enum = ClusterCodeToEnum( this->m_clusterMasks.value(), name() ); },
        Gaudi::Details::Property::ImmediatelyInvokeHandler{true}};
    // Id (string) of the particle
    Gaudi::Property<std::string> m_part{this, "Particle", "pi0"};
    // Filters
    Gaudi::Property<double> m_clCut{this, "ConfLevelCut", -99};
    Gaudi::Property<double> m_ptCut{this, "PtCut", 150.0};
    Gaudi::Property<double> m_gPtCut{this, "GammaPtCut", 0. * Gaudi::Units::MeV};
    Gaudi::Property<double> m_ggDistCut{this, "GammaGammaDistCut", 2.8};
    Gaudi::Property<double> m_chi2Cut{this, "Chi2Cut", 1.};
    Gaudi::Property<double> m_MassWin{this, "MassWindow", 30. * Gaudi::Units::MeV};

    // Counters
    // Total of CL
    mutable Gaudi::Accumulators::StatCounter<double> m_confidenceLevelCounter_pi0{this, "Confidence Level pi0s"};
    // Total and selected pi0s
    mutable Gaudi::Accumulators::Counter<>     m_Pi0sCounter{this, "Created pi0s"};
    mutable Gaudi::Accumulators::StatCounter<> m_SelPi0sCounter{this, "Selected pi0s"};
    mutable Gaudi::Accumulators::Counter<>     m_SkipPi0sCounter{this, "Skipped pi0s"};
    // Count Errors
    mutable Gaudi::Accumulators::MsgCounter<MSG::ERROR> m_invalid_calomom{this, "Invalid CaloMomentum status"};
    mutable Gaudi::Accumulators::MsgCounter<MSG::ERROR> m_not_neutral{this, "Track(s) found. Particle must be neutral"};
    mutable Gaudi::Accumulators::MsgCounter<MSG::ERROR> m_empty_hypotheses{this, "No hypotheses found"};
    mutable Gaudi::Accumulators::MsgCounter<MSG::ERROR> m_null_caloposition{this, "CaloPosition point to null"};

    // ====================
    // List of functions
  public:
    MergedPi0Maker( const std::string& name, ISvcLocator* pSvcLocator );

    StatusCode initialize() override;

    LHCb::Particles operator()( LHCb::ProtoParticles const& protos, LHCb::RecVertices const& PVs,
                                DeCalorimeter const& DECalo ) const override;

  private:
    // Transformer of proto neutral into a pi0
    std::optional<LHCb::Particle> make_pi0( const LHCb::ProtoParticle& proto, const DeCalorimeter& DECalo,
                                            Gaudi::XYZPoint const& point, Gaudi::SymMatrix3x3 const& pointErr ) const;

    // Debug printing
    void print_debug() const;
  }; // namespace LHCb::Phys::ParticleMakers

  // Merged Pi0 implementation
  DECLARE_COMPONENT( LHCb::Phys::ParticleMakers::MergedPi0Maker )
  MergedPi0Maker::MergedPi0Maker( const std::string& name, ISvcLocator* pSvcLocator )
      : Transformer( name, pSvcLocator,
                     {KeyValue{"InputProtoParticles", LHCb::ProtoParticleLocation::Neutrals},
                      KeyValue{"InputPrimaryVertices", LHCb::RecVertexLocation::Primary},
                      KeyValue{"DECalorimeter", DeCalorimeterLocation::Ecal}},
                     {KeyValue{"Particles", ""}} ) {}

  // ====================
  // Initialize algorithm
  StatusCode MergedPi0Maker::initialize() {
    return Transformer::initialize().andThen( [&] { m_partProp = m_particlePropertySvc->find( m_part.value() ); } );
  }
  // ====================
  // Main execution
  LHCb::Particles MergedPi0Maker::operator()( LHCb::ProtoParticles const& protos, LHCb::RecVertices const& PVs,
                                              DeCalorimeter const& DECalo ) const {

    LHCb::Particles particles;

    // Loop over protos
    for ( const LHCb::ProtoParticle* proto : protos ) {
      auto particle = make_pi0( *proto, DECalo, GetPoint( PVs, m_setPV.value() ), GetPointErr( PVs, m_setPV.value() ) );
      // Add particle if built correctly
      if ( !particle.has_value() ) { continue; }
      particles.add( new LHCb::Particle( std::move( particle ).value() ) );
    }
    // Add to stats of sel photons
    m_SelPi0sCounter += particles.size();

    // === debug
    if ( msgLevel( MSG::DEBUG ) ) { print_debug(); }

    return particles;
  }

  // ====================
  // Part to build one pi0 from its protoparticle
  std::optional<LHCb::Particle> MergedPi0Maker::make_pi0( const LHCb::ProtoParticle& proto, DeCalorimeter const& DECalo,
                                                          Gaudi::XYZPoint const&     point,
                                                          Gaudi::SymMatrix3x3 const& pointErr ) const {
    // ---- skip invalid and charged
    if ( proto.track() ) {
      ++m_not_neutral;
      return {};
    }

    // ---- Check the hypothesis
    const auto& hypos = proto.calo();
    if ( hypos.empty() ) {
      ++m_empty_hypotheses;
      return {};
    }

    //  ---- Check the hypothesis. Nothing wrong if it's not matched to a photon, just skip
    const auto& hypo = hypos.front();
    if ( LHCb::CaloHypo::Hypothesis::Pi0Merged != hypo->hypothesis() ) { return {}; }

    // Add to total pi0s
    ++m_Pi0sCounter;

    // Filters
547
    LHCb::Calo::Momentum pi0Momentum( &proto, point, pointErr );
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
    if ( pi0Momentum.status() ) {
      ++m_invalid_calomom;
      return {};
    }

    // ---- apply mass window
    double part_mass = pi0Momentum.mass();
    if ( m_MassWin.value() < fabs( m_partProp->mass() - part_mass ) ) return {};

    // ---- apply Pt(pi0) cut
    if ( m_ptCut > pi0Momentum.pt() ) return {};

    // ---- apply chi2(Tr,cluster) cut
    const double chi2 = proto.info( LHCb::ProtoParticle::additionalInfo::CaloTrMatch, +1.e+06 );
    if ( m_chi2Cut >= 0 && chi2 < m_chi2Cut ) return {};

    // ---- apply mask on ClusterCode
    if ( m_clusterMasks_enum.size() != 0 ) {
      for ( const auto& [type, window] : m_clusterMasks_enum ) {
        std::optional<int> code = ClusterCode( proto, type );
        if ( code.has_value() && ( code.value() < (int)window.first || code.value() > (int)window.second ) ) return {};
      }
    }

    // == extract SplitPhotons hypos
573
574
575
576
577
    const auto&          ghypos = hypo->hypos();
    const auto&          g1     = ghypos.front();
    const auto&          g2     = ghypos.at( 1 );
    LHCb::Calo::Momentum g1Momentum( g1, point, pointErr );
    LHCb::Calo::Momentum g2Momentum( g2, point, pointErr );
578
579
580
581
582
583
584
585
586
587
588
589
590
    // info()  << hypos.size() << "    -> " << g1Momentum.pt() << " " << g2Momentum.pt() << endmsg;

    // ---- Apply SplitPhoton pT cut
    if ( m_gPtCut > g1Momentum.pt() ) return {};
    if ( m_gPtCut > g2Momentum.pt() ) return {};

    // Gamma-Gamma Min distance
    // retrieve cellID by position
    // (WARNING USE g1 split photon 'position')
    const LHCb::CaloPosition* hypoPos = g1->position();
    if ( !hypoPos ) ++m_null_caloposition;
    const Gaudi::XYZPoint hypoPoint( hypoPos->x(), hypoPos->y(), hypoPos->z() );

591
592
593
594
595
    LHCb::Calo::CellID cellID   = DECalo.Cell( hypoPoint );
    double             CellSize = DECalo.cellSize( cellID );
    double             zpos     = DECalo.cellZ( cellID );
    double             epi0     = pi0Momentum.e();
    double             dmin     = ( epi0 * CellSize > 0 ) ? zpos * 2. * m_partProp->mass() / epi0 / CellSize
596
597
598
599
600
601
602
603
                                          : +9999.; // rare FPE ( hypo outside Calo acceptance ?)
    if ( m_ggDistCut < dmin ) return {};

    // ---- apply CL filter (must be after pT cut to match neutralID definition range)
    double CL = confLevelMergedPi0( proto );
    if ( m_clCut >= 0 && CL < m_clCut ) return {};
    m_confidenceLevelCounter_pi0 += CL;

604
605
606
    // === set MergedPi0 parameters in particle(4-momentum, vertex and correlations)
    auto particle = std::make_optional<LHCb::Particle>( make_neutral( pi0Momentum ) );

607
608
609
610
611
612
613
614
    particle->setParticleID( LHCb::ParticleID( m_partProp->pdgID().pid() ) );
    particle->setProto( &proto );

    // --- set confidence level
    particle->setConfLevel( CL );

    //-- set mass and mass uncertainties
    particle->setMeasuredMass( part_mass );
615
    particle->setMeasuredMassErr( pi0Momentum.emass() );
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637

    // === printout
    if ( msgLevel( MSG::VERBOSE ) ) {
      verbose() << " ---- Merged " << m_part << " found [" << m_SelPi0sCounter.nEntries() << "]" << endmsg;
      verbose() << "Pt    " << pi0Momentum.pt() << endmsg;
      verbose() << "CL / Chi2  " << CL << " / " << chi2 << endmsg;
      verbose() << "dist(gg)" << dmin << endmsg;
    }

    return particle;
  }

  // ====================
  // Debug printing
  void MergedPi0Maker::print_debug() const {
    debug() << " " << endmsg;
    debug() << "-----------------------" << endmsg;
    debug() << " Filtered and created :" << endmsg;
    debug() << " --> " << m_SelPi0sCounter << " Merged " << m_part << " (among " << m_Pi0sCounter << ")" << endmsg;
    debug() << " Skipped : " << m_SkipPi0sCounter << endmsg;
    debug() << "--------------------" << endmsg;
  }
CI Runner's avatar
CI Runner committed
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679

  using neutral_basics_maker_output_t =
      std::tuple<LHCb::Event::NeutralBasics, std::unique_ptr<LHCb::Event::CaloHypothesesWithDirection>>;

  /** @class NeutralBasicsMaker
   *
   * @brief Create a container of neutral objects from the output of the reconstruction sequence and a set of vertices
   *
   *
   */
  class NeutralBasicsMaker : public Gaudi::Functional::Transformer<neutral_basics_maker_output_t(
                                 LHCb::UniqueIDGenerator const&, LHCb::Event::Calo::v2::Hypotheses const&,
                                 LHCb::Event::v2::RecVertices const& )> {

  public:
    NeutralBasicsMaker( const std::string& name, ISvcLocator* pSvcLocator )
        : Transformer( name, pSvcLocator,
                       {KeyValue{"InputUniqueIDGenerator", LHCb::UniqueIDGeneratorLocation::Default},
                        KeyValue{"InputCaloObjects", LHCb::Event::Calo::v2::HypothesesLocation::Default},
                        KeyValue{"InputPrimaryVertices", LHCb::Event::v2::RecVertexLocation::Primary}},
                       {KeyValue{"OutputParticles", ""}} ) {}

    neutral_basics_maker_output_t operator()( LHCb::UniqueIDGenerator const&           unique_id_gen,
                                              LHCb::Event::Calo::v2::Hypotheses const& calo_hypos,
                                              LHCb::Event::v2::RecVertices const& primary_vertices ) const override {

      auto zn                        = Zipping::generateZipIdentifier();
      auto calo_hypos_with_direction = std::make_unique<LHCb::Event::CaloHypothesesWithDirection>( unique_id_gen, zn );

      calo_hypos_with_direction->reserve( calo_hypos.size() * primary_vertices.size() );

      // here we are creating one hypothesis per primary vertex
      for ( auto const& ch : calo_hypos )
        for ( auto const& vtx : primary_vertices ) calo_hypos_with_direction->emplace_back( unique_id_gen, ch, vtx );

      return {LHCb::Pr::make_zip( std::as_const( *calo_hypos_with_direction ) ),
              std::move( calo_hypos_with_direction )};
    }
  };

  DECLARE_COMPONENT( NeutralBasicsMaker )

680
} // namespace LHCb::Phys::ParticleMakers