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  SmartIF<IIncidentSvc> incsvc{service("IncidentSvc")};
57 
58  if(!incsvc)
59  ATH_MSG_WARNING("Unable to get IncidentSvc!");
60  else
61  incsvc->addListener(this, "BeginEvent", 0);
62 
63  if(m_isOnline){
64  if( m_onlineEDsvc.retrieve().isFailure()){
65  ATH_MSG_ERROR("Could not locate the online event displays service");
66  return StatusCode::FAILURE;
67  }
68  }
69 
70  return result;
71 }
72 
74 {
75  ATH_MSG_DEBUG(" in execute(). Nothing to do here...");
76 
77  return StatusCode::SUCCESS;
78 }
79 
81 {
82  ATH_MSG_DEBUG("in finalize() ");
83 
84  // handle the output of the last event
85  if(m_nEvent) {
86 
87  --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
88 
89  ATH_MSG_DEBUG("--> Input POOL file: " << m_inputPoolFile);
90 
91  std::ostringstream ostri;
92  ostri << m_inputPoolFile << "._" << std::setw(4) << std::setfill('0') << m_nEvent;
93 
94  std::string inputFileName = ostri.str();
95  ATH_MSG_DEBUG("copying the input file: '"<< inputFileName << "'...");
96  ATH_MSG_DEBUG("VP1 alg event number: " << m_eventNumber);
97  try {
98 
99  /* clean the output directory if m_maxProducedFiles == 0
100  * or keep up to 'm_maxProducedFiles' output files
101  */
102  VP1FileUtilities fileUtil(".", m_maxProducedFiles, m_destinationDir, m_createDestinationDir, m_removeTempInputFiles); // inputDir, fileLimit, outputDir, forceMakeOutputDir, removeInputFile
103 
104  if (m_outputFileType != "")
105  fileUtil.produceNewFile(inputFileName, m_runNumber, m_eventNumber, m_timeStamp, m_humanTimestamp+"."+m_outputFileType); // with UNIX and human-readable timestamp
106  else
107  fileUtil.produceNewFile(inputFileName, m_runNumber, m_eventNumber, m_timeStamp, m_humanTimestamp); // with UNIX timestamp
108  }
109  catch(std::runtime_error& err) {
110  ATH_MSG_WARNING("Exception caught: " << err.what());
111  ATH_MSG_WARNING("In finalize() -- Unable to produce new VP1 event file");
112  }
113  }
114 
115  return StatusCode::SUCCESS;
116 }
117 
118 
119 
120 void VP1EventProd::handle(const Incident& inc)
121 {
122  ATH_MSG_DEBUG("in handle()... ");
123  ATH_MSG_DEBUG("Handling incident '" << inc.type() << "'");
124 
125  // Let VP1FileUtilities handle the output of the previous event.
126  // Skip this if m_nEvent == 0,
127  // because the processing of the event is not completed, yet;
128  // and the input file is not created at this stage, yet.
129  // Basically we run the code below while in event_2, to get the processed file for event_1
130  if(m_nEvent) {
131 
132  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)
133 
134  ATH_MSG_DEBUG("--> Input POOL file: " << m_inputPoolFile);
135  std::ostringstream ostri;
136  ostri << m_inputPoolFile << "._" << std::setw(4) << std::setfill('0') << nLastFile;
137 
138  std::string inputFileName = ostri.str();
139  ATH_MSG_DEBUG("copying the input file: '"<< inputFileName << "'...");
140 
141  try {
142 
143  /* clean the output directory if m_maxProducedFiles == 0
144  * or keep up to 'm_maxProducedFiles' output files
145  */
146  VP1FileUtilities fileUtil(".", m_maxProducedFiles, m_destinationDir, m_createDestinationDir, m_removeTempInputFiles); // inputDir, fileLimit, outputDir, forceMakeOutputDir, removeInputFile
147 
148  if (m_outputFileType != "")
149  fileUtil.produceNewFile(ostri.str(), m_runNumber, m_eventNumber, m_timeStamp, m_humanTimestamp+"."+m_outputFileType); // with UNIX and human-readable timestamp
150  else
151  fileUtil.produceNewFile(ostri.str(), m_runNumber, m_eventNumber, m_timeStamp, m_humanTimestamp); // with UNIX timestamp
152 
153  }
154  catch(std::runtime_error& err) {
155  ATH_MSG_WARNING("Exception caught: " << err.what());
156  ATH_MSG_WARNING("In handle() -- Unable to produce new VP1 event file");
157  }
158  }
159 
160 
161  /*
162  * -------------------------------------------------------------------
163  * === NOTE!!! ===
164  *
165  * AFTER having processed the file from the previous event,
166  * we now update the run number and event number for the current event
167  * --------------------------------------------------------------------
168  */
169 
170  // increment event count
171  m_nEvent++;
172 
173  // Update run_number/event_number/time_stamp
174  const EventContext& context = getContext();
175  m_eventNumber = context.eventID().event_number();
176  m_runNumber = context.eventID().run_number();
177  m_timeStamp = context.eventID().time_stamp();
178 
179  ATH_MSG_DEBUG(" Got run number = " << m_runNumber
180  << ", event number = " << m_eventNumber
181  << ", UNIX timestamp = " << m_timeStamp);
182 
183  time_t t_timestamp = m_timeStamp;
184  struct tm ltm;
185  localtime_r(&t_timestamp, &ltm);
186 
187  // print various components of tm structure.
188  ATH_MSG_DEBUG("Year: "<< 1900 + ltm.tm_year
189  << " - " << "Month: "<< 1 + ltm.tm_mon<< " - " // tm_mon is in the range [0, 11], so 1 must be added to get real months
190  << "Day: "<< ltm.tm_mday
191  << " - " "Time: "<< ltm.tm_hour << ":" << ltm.tm_min << ":" << ltm.tm_sec << "CEST"
192  );
193 
194  std::ostringstream ostri;
195  ostri << 1900 + ltm.tm_year
196  << "-" << 1 + ltm.tm_mon // tm_mon is in the range [0, 11], so 1 must be added to get real months
197  << "-" << ltm.tm_mday
198  << "T" << ltm.tm_hour << "-" << ltm.tm_min << "-" << ltm.tm_sec << "CEST";
199 
200  m_humanTimestamp = ostri.str();
201  ATH_MSG_DEBUG("'human readable' timestamp: " << m_humanTimestamp);
202 
203  if(m_isOnline){
204  m_destinationDir = m_onlineEDsvc->getEntireOutputStr();
205  }
206  ATH_MSG_DEBUG("Destination Directory: " << m_destinationDir);
207 
208 }
VP1EventProd::m_removeTempInputFiles
bool m_removeTempInputFiles
Definition: VP1EventProd.h:57
VP1EventProd::finalize
StatusCode finalize()
Definition: VP1EventProd.cxx:80
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:73
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:120
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:182
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:221
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
VP1FileUtilities
Definition: VP1FileUtilities.h:25
VP1EventProd::~VP1EventProd
~VP1EventProd()
Definition: VP1EventProd.cxx:45