Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:10:29

0001 /****************************************************************************
0002  *
0003  * This is a part of the TOTEM offline software.
0004  * Authors: 
0005  *   Maciej Wróbel (wroblisko@gmail.com)
0006  *   Jan Kašpar (jan.kaspar@gmail.com)
0007  *
0008  ****************************************************************************/
0009 
0010 #ifndef EventFilter_CTPPSRawToDigi_CounterChecker
0011 #define EventFilter_CTPPSRawToDigi_CounterChecker
0012 
0013 #include <map>
0014 #include <string>
0015 #include <vector>
0016 #include <iostream>
0017 
0018 #include "CondFormats/PPSObjects/interface/TotemFramePosition.h"
0019 
0020 #include "DataFormats/CTPPSDigi/interface/TotemVFATStatus.h"
0021 
0022 /**
0023  *\brief Class for finding the most popular both EC and BC counter, and filling the conversion
0024  * status 'wrong EC/BC number' for frames with different value.
0025  */
0026 class CounterChecker {
0027 public:
0028   typedef unsigned short word;
0029 
0030   typedef std::map<word, std::vector<TotemFramePosition> > CounterMap;
0031 
0032   enum CheckerType { BCChecker, ECChecker };
0033 
0034   /**
0035      * \param t: CounterChecker::ECCounter or CounterChecker::BCCounter. On that, depends whether
0036      * checker will fill wrong EC or BC rogress error.
0037      * \param name: name
0038      * \param min: minimal required number of frames to search for the most frequent one
0039      */
0040   CounterChecker(CheckerType _type = CounterChecker::BCChecker,
0041                  const std::string &_name = "",
0042                  unsigned int _min = 0,
0043                  double _fraction = 0.,
0044                  unsigned int _verbosity = 0)
0045       : type(_type), name(_name), min(_min), fraction(_fraction), verbosity(_verbosity) {}
0046 
0047   /// add new value to map, counter takes value of EC or BC number
0048   void Fill(word counter, TotemFramePosition fr);
0049 
0050   /// summarizes and fill the status (wrong EC and BC progress error for some frames)
0051   template <typename T>
0052   void Analyze(T &status, bool error, std::ostream &es);
0053 
0054 private:
0055   class Comparer {
0056   public:
0057     typedef unsigned short word;
0058     bool operator()(const std::pair<word, std::vector<TotemFramePosition> > &a,
0059                     const std::pair<word, std::vector<TotemFramePosition> > &b) {
0060       return a.second.size() < b.second.size();
0061     }
0062   };
0063 
0064   /// counter value -> list of frames with this value
0065   CounterMap relationMap;
0066 
0067   /// EC or BC counter checker
0068   CheckerType type;
0069 
0070   /// the name of this check, used in error messages
0071   std::string name;
0072 
0073   /// minimal required number of frames to search for the most frequent one
0074   unsigned int min;
0075 
0076   /// the most frequent value is accepted only provided its sub-sample size is greater than the
0077   /// specified fraction of the full sample
0078   double fraction;
0079 
0080   /// level of verbosity
0081   unsigned int verbosity;
0082 };
0083 
0084 template <typename T>
0085 void CounterChecker::Analyze(T &status, bool error, std::ostream &es) {
0086   word mostFrequentCounter = 0;
0087   word mostFrequentSize = 0;
0088   unsigned int totalFrames = 0;
0089 
0090   // finding the most frequent counter
0091   for (CounterMap::iterator iter = relationMap.begin(); iter != relationMap.end(); iter++) {
0092     unsigned int iterSize = iter->second.size();
0093     totalFrames += iterSize;
0094 
0095     if (iterSize > mostFrequentSize) {
0096       mostFrequentCounter = iter->first;
0097       mostFrequentSize = iter->second.size();
0098     }
0099   }
0100 
0101   if (totalFrames < min) {
0102     if (verbosity > 0)
0103       es << "Too few frames to determine the most frequent " << name << " value.";
0104 
0105     return;
0106   }
0107 
0108   // if there are too few frames with the most frequent value
0109   if ((float)mostFrequentSize / (float)totalFrames < fraction) {
0110     if (verbosity > 0)
0111       es << "The most frequent " << name << " value is doubtful - variance is too high.";
0112 
0113     return;
0114   }
0115 
0116   for (CounterMap::iterator iter = relationMap.begin(); iter != relationMap.end(); iter++) {
0117     if (iter->first != mostFrequentCounter) {
0118       for (std::vector<TotemFramePosition>::iterator fr = iter->second.begin(); fr != iter->second.end(); fr++) {
0119         if (error) {
0120           if (type == ECChecker)
0121             status[*fr].status.setECProgressError();
0122           if (type == BCChecker)
0123             status[*fr].status.setBCProgressError();
0124         }
0125 
0126         if (verbosity > 0)
0127           es << "Frame at " << *fr << ": " << name << " number " << iter->first
0128              << " is different from the most frequent one " << mostFrequentCounter << std::endl;
0129       }
0130     }
0131   }
0132 }
0133 
0134 #endif