CMS 3D CMS Logo

SiStripSpyExtractRunModule.cc
Go to the documentation of this file.
1 // Original Author: Anne-Marie Magnan
2 // Created: 2010/02/25
3 //
4 
5 #include <sstream>
6 #include <fstream>
7 #include <iostream>
8 #include <memory>
9 #include <list>
10 #include <algorithm>
11 #include <cassert>
12 
24 
27 
29 
30 //
31 // Class declaration
32 //
33 namespace sistrip {
34 
36  public:
37  explicit SpyExtractRunModule(const edm::ParameterSet&);
38  ~SpyExtractRunModule() override;
39 
40  private:
41  void beginJob() override;
42  void analyze(const edm::Event&, const edm::EventSetup&) override;
43  void endJob() override;
44 
45  //check when the current run changes
46  const bool updateRun(const uint32_t aRun);
47 
48  //name of the output file containing the run number
49  //get it from the input file
51 
52  //tag of spydata run number collection
55 
56  //cache of the current and previous run number
57  uint32_t currentRun_;
58  uint32_t previousRun_;
59 
60  //error counter for number of times the run number changes
61  uint32_t errCounter_;
62  };
63 } // namespace sistrip
64 
65 using edm::LogError;
66 using edm::LogInfo;
67 using edm::LogWarning;
68 //
69 // Constructors and destructor
70 //
71 namespace sistrip {
72 
74  : fileName_(iConfig.getParameter<std::string>("OutputTextFile")),
75  runTag_(iConfig.getParameter<edm::InputTag>("RunNumberTag")),
76  currentRun_(0),
77  previousRun_(0),
78  errCounter_(0) {
79  runToken_ = consumes<uint32_t>(runTag_);
80  }
81 
83 
85  currentRun_ = 0;
86  previousRun_ = 0;
87  errCounter_ = 0;
88  }
89 
90  void SpyExtractRunModule::analyze(const edm::Event& aEvt, const edm::EventSetup& aSetup) {
91  static bool lFirstEvent = true;
93  aEvt.getByToken(runToken_, lRun);
94 
95  const bool isUpdated = updateRun(*lRun);
96 
97  if (isUpdated && !lFirstEvent) {
98  edm::LogError("SpyExtractRunModule")
99  << " -- Run number changed for event : " << aEvt.id().event() << " (id().run() = " << aEvt.id().run()
100  << ") from " << previousRun_ << " to " << currentRun_ << std::endl;
101  }
102 
103  lFirstEvent = false;
104  }
105 
107  //save global run number in text file in local directory
108  //output loginfo with number of errors
109  //or throw exception ?
110 
111  if (errCounter_ == 1) {
112  edm::LogInfo("SiStripSpyExtractRun")
113  << " -- Writting run number " << currentRun_ << " into file " << fileName_ << std::endl;
114  std::ofstream lOutFile;
115  lOutFile.open(fileName_.c_str(), std::ios::out);
116  if (!lOutFile.is_open()) {
117  edm::LogError("SiStripSpyExtractRun")
118  << " -- Cannot open file : " << fileName_ << " for writting run number " << currentRun_ << std::endl;
119  } else {
120  lOutFile << currentRun_ << std::endl;
121  lOutFile.close();
122  }
123 
124  } else {
125  edm::LogError("SiStripSpyExtractRun")
126  << " -- Number of times the run number changed in this job = " << errCounter_
127  << ", currentRun = " << currentRun_ << ", previousRun = " << previousRun_ << std::endl;
128  }
129  }
130 
131  const bool SpyExtractRunModule::updateRun(const uint32_t aRun) {
132  if (aRun != currentRun_) {
134  currentRun_ = aRun;
135  errCounter_++;
136  return true;
137  }
138  return false;
139  }
140 
141 } // namespace sistrip
142 
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
void analyze(const edm::Event &, const edm::EventSetup &) override
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:539
edm::EDGetTokenT< uint32_t > runToken_
Log< level::Error, false > LogError
sistrip classes
edm::EventID id() const
Definition: EventBase.h:59
const bool updateRun(const uint32_t aRun)
Log< level::Info, false > LogInfo
RunNumber_t run() const
Definition: EventID.h:38
sistrip::SpyExtractRunModule SiStripSpyExtractRunModule
Constants and enumerated types for FED/FEC systems.
HLT enums.
Log< level::Warning, false > LogWarning
SpyExtractRunModule(const edm::ParameterSet &)
EventNumber_t event() const
Definition: EventID.h:40