Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-10-17 22:59:08

0001 #ifndef RecoTracker_MkFitCore_standalone_Event_h
0002 #define RecoTracker_MkFitCore_standalone_Event_h
0003 
0004 #include "RecoTracker/MkFitCore/interface/Config.h"
0005 #include "RecoTracker/MkFitCore/interface/Track.h"
0006 #include "RecoTracker/MkFitCore/interface/BeamSpot.h"
0007 #include "Validation.h"
0008 
0009 #include <mutex>
0010 
0011 namespace mkfit {
0012 
0013   struct DataFile;
0014 
0015   class Event {
0016   public:
0017     explicit Event(int evtID, int nLayers);
0018     Event(Validation &v, int evtID, int nLayers);
0019 
0020     void reset(int evtID);
0021     void validate();
0022     void printStats(const TrackVec &, TrackExtraVec &);
0023 
0024     int evtID() const { return evtID_; }
0025     void resetLayerHitMap(bool resetSimHits);
0026 
0027     void write_out(DataFile &data_file);
0028     void read_in(DataFile &data_file, FILE *in_fp = 0);
0029     int write_tracks(FILE *fp, const TrackVec &tracks);
0030     int read_tracks(FILE *fp, TrackVec &tracks, bool skip_reading = false);
0031 
0032     void setInputFromCMSSW(std::vector<HitVec> hits, TrackVec seeds);
0033 
0034     void kludge_cms_hit_errors();
0035 
0036     int use_seeds_from_cmsswtracks();  //special mode --> use only seeds which generated cmssw reco track
0037     int clean_cms_simtracks();
0038     int clean_cms_seedtracks(
0039         TrackVec *seed_ptr = nullptr);    //operates on seedTracks_; returns the number of cleaned seeds
0040     int clean_cms_seedtracks_badlabel();  //operates on seedTracks_, removes those with label == -1;
0041     void relabel_bad_seedtracks();
0042     void relabel_cmsswtracks_from_seeds();
0043 
0044     int select_tracks_iter(unsigned int n = 0);  //for cmssw input
0045 
0046     void fill_hitmask_bool_vectors(int track_algo, std::vector<std::vector<bool>> &layer_masks);
0047     void fill_hitmask_bool_vectors(std::vector<int> &track_algo_vec, std::vector<std::vector<bool>> &layer_masks);
0048 
0049     void print_tracks(const TrackVec &tracks, bool print_hits) const;
0050 
0051     Validation &validation_;
0052 
0053     // For seed access in deep data dumpers.
0054     struct SimLabelFromHits {
0055       int n_hits = 0, n_match = 0, label = -1;
0056       float good_frac() const { return (float)n_match / n_hits; }
0057       bool is_set() const { return label >= 0; }
0058     };
0059     void setCurrentSeedTracks(const TrackVec &seeds);
0060     const Track &currentSeed(int i) const;
0061     SimLabelFromHits simLabelForCurrentSeed(int i) const;
0062     void resetCurrentSeedTracks();
0063 
0064   private:
0065     int evtID_;
0066     const TrackVec *currentSeedTracks_ = nullptr;
0067     mutable std::vector<SimLabelFromHits> currentSeedSimFromHits_;
0068 
0069   public:
0070     BeamSpot beamSpot_;  // XXXX Read/Write of BeamSpot + file-version bump or extra-section to be added.
0071     std::vector<HitVec> layerHits_;
0072     std::vector<std::vector<uint64_t>> layerHitMasks_;  //aligned with layerHits_
0073     MCHitInfoVec simHitsInfo_;
0074 
0075     TrackVec simTracks_, seedTracks_, candidateTracks_, fitTracks_;
0076     TrackVec cmsswTracks_;
0077     // validation sets these, so needs to be mutable
0078     mutable TrackExtraVec simTracksExtra_, seedTracksExtra_, candidateTracksExtra_, fitTracksExtra_;
0079     mutable TrackExtraVec cmsswTracksExtra_;
0080 
0081     TSVec simTrackStates_;
0082     static std::mutex printmutex;
0083   };
0084 
0085   typedef std::vector<Event> EventVec;
0086 
0087   struct DataFileHeader {
0088     int f_magic = 0xBEEF;
0089     int f_format_version = 7;  //last update with ph2 geom
0090     int f_sizeof_track = sizeof(Track);
0091     int f_sizeof_hit = sizeof(Hit);
0092     int f_sizeof_hot = sizeof(HitOnTrack);
0093     int f_n_layers = -1;
0094     int f_n_events = -1;
0095 
0096     int f_extra_sections = 0;
0097 
0098     DataFileHeader() = default;
0099   };
0100 
0101   struct DataFile {
0102     enum ExtraSection {
0103       ES_SimTrackStates = 0x1,
0104       ES_Seeds = 0x2,
0105       ES_CmsswTracks = 0x4,
0106       ES_HitIterMasks = 0x8,
0107       ES_BeamSpot = 0x10
0108     };
0109 
0110     FILE *f_fp = 0;
0111     long f_pos = sizeof(DataFileHeader);
0112 
0113     DataFileHeader f_header;
0114 
0115     std::mutex f_next_ev_mutex;
0116 
0117     // ----------------------------------------------------------------
0118 
0119     bool hasSimTrackStates() const { return f_header.f_extra_sections & ES_SimTrackStates; }
0120     bool hasSeeds() const { return f_header.f_extra_sections & ES_Seeds; }
0121     bool hasCmsswTracks() const { return f_header.f_extra_sections & ES_CmsswTracks; }
0122     bool hasHitIterMasks() const { return f_header.f_extra_sections & ES_HitIterMasks; }
0123     bool hasBeamSpot() const { return f_header.f_extra_sections & ES_BeamSpot; }
0124 
0125     int openRead(const std::string &fname, int expected_n_layers);
0126     void openWrite(const std::string &fname, int n_layers, int n_ev, int extra_sections = 0);
0127 
0128     void rewind();
0129 
0130     int advancePosToNextEvent(FILE *fp);
0131 
0132     void skipNEvents(int n_to_skip);
0133 
0134     void close();
0135     void CloseWrite(int n_written);  //override nevents in the header and close
0136   };
0137 
0138   void print(std::string pfx, int itrack, const Track &trk, const Event &ev);
0139 
0140 }  // end namespace mkfit
0141 #endif