HotMapMaker_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: HotMapMaker
3 // Module Type: analyzer
4 // File: HotMapMaker_module.cc
5 //
6 // Generated at Fri Feb 24 17:38:07 2017 by Andrey Sheshukov using artmod
7 // from cetpkgsupport v1_10_02.
8 ////////////////////////////////////////////////////////////////////////
9 
17 #include "fhiclcpp/ParameterSet.h"
22 #include <NovaTimingUtilities/TimingUtilities.h>
23 #include <boost/property_tree/ptree.hpp>
24 #include <boost/property_tree/json_parser.hpp>
25 #include <sstream>
26 
27 
28 namespace novaddt {
29  class HotMapMaker;
30 }
31 
33 public:
34  explicit HotMapMaker(fhicl::ParameterSet const & p);
35  // The destructor generated by the compiler is fine for classes
36  // without bare pointers or other resource use.
37 
38  // Plugins should not be copied or assigned.
39  HotMapMaker(HotMapMaker const &) = delete;
40  HotMapMaker(HotMapMaker &&) = delete;
41  HotMapMaker & operator = (HotMapMaker const &) = delete;
42  HotMapMaker & operator = (HotMapMaker &&) = delete;
43 
44  // Required functions.
45  void analyze(art::Event const & e) override;
46  void endJob(){ SaveMaps(); }
47 
49  void saveMetaData(std::string filename);
51  void AppendHot();
52  void AppendCold();
53  void AppendRate();
54  void SaveMaps();
55  void ResetMaps();
56 
57 private:
58 
59  // Declare member data here.
60  novaddt::calib::FDHitMap fShortMap;// hit rate per 5ms, collected over fEventsToCollect
61  novaddt::calib::FDHitMap fRateMap; // cumulative hit rate/5ms
62  novaddt::calib::FDHitMap fHotMap; // maximum of short rate maps
63  novaddt::calib::FDHitMap fColdMap; // count number of windows when channel was cold
64  // novaddt::calib::FDHitMap fMaskMap; // final product
65 
66  //configuration parameters
67  art::InputTag fHitsTag; //where to read the hits for noise activity
68  uint32_t fWindowSize;
69  double fColdLevel;
70  //thresholds to define channels
72  double fHotThreshold;
73  std::string fHotOutput; //where to write map
74  std::string fColdOutput; //where to write map
75  std::string fRateOutput; //where to write map
76  std::string fMetaOutput; //where to write metadata
77  std::string fMaskOutput; //where to write mask
78 
79  std::string fWatchFile; //a file to signalize that the map should be updated
80  //state
82  uint32_t fEventsCount=0;
83  uint32_t fMapsCount=0;
84  //metadata
87  uint32_t fEventsTotal=0;
88 
89 };
90 
91 
93  EDAnalyzer(p),
94  fHitsTag(p.get<std::string>("hits_tag")),
95  fWindowSize (p.get<uint32_t>("window_size")),
96  fColdLevel (p.get<double>("cold_level")),
97  fColdThreshold(p.get<double>("thresholds.cold")),
98  fHotThreshold (p.get<double>("thresholds.hot")),
99  fHotOutput (p.get<std::string>("output.hot", "")),
100  fColdOutput(p.get<std::string>("output.cold","")),
101  fRateOutput(p.get<std::string>("output.rate","")),
102  fMetaOutput(p.get<std::string>("output.metadata","")),
103  fMaskOutput(p.get<std::string>("output.mask","")),
104  fWatchFile (p.get<std::string>("watch")),
106 {
107  mf::SetModuleName("HotMapMaker");
108  mf::LogInfo("Configuration")<<"input_hits:\""<<fHitsTag
109  <<"\n* Mask Output: "<<fMaskOutput
110  <<"\n* Cold Output: "<<fColdOutput
111  <<"\n* Hot Output: "<<fHotOutput
112  <<"\n* Rate Output: "<<fRateOutput
113  <<"\n* Metadata Output: "<<fMetaOutput
114  <<"\nWatch file: "<<fWatchFile
115  <<"\nEvents to collect: "<<fWindowSize
116  <<std::endl;
117 
118  fShortMap.reset();
119  ResetMaps();
120 }
122  std::transform(fHotMap.data.begin(),fHotMap.data.end(),
123  fShortMap.data.begin(),fHotMap.data.begin(),
124  [](double a, double b){return std::max(a,b);}
125  );
126 }
127 
129  auto clevel=fColdLevel;
130  std::transform(fColdMap.data.begin(),fColdMap.data.end(),
131  fShortMap.data.begin(),fColdMap.data.begin(),
132  [clevel](double a, double b){return a+(b<=clevel);}
133  );
134 }
135 
137  std::cout<<"ShortMap max="<<*std::max_element(fShortMap.data.begin(),fShortMap.data.end())<<std::endl;
138  std::transform(fRateMap.data.begin(),fRateMap.data.end(),
139  fShortMap.data.begin(),fRateMap.data.begin(),
140  [](double a, double b){return a+b;}
141  );
142  std::cout<<"HotMap max="<<*std::max_element(fHotMap.data.begin(),fHotMap.data.end())<<std::endl;
143 }
144 
146 {
147  //get the hits which we'll use to fill the map
149  //fill the metadata
150  if(fEventsCount==0)
151  fTimeStart=hits->front().TDC();
152 
153  //fill the map
154  for(const auto &hit: *hits) fShortMap[hit]+=1;
155  //Increment events counter
156  if(++fEventsCount>=fWindowSize){
157  //normalize the map to 1 event
158  mf::LogInfo("Progress")<<"Appending hit map with "
159  <<fEventsCount<<" events"<<std::endl;
161  AppendHot();
162  AppendCold();
163  AppendRate();
164  fShortMap.reset();
166  ++fMapsCount;
167  fEventsCount=0;
168  }
169 
170  //check if we should do the rollover
171  if(fFileWatcher.isModified()){
172  //fill the metadata
173  fTimeEnd=hits->back().TDC();
174  SaveMaps();
175  ResetMaps();
177  fMapsCount=0;
178  fEventsTotal=0;
179  }
180 }
181 
182 
184 {
185  mf::LogInfo("MapRollover")<<" preparing the mask"<<std::endl;
187  //combine the mask from Hot and Cold maps:
188  double c_thresh=fColdThreshold;
189  double h_thresh=fHotThreshold;
190 
191  std::transform(fColdMap.data.begin(),fColdMap.data.end(),
192  fHotMap.data.begin(),mask.data.begin(),
193  [c_thresh,h_thresh](double a, double b){return 1*(a>=c_thresh)+2*(b>=h_thresh);}
194  );
195 
196  return mask;
197 }
198 
200 {
201  fHotMap.reset();
202  fColdMap.reset();
203  fRateMap.reset();
204 }
206 {
207  //normalize the maps
211  if(!fMaskOutput.empty()) saveMapTo(MakeMask(),fMaskOutput);
212  if(!fHotOutput.empty()) saveMapTo(fHotMap,fHotOutput);
215  if(!fMetaOutput.empty()) saveMetaData(fMetaOutput);
216 
217 }
219  mf::LogInfo("Progress")<<"Saving map to file:"<<fname <<std::endl;
220  //save map to the file
221  std::ofstream s(fname.data(),std::ofstream::binary);
222  s<<map;
223 }
224 
226  mf::LogInfo("Progress")<<"Saving metadata to file:"<<fname <<std::endl;
227  namespace pt = boost::property_tree;
228  pt::ptree tree;
229  tree.put("window_size", fWindowSize);
230  tree.put("count.maps", fMapsCount);
231  tree.put("count.events", fEventsTotal);
232  tree.put("time.start",fTimeStart.val);
233  tree.put("time.end", fTimeEnd.val);
234  tree.put("thresholds.cold", fColdThreshold);
235  tree.put("thresholds.hot", fHotThreshold);
236  tree.put("cold_level", fColdLevel);
237  pt::write_json(fname,tree);
238 }
T max(const caf::Proxy< T > &a, T b)
void saveMapTo(const novaddt::calib::FDHitMap &map, std::string filename)
novaddt::calib::FDHitMap fHotMap
bool isModified()
Definition: FileWatcher.h:15
novaddt::calib::FDHitMap fShortMap
value_type val
Definition: BaseProducts.h:34
HotMapMaker & operator=(HotMapMaker const &)=delete
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
void update()
Definition: FileWatcher.h:27
std::vector< DAQHit > HitList
Definition: HitList.h:15
const char * p
Definition: xmltok.h:285
novaddt::calib::FDHitMap fRateMap
void SetModuleName(std::string const &)
HotMapMaker(fhicl::ParameterSet const &p)
string filename
Definition: shutoffs.py:106
DEFINE_ART_MODULE(TestTMapFile)
calib::FDHitMap MakeMask()
const XML_Char * s
Definition: expat.h:262
void hits()
Definition: readHits.C:15
const double a
def write_json(jsonData, beam)
Definition: POTGetData.py:407
EDAnalyzer(Table< Config > const &config)
Definition: EDAnalyzer.h:100
OStream cout
Definition: OStream.cxx:6
void Scale(T koeff)
Definition: HitMap.h:37
Definition: structs.h:12
std::array< T, Dimension::length > data
Definition: HitMap.h:64
const hit & b
Definition: hits.cxx:21
ValidHandle< PROD > getValidHandle(InputTag const &tag) const
void analyze(art::Event const &e) override
Float_t e
Definition: plot.C:35
novaddt::calib::FDHitMap fColdMap
void saveMetaData(std::string filename)
enum BeamMode string