cmssw icon indicating copy to clipboard operation
cmssw copied to clipboard

Use of const_cast in reconstruction geometries (Tracker, MTD)

Open fabiocos opened this issue 1 year ago • 4 comments

During the review of #43407 a static analyser warning was reported (see https://github.com/cms-sw/cmssw/pull/43407#issuecomment-1836279159) :

>> Analyzing  /data/cmsbld/jenkins/workspace/ib-run-pr-tests/CMSSW_14_0_X_2023-11-29-1100/src/Geometry/MTDNumberingBuilder/src/module.cc 
/data/cmsbld/jenkins/workspace/ib-run-pr-tests/CMSSW_14_0_X_2023-11-29-1100/src/Geometry/MTDNumberingBuilder/src/GeometricTimingDet.cc:222:67: warning: const_cast was used, this may result in thread-unsafe code [threadsafety.ConstCast]
  222 |     void operator()(GeometricTimingDet const* det) const { delete const_cast<GeometricTimingDet*>(det); }
      |                                                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 warning generated.
/data/cmsbld/jenkins/workspace/ib-run-pr-tests/CMSSW_14_0_X_2023-11-29-1100/src/Geometry/MTDGeometryBuilder/src/MTDGeometry.cc:110:12: warning: const_cast was used, this may result in thread-unsafe code [threadsafety.ConstCast]
  110 |     delete const_cast<GeomDet*>(d);
      |            ^~~~~~~~~~~~~~~~~~~~~~~
/data/cmsbld/jenkins/workspace/ib-run-pr-tests/CMSSW_14_0_X_2023-11-29-1100/src/Geometry/MTDGeometryBuilder/src/MTDGeometry.cc:112:12: warning: const_cast was used, this may result in thread-unsafe code [threadsafety.ConstCast]
  112 |     delete const_cast<GeomDetType*>(d);
      |            ^~~~~~~~~~~~~~~~~~~~~~~~~~~
/data/cmsbld/jenkins/workspace/ib-run-pr-tests/CMSSW_14_0_X_2023-11-29-1100/src/Geometry/MTDGeometryBuilder/src/MTDGeometry.cc:132:3: warning: const_cast was used, this may result in thread-unsafe code [threadsafety.ConstCast]
  132 |   const_cast<GeomDet*>(p)->setIndex(theDetUnits.size());
      |   ^~~~~~~~~~~~~~~~~~~~~~~
/data/cmsbld/jenkins/workspace/ib-run-pr-tests/CMSSW_14_0_X_2023-11-29-1100/src/Geometry/MTDGeometryBuilder/src/MTDGeometry.cc:141:3: warning: const_cast was used, this may result in thread-unsafe code [threadsafety.ConstCast]
  141 |   const_cast<GeomDet*>(p)->setGdetIndex(theDets.size());
      |   ^~~~~~~~~~~~~~~~~~~~~~~
4 warnings generated.

(...)

>> Analyzing  /data/cmsbld/jenkins/workspace/ib-run-pr-tests/CMSSW_14_0_X_2023-11-29-1100/src/SimFastTiming/FastTimingCommon/src/MTDShapeBase.cc 
/data/cmsbld/jenkins/workspace/ib-run-pr-tests/CMSSW_14_0_X_2023-11-29-1100/src/RecoMTD/DetLayers/src/MTDDetLayerGeometry.cc:173:7: warning: const_cast was used, this may result in thread-unsafe code [threadsafety.ConstCast]
  173 |     (*const_cast<DetLayer*>(l)).setSeqNum(sq++);
      |       ^~~~~~~~~~~~~~~~~~~~~~~~
1 warning generated.

While the delete-related use of const_cast appears removable, the other cases concern updating objects previously declared as const . The patterns in MTD classes are directly derived from the code in Tracker geometry, which was the template originally used. So in case it would make sense to have a common solution. I did not check for further possible instances of the same issue.

A simple, but not cost-free, solution I see would be to internally build a vector of non const objects, update them, and then produce a const copy of this as output of the ESProducer, while deleting the temporary non const version.

Suggestions for alternative smarter solutions are welcome.

fabiocos avatar Dec 05 '23 12:12 fabiocos

A new Issue was created by @fabiocos Fabio Cossutti.

@sextonkennedy, @Dr15Jones, @makortel, @rappoccio, @antoniovilela, @smuzaffar can you please review it and eventually sign/assign? Thanks.

cms-bot commands are listed here

cmsbuild avatar Dec 05 '23 12:12 cmsbuild

assign geometry

@cms-sw/trk-dpg-l2 @cms-sw/mtd-dpg-l2

makortel avatar Dec 05 '23 14:12 makortel

New categories assigned: geometry

@Dr15Jones,@civanch,@bsunanda,@makortel,@mdhildreth you have been requested to review this Pull request/Issue and eventually sign? Thanks

cmsbuild avatar Dec 05 '23 14:12 cmsbuild

@fabiocos , it seems that in run time geometry should be unchanged, so detector description and all access methods should be "const". When geometry description is created the description cannot be "const" - there is no sense to declare it const but after define some parameters. To me a solution should be to have initialisation methods with non-const objects and const run time methods.

civanch avatar Mar 11 '24 12:03 civanch