ChannelMapService_service.cc
Go to the documentation of this file.
1 #ifndef CHANNELMAP_SERVICE__CC
2 #define CHANNELMAP_SERVICE__CC
3 ////////////////////////////////////////////////////////////////////////
4 /// \file ChannelMap_service.cc
5 ///
6 /// \brief A service to manipulate channel map, containing activity
7 ///
8 /// \author Andrey Sheshukov
9 ////////////////////////////////////////////////////////////////////////
10 
12 
14 #include <boost/algorithm/string/replace.hpp>
15 #include <boost/lexical_cast.hpp>
16 
17 namespace novaddt{
19  type(p.get<std::string>("type")),
20  name(p.get<std::string>("name")),
21  count(p.get<int>("count"))
22  {
23  if(type!="file" && type!="product")
24  mf::LogError("MapSource")<<"Wrong source type '"<<type<<"'";
25  }
26 }
27 
28 
31  std::string RunS=boost::lexical_cast<std::string>(run.run());
32  boost::replace_all(result,"%r",RunS);
33  return result;
34 }
35 
36 namespace novaddt{
39  fMapSource(p.get<fhicl::ParameterSet>("source")),
40  fMapSave(p.get<std::string>("save.filename")),
41  fSegName(p.get<std::string>("shmem.segment")),
42  fMapName(p.get<std::string>("shmem.object")),
43  fSharedMapContainer(fSegName.data(),fMapName.data())
44 {
45  mf::LogInfo("ChannelMapService")<<"\t config:"
46  <<"\n\tmap source:"
47  <<"\n\t\ttype:"<<fMapSource.type
48  <<"\n\t\tname:"<<fMapSource.name
49  <<"\n\t\tcount:"<<fMapSource.count
50  <<"\nshmem.segment="<<fSegName<<";shmem.object="<<fMapName;
51 
52  fSharedMapContainer.getData().SetCounterMax(fMapSource.count);
53  if(fMapSource.type=="file")
54  readMapFromFile(fMapSource.name);
55 
56  //register callbacks: before starting run
58 
59  //register callbacks: after processing an event
60  if(fMapSource.type=="product")
62 }
63 
65 
66  saveMapToFile(MakeFileName(fMapSave,run));
67 }
68 
70 {
71  fillMapFromEvent(event,fMapSource.name);
72 }
73 
75 {
76  mf::LogInfo("ChannelMapService")<<"read map from file \""<<fname<<"\"";
77 
78  std::ifstream s(fname.data(),std::ifstream::binary);
79  if(!s.good()){
80  mf::LogError("ChannelMapService")<<" error opening file \""<<fname<<"\"";
81  return;
82  }
83 
84  {
85  auto lock= GetLock();
86  std::cout<<"Let's start!"<<std::endl;
87  auto &map = GetWMap();
88  std::cout<<"sizeof(map)="<<sizeof(map)
89  <<" ["<<sizeof(map)/map.Length()<<" per channel ]"<<std::endl;
90  size_t length = sizeof(map);
91  char buffer[length];
92  //reinterpret_cast<char*>(&map);
93  s.read(buffer,length);
94  mf::LogInfo("ChannelMapService")<<"read successfully";
95  }
96 }
98 {
99  mf::LogInfo("ChannelMapService")<<"save map to file \""<<fname<<"\"";
100 
101  std::ofstream s(fname.data(),std::ofstream::binary);
102  if(!s.good()){
103  mf::LogError("ChannelMapService")<<" error opening file \""<<fname<<"\"";
104  return;
105  }
106 
107  auto lock= GetLock();
108  auto map = GetRMap();
109  std::cout<<"sizeof(map)="<<sizeof(map)
110  <<" ["<<sizeof(map)/map.Length()<<" per channel ]"<<std::endl;;
111  s.write(reinterpret_cast<char*>(&map),sizeof(map));
112 }
113 
115  (const art::Event& event, const art::InputTag& product)
116 {
117  auto hits=event.getValidHandle<novaddt::HitList>(product);
118  mf::LogDebug("ChannelMapService")<<"fill map from \""<<product
119  <<"\" with "<<hits->size()<<" hits"
120  <<"\n Counter="<<getCounter();
121 
122  addCounter();
123  auto lock = GetLock();
124  auto &NoiseMap = GetWMap();
125  //fill the hits map
126  for(const auto &hit: *hits)
127  NoiseMap[hit]+=1;
128 }
129 
130 
132  std::cout<<"+++requested lock+++"<<std::endl;
133  return fSharedMapContainer.getData().getLock();
134 }
135 
137  return fSharedMapContainer.getData().getRBuffer();
138 }
139 
141  return fSharedMapContainer.getData().getWBuffer();
142 }
143 
144 
146 }
147 #endif
const XML_Char * name
Definition: expat.h:151
MapSource(fhicl::ParameterSet const &pset)
void readMapFromFile(const std::string &fname)
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
#define DEFINE_ART_SERVICE(svc)
Definition: ServiceMacros.h:93
std::vector< DAQHit > HitList
Definition: HitList.h:15
const char * p
Definition: xmltok.h:285
void preBeginRun(const art::Run &run)
const novaddt::calib::FDHitMap & GetRMap() const
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
RunNumber_t run() const
Definition: Run.h:47
::xsd::cxx::tree::buffer< char > buffer
Definition: Database.h:179
Definition: Run.h:31
novaddt::calib::sharableLock GetLock()
::xsd::cxx::tree::type type
Definition: Database.h:110
const XML_Char * s
Definition: expat.h:262
bool replace_all(std::string &in, std::string const &from, std::string const &to)
Replace all occurrences of from in string with to.
void fillMapFromEvent(const art::Event &event, const art::InputTag &product)
void hits()
Definition: readHits.C:15
length
Definition: demo0.py:21
void saveMapToFile(const std::string &fname)
boost::interprocess::sharable_lock< mutexType > sharableLock
Definition: BufferManager.h:12
T get(std::string const &key) const
Definition: ParameterSet.h:231
ChannelMapService(fhicl::ParameterSet const &pset, art::ActivityRegistry &reg)
void postProcessEvent(const art::Event &event)
Definition: run.py:1
OStream cout
Definition: OStream.cxx:6
std::string MakeFileName(const std::string fmt, const art::Run &run)
GlobalSignal< detail::SignalResponseType::FIFO, void(Run const &)> sPreBeginRun
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
T product(std::vector< T > dims)
novaddt::calib::FDHitMap & GetWMap()
MaybeLogger_< ELseverityLevel::ELsev_success, false > LogDebug
Definition: event.h:1
GlobalSignal< detail::SignalResponseType::LIFO, void(Event const &)> sPostProcessEvent