ATLAS Offline Software
VP1EventProd.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
8 
10 
11 #include "AthenaBaseComps/AthMsgStreamMacros.h" // ATH_MSG_* macros
12 
13 #include "GaudiKernel/ServiceHandle.h"
14 #include "GaudiKernel/IEvtSelector.h"
15 #include "GaudiKernel/IIncidentSvc.h"
16 #include "GaudiKernel/ToolHandle.h"
17 
18 #include <vector>
19 #include <iostream>
20 #include <iomanip>
21 #include <stdexcept>
22 #include <unistd.h>
23 #include <ctime> /* time_t, time, ctime */
24 
25 
26 VP1EventProd::VP1EventProd(const std::string& name, ISvcLocator* svcLocator):
27  AthAlgorithm(name, svcLocator),
28  m_runNumber(0),
29  m_eventNumber(0),
30  m_timeStamp(0),
31  m_humanTimestamp(""),
32  m_nEvent(0)
33 {
34  declareProperty("InputPoolFile", m_inputPoolFile="");
35  declareProperty("DestinationDirectory", m_destinationDir="."); // produce files in the run directory by default
36  declareProperty("ForceCreateDestinationDirectory", m_createDestinationDir=true); // force creation of new output folder
37  declareProperty("MaxNumberOfFiles", m_maxProducedFiles=-1); // keep N event files in the run directory. Default is '-1' that means ALL.
38  declareProperty("OutputFileTypeLabel", m_outputFileType=""); // put a label at the end of the output file, stating its file type
39  declareProperty("RemoveTempInputFiles", m_removeTempInputFiles=true); // remove the temp input file, i.e. the single-event files produced by the Athena job (HITS, ESD, ...) and which are then copied to files called "vp1_...pool.root"
40 
41  // Note: you cannot initialize a VP1FileUtils instance here, because otherwise it would take the default properties only.
42 
43 }
44 
46 {
47 }
48 
50 {
51  ATH_MSG_DEBUG(" in initialize() ");
52 
53  StatusCode result = StatusCode::SUCCESS;
54 
55  // use the incident service to register a handle
56  IIncidentSvc* incsvc = 0;
57  StatusCode status = service("IncidentSvc", incsvc, true);
58 
59  if(status.isFailure() || incsvc==0)
60  ATH_MSG_WARNING("Unable to get IncidentSvc!");
61  else
62  incsvc->addListener(this, "BeginEvent", 0);
63 
64  if(m_isOnline){
65  if( m_onlineEDsvc.retrieve().isFailure()){
66  ATH_MSG_ERROR("Could not locate the online event displays service");
67  return StatusCode::FAILURE;
68  }
69  }
70 
71  return result;
72 }
73 
75 {
76  ATH_MSG_DEBUG(" in execute(). Nothing to do here...");
77 
78  if(m_isOnline){
79  m_destinationDir = m_onlineEDsvc->getEntireOutputStr();
80  }
81 
82  return StatusCode::SUCCESS;
83 }
84 
86 {
87  ATH_MSG_DEBUG("in finalize() ");
88 
89  if(m_isOnline){
90  m_destinationDir = m_onlineEDsvc->getEntireOutputStr();
91  }
92 
93  ATH_MSG_DEBUG("VP1ALG m_destinationDir " << m_destinationDir);
94  // handle the output of the last event
95  if(m_nEvent) {
96 
97  --m_nEvent; // since we don't use another call to handle() to process the last event, we need to revert the counter by one, otherwise the wrong file is looked for
98 
99  ATH_MSG_DEBUG("--> Input POOL file: " << m_inputPoolFile);
100 
101  std::ostringstream ostri;
102  ostri << m_inputPoolFile << "._" << std::setw(4) << std::setfill('0') << m_nEvent;
103 
104  std::string inputFileName = ostri.str();
105  ATH_MSG_DEBUG("copying the input file: '"<< inputFileName << "'...");
106  ATH_MSG_DEBUG("VP1 alg event number: " << m_eventNumber);
107  try {
108 
109  /* clean the output directory if m_maxProducedFiles == 0
110  * or keep up to 'm_maxProducedFiles' output files
111  */
112  VP1FileUtilities fileUtil(".", m_maxProducedFiles, m_destinationDir, m_createDestinationDir, m_removeTempInputFiles); // inputDir, fileLimit, outputDir, forceMakeOutputDir, removeInputFile
113 
114  if (m_outputFileType != "")
115  fileUtil.produceNewFile(inputFileName, m_runNumber, m_eventNumber, m_timeStamp, m_humanTimestamp+"."+m_outputFileType); // with UNIX and human-readable timestamp
116  else
117  fileUtil.produceNewFile(inputFileName, m_runNumber, m_eventNumber, m_timeStamp, m_humanTimestamp); // with UNIX timestamp
118  }
119  catch(std::runtime_error& err) {
120  ATH_MSG_WARNING("Exception caught: " << err.what());
121  ATH_MSG_WARNING("In finalize() -- Unable to produce new VP1 event file");
122  }
123  }
124 
125  return StatusCode::SUCCESS;
126 }
127 
128 
129 
130 void VP1EventProd::handle(const Incident& inc)
131 {
132  ATH_MSG_DEBUG("in handle()... ");
133  ATH_MSG_DEBUG("Handling incident '" << inc.type() << "'");
134 
135  if(m_isOnline){
136  m_destinationDir = m_onlineEDsvc->getEntireOutputStr();
137  }
138 
139  ATH_MSG_DEBUG("VP1ALG m_destinationDir " << m_destinationDir);
140 
141  // Let VP1FileUtilities handle the output of the previous event.
142  // Skip this if m_nEvent == 0,
143  // because the processing of the event is not completed, yet;
144  // and the input file is not created at this stage, yet.
145  // Basically we run the code below while in event_2, to get the processed file for event_1
146  if(m_nEvent) {
147 
148  unsigned int nLastFile = m_nEvent - 1; // we copy the file produced while processing the previous event, so we need a file number of (current - 1)
149 
150  ATH_MSG_DEBUG("--> Input POOL file: " << m_inputPoolFile);
151  std::ostringstream ostri;
152  ostri << m_inputPoolFile << "._" << std::setw(4) << std::setfill('0') << nLastFile;
153 
154  std::string inputFileName = ostri.str();
155  ATH_MSG_DEBUG("copying the input file: '"<< inputFileName << "'...");
156 
157  try {
158 
159  /* clean the output directory if m_maxProducedFiles == 0
160  * or keep up to 'm_maxProducedFiles' output files
161  */
162  VP1FileUtilities fileUtil(".", m_maxProducedFiles, m_destinationDir, m_createDestinationDir, m_removeTempInputFiles); // inputDir, fileLimit, outputDir, forceMakeOutputDir, removeInputFile
163 
164  if (m_outputFileType != "")
165  fileUtil.produceNewFile(ostri.str(), m_runNumber, m_eventNumber, m_timeStamp, m_humanTimestamp+"."+m_outputFileType); // with UNIX and human-readable timestamp
166  else
167  fileUtil.produceNewFile(ostri.str(), m_runNumber, m_eventNumber, m_timeStamp, m_humanTimestamp); // with UNIX timestamp
168 
169  }
170  catch(std::runtime_error& err) {
171  ATH_MSG_WARNING("Exception caught: " << err.what());
172  ATH_MSG_WARNING("In handle() -- Unable to produce new VP1 event file");
173  }
174  }
175 
176 
177  /*
178  * -------------------------------------------------------------------
179  * === NOTE!!! ===
180  *
181  * AFTER having processed the file from the previous event,
182  * we now update the run number and event number for the current event
183  * --------------------------------------------------------------------
184  */
185 
186  // increment event count
187  m_nEvent++;
188 
189  // Update run_number/event_number/time_stamp
190  const EventContext& context = getContext();
191  m_eventNumber = context.eventID().event_number();
192  m_runNumber = context.eventID().run_number();
193  m_timeStamp = context.eventID().time_stamp();
194 
195  ATH_MSG_DEBUG(" Got run number = " << m_runNumber
196  << ", event number = " << m_eventNumber
197  << ", UNIX timestamp = " << m_timeStamp);
198 
199  time_t t_timestamp = m_timeStamp;
200  struct tm ltm;
201  localtime_r(&t_timestamp, &ltm);
202 
203  // print various components of tm structure.
204  ATH_MSG_DEBUG("Year: "<< 1900 + ltm.tm_year
205  << " - " << "Month: "<< 1 + ltm.tm_mon<< " - " // tm_mon is in the range [0, 11], so 1 must be added to get real months
206  << "Day: "<< ltm.tm_mday
207  << " - " "Time: "<< ltm.tm_hour << ":" << ltm.tm_min << ":" << ltm.tm_sec << "CEST"
208  );
209 
210  std::ostringstream ostri;
211  ostri << 1900 + ltm.tm_year
212  << "-" << 1 + ltm.tm_mon // tm_mon is in the range [0, 11], so 1 must be added to get real months
213  << "-" << ltm.tm_mday
214  << "T" << ltm.tm_hour << "-" << ltm.tm_min << "-" << ltm.tm_sec << "CEST";
215 
216  m_humanTimestamp = ostri.str();
217  ATH_MSG_DEBUG("'human readable' timestamp: " << m_humanTimestamp);
218 
219 }
VP1EventProd::m_removeTempInputFiles
bool m_removeTempInputFiles
Definition: VP1EventProd.h:57
VP1EventProd::finalize
StatusCode finalize()
Definition: VP1EventProd.cxx:85
VP1EventProd::m_nEvent
int m_nEvent
Definition: VP1EventProd.h:64
get_generator_info.result
result
Definition: get_generator_info.py:21
VP1EventProd::m_runNumber
unsigned long m_runNumber
Definition: VP1EventProd.h:51
VP1EventProd::execute
StatusCode execute()
Definition: VP1EventProd.cxx:74
AthMsgStreamMacros.h
VP1EventProd::m_outputFileType
std::string m_outputFileType
Definition: VP1EventProd.h:56
AthCommonDataStore< AthCommonMsg< Algorithm > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
VP1EventProd::initialize
StatusCode initialize()
Definition: VP1EventProd.cxx:49
VP1EventProd::m_destinationDir
std::string m_destinationDir
Definition: VP1EventProd.h:61
VP1EventProd::handle
void handle(const Incident &inc)
Definition: VP1EventProd.cxx:130
VP1EventProd::m_isOnline
Gaudi::Property< bool > m_isOnline
Definition: VP1EventProd.h:47
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
dqt_zlumi_pandas.err
err
Definition: dqt_zlumi_pandas.py:193
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
VP1EventProd::m_maxProducedFiles
int m_maxProducedFiles
Definition: VP1EventProd.h:63
AthAlgorithm
Definition: AthAlgorithm.h:47
VP1EventProd::VP1EventProd
VP1EventProd(const std::string &name, ISvcLocator *pSvcLocator)
Definition: VP1EventProd.cxx:26
PathResolver.h
VP1EventProd::m_eventNumber
unsigned long long m_eventNumber
Definition: VP1EventProd.h:52
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
VP1EventProd::m_humanTimestamp
std::string m_humanTimestamp
Definition: VP1EventProd.h:55
VP1EventProd::m_inputPoolFile
std::string m_inputPoolFile
Definition: VP1EventProd.h:60
VP1EventProd::m_onlineEDsvc
ServiceHandle< IOnlineEventDisplaysSvc > m_onlineEDsvc
Definition: VP1EventProd.h:48
VP1EventProd::m_timeStamp
unsigned long m_timeStamp
Definition: VP1EventProd.h:54
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
VP1FileUtilities::produceNewFile
void produceNewFile(const std::string &sourceFile, unsigned int runNumber, unsigned long long eventNumber, unsigned int timeStamp, const std::string &textLabel="")
Definition: VP1FileUtilities.cxx:67
VP1EventProd.h
VP1FileUtilities.h
VP1EventProd::m_createDestinationDir
bool m_createDestinationDir
Definition: VP1EventProd.h:62
merge.status
status
Definition: merge.py:17
VP1FileUtilities
Definition: VP1FileUtilities.h:25
VP1EventProd::~VP1EventProd
~VP1EventProd()
Definition: VP1EventProd.cxx:45