ATLAS Offline Software
VP1Alg.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include "VP1Algs/VP1Alg.h"
6 
7 #include "VP1Gui/VP1Gui.h"
9 
12 
13 #include "GaudiKernel/IToolSvc.h"
14 #include "GaudiKernel/FileIncident.h"
15 #include "GaudiKernel/ServiceHandle.h"
16 #include "GaudiKernel/IEvtSelector.h"
17 #include "GaudiKernel/IIncidentSvc.h"
18 
19 #include <vector>
20 #include <stdexcept>
21 #include <iostream>
22 #include <iomanip>
23 #include <cstdint> // for uint64_t (unsigned long long)
24 #include <cstdlib> //For setenv
25 
26 
27 //____________________________________________________________________
28 VP1Alg::VP1Alg(const std::string& name, ISvcLocator* svcLocator):
29  AthAlgorithm(name, svcLocator),
30  m_toolSvc(0),m_vp1gui(0)
31 {
32 
33 
34  /*
35  // This is Work In Progress
36  // Those properties are needed in VP1 configurations, which
37  // still need to be migrated to CA.
38  // See the header file for details.
39  //
40  declareProperty("InitiallyLoadedVP1Files",m_initialvp1files);
41  declareProperty("InitialCruiseMode",m_initialCruiseMode="NONE");
42  declareProperty("InitialCruiseModePeriod",m_initialCruiseSeconds=10);
43 
44  declareProperty("NoGui",m_noGui=false);
45 
46  // **** MF ****
47  declareProperty("MultipleFilesON",m_mfOn=false);
48  declareProperty("MFNumFileLimit",m_mfLimit=10);
49  declareProperty("MFSourceDir",m_mfSourceDir="");
50  declareProperty("MFLocalCopyDir",m_mfLocalCopyDir="");
51  declareProperty("MFAvailableLocalInputDirectories",m_mfAvailableLocalInputDirectories);
52 */
53 
54  // Two ways of running in multiple files mode:
55  //
56  // 1) Source is directory on filesystem (e.g. for use in the control room).
57  // * MFSourceDir indicates the directory where files can be found.
58  // * MFLocalCopyDir indicates the temporary directory where
59  // files are copied before we tell pool to get them there.
60  // 2) Source is web directory
61  // * MFSourceDir indicates http url to small text file (to be
62  // parsed by VP1Gui/VP1EvtsOnServerInfo), with a list of
63  // available files (assumed to be in the same dir).
64  // Example value: "http://pcatdwww.cern.ch/atlas-point1/vp1events/fileinfo.txt"
65  // * MFLocalCopyDir has same meaning as before, but this time a
66  // subdirectory of it, "active_downloads", will be used to
67  // download files before they are subsequently moved to
68  // MFLocalCopyDir itself.
69 
70  // **** MF ****
71 
72  // Pick up patched Coin
73  ::setenv("LCGPATCH_COINMULTISELECT","1",1);
74 }
75 
76 //____________________________________________________________________
78 {
79 }
80 
81 //____________________________________________________________________
83 {
84  msg(MSG::INFO) << " in initialize() " << endmsg;
85 
86  //ToolSvc
87  StatusCode status = service("ToolSvc",m_toolSvc);
88  if (status.isFailure()||!m_toolSvc) {
89  msg(MSG::ERROR) << " Unable to get ToolSvc!" << endmsg;
90  return status;
91  }
92 
94  for (it = m_initialvp1files.begin();it!=itE;++it) {
95  std::string file = PathResolver::find_file (*it, "DATAPATH");
96  if (file=="")
97  file = PathResolver::find_file (*it+".vp1", "DATAPATH");
98  if (file!="")
99  *it = file;
100  }
101 
102  // use the incident service to register a handler
103  IIncidentSvc* incsvc = 0;
104  status = service("IncidentSvc", incsvc, true);
105 
106  if(status.isFailure() || incsvc==0) {
107  msg(MSG::WARNING) << "Unable to get IncidentSvc! MF mechanism is disabled" << endmsg;
108  return StatusCode::SUCCESS;
109  }
110 
111  std::string endfilekey("EndTagFile");
112  incsvc->addListener(this, endfilekey, 0);
113  msg(MSG::DEBUG) << "Added listener on "<<endfilekey << endmsg;
114 
115  //Create VP1 gui object and see if it considers settings to be valid.
116  m_vp1gui = new VP1Gui(&(*evtStore()),&(*detStore()),serviceLocator(),m_toolSvc,
120  (m_mfOn ? m_mfSourceDir : ""),
121  (m_mfOn ? m_mfLocalCopyDir : ""),
122  m_mfLimit,
123  ( m_mfOn ? m_mfAvailableLocalInputDirectories : Gaudi::Property<std::vector<std::string>>() ) );
124  if (!m_vp1gui->argumentsAreValid()) {
125  delete m_vp1gui;
126  m_vp1gui = 0;
127  exit(1);//Should we do this? It is kind of nice to not have to dig through the logfile to find the failure.
128  return StatusCode::FAILURE;
129  }
130  return StatusCode::SUCCESS;
131 }
132 
133 //____________________________________________________________________
135 {
136 
137  // Here you do everything that needs to be done on every single
138  // events, e.g. read input variables, apply cuts, and fill
139  // histograms and trees, if any.
140 
141  msg(MSG::DEBUG) <<" in execute() " << endmsg;
142 
143  if (!m_vp1gui)
144  return StatusCode::FAILURE;
145 
146  if (!m_noGui&&!m_vp1gui->hasBeenInitialised()) {
147  m_vp1gui->init();//Launch!
148  }
149 
150 
151 
152 
153  unsigned int trigType = 0;
154 
155  // retrieve the eventInfo object from the event store
156  const xAOD::EventInfo *eventInfo = nullptr;
157  StatusCode status = evtStore()->retrieve (eventInfo, "EventInfo");
158 
159  if(status.isSuccess()) {
160  // Get run/event number:
161  const unsigned long long eventNumber = eventInfo->eventNumber();
162  const uint32_t runNumber = eventInfo->runNumber();
163  msg(MSG::DEBUG) << " Got run number = " << runNumber
164  << ", event number = " << eventNumber << endmsg;
165 
166  trigType = eventInfo->level1TriggerType();
167 
168  // Get time stamp:
169  uint32_t time = eventInfo->timeStamp();//0 means no info.
170 
172  return StatusCode::SUCCESS;
173  } else {
174  msg(MSG::INFO) << " Ending application gracefully." << endmsg;
175  return StatusCode::FAILURE;
176  }
177  return StatusCode::SUCCESS;
178  }
179 
180  msg(MSG::DEBUG) << " Unable to retrieve EventInfo from StoreGate... using dummy values" << endmsg;
181 
182  if (m_noGui||m_vp1gui->executeNewEvent(999,999,trigType,0)) {
183  return StatusCode::SUCCESS;
184  } else {
185  msg(MSG::INFO) << " Ending application gracefully." << endmsg;
186  return StatusCode::FAILURE;
187  }
188 
189  return StatusCode::SUCCESS;
190 
191 }
192 
193 //____________________________________________________________________
195 {
196  msg(MSG::INFO) <<" in finalize() " << endmsg;
197 
198  if (!m_vp1gui)
199  return StatusCode::FAILURE;
200 
201  if (!m_noGui)
202  m_vp1gui->cleanup();
203  delete m_vp1gui;
204 
205  return StatusCode::SUCCESS;
206 }
207 
208 //____________________________________________________________________
209 void VP1Alg::handle(const Incident& inc)
210 {
211  msg(MSG::INFO) << "Handling incident '" << inc.type() << "'" << endmsg;
212 
213  if (!m_vp1gui) {
214  msg(MSG::INFO) << "Aborting due to null VP1Gui pointer." << endmsg;
215  return;
216  }
217 
218  const FileIncident* fileInc = dynamic_cast<const FileIncident*>(&inc);
219  if(fileInc == 0) {
220  msg(MSG::WARNING) << " Unable to cast to file incident" << endmsg;
221  return;
222  }
223  else
224  msg(MSG::DEBUG) << " Casting to file incident successful" << endmsg;
225 
226  // Locate the EventSelector
227  ServiceHandle<IEvtSelector> pEvtSelector("EventSelector", this->name());
228  StatusCode sc = pEvtSelector.retrieve();
229 
230  if(!sc.isSuccess() || 0 == pEvtSelector) {
231  msg(MSG::WARNING) << "Could not find EventSelector" << endmsg;
232  return;
233  }
234  else
235  msg(MSG::DEBUG) << " Got EventSelector" << endmsg;
236 
237  IProperty* propertyServer = dynamic_cast<IProperty*>(pEvtSelector.operator->());
238  if (!propertyServer) {
239  msg(MSG::WARNING) << "Could not get propertyServer" << endmsg;
240  return;
241  }
242 
243  std::vector<std::string> vect;
244  StringArrayProperty inputCollections("InputCollections", vect);
245 
246  sc = propertyServer->getProperty(&inputCollections);
247  if(!sc.isSuccess()) {
248  msg(MSG::INFO) << "Could not get InputCollections property" << endmsg;
249  return;
250  }
251  else
252  msg(MSG::DEBUG) << " Got InputCollections property" << endmsg;
253 
254  for (const std::string& s : inputCollections.value()) {
255  if(s != fileInc->fileName())
256  vect.push_back(s);
257  }
258 
259  if(m_mfOn) {
260  std::string strNewFileName = m_vp1gui->nextRequestedEventFile();
261 
262  if (strNewFileName.empty())
263  return;
264  if (!VP1FileUtilities::fileExistsAndReadable(strNewFileName)) {
265  msg(MSG::WARNING) << " File requested by VP1 does not exists or is not readable: "<<strNewFileName<<". Ending." << endmsg;
266  return;
267  }
268 
269  vect.push_back(strNewFileName);
270  msg(MSG::INFO) << " Setting next event file: " << strNewFileName<< endmsg;
271  }
272  else {
273  std::vector<std::string> strNewFileNames = m_vp1gui->userRequestedFiles();
274  for(unsigned i=0; i<strNewFileNames.size(); ++i) {
275  const std::string& strNewFileName = strNewFileNames[i];
276  if (strNewFileName.empty())
277  continue;
278  if (!VP1FileUtilities::fileExistsAndReadable(strNewFileName)) {
279  msg(MSG::WARNING) << " File requested by VP1 does not exists or is not readable: " << strNewFileName << endmsg;
280  continue;
281  }
282  vect.push_back(strNewFileName);
283  msg(MSG::INFO) << " Setting next event file: " << strNewFileName<< endmsg;
284  }
285  }
286 
287  StringArrayProperty newInputCollections("InputCollections", vect);
288 
289  if(propertyServer->setProperty(newInputCollections)!=StatusCode::SUCCESS)
290  msg(MSG::WARNING) << "Could not set new InputCollections property" << endmsg;
291  else
292  msg(MSG::DEBUG) << " InputCollections property set" << endmsg;
293 }
294 
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
VP1Alg::initialize
virtual StatusCode initialize() override
Definition: VP1Alg.cxx:82
VP1FileUtilities::fileExistsAndReadable
static bool fileExistsAndReadable(const std::string &)
Definition: VP1FileUtilities.cxx:211
VP1Alg::m_vp1gui
VP1Gui * m_vp1gui
Definition: VP1Alg.h:76
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
VP1Gui
Definition: VP1Gui.h:28
xAOD::EventInfo_v1::eventNumber
uint64_t eventNumber() const
The current event's event number.
PathResolver::find_file
static std::string find_file(const std::string &logical_file_name, const std::string &search_path, SearchType search_type=LocalSearch)
Definition: PathResolver.cxx:251
xAOD::uint32_t
setEventNumber uint32_t
Definition: EventInfo_v1.cxx:127
VP1Gui.h
VP1Gui::cleanup
void cleanup()
Definition: VP1Gui.cxx:205
VP1Alg::m_initialvp1files
Gaudi::Property< std::vector< std::string > > m_initialvp1files
Definition: VP1Alg.h:65
VP1Gui::init
void init()
Definition: VP1Gui.cxx:181
skel.it
it
Definition: skel.GENtoEVGEN.py:423
VP1Gui::executeNewEvent
bool executeNewEvent(const int &run, const uint64_t &event, const unsigned &triggerType=0, const unsigned &time=0)
Definition: VP1Gui.cxx:212
VP1Alg::m_mfSourceDir
Gaudi::Property< std::string > m_mfSourceDir
Definition: VP1Alg.h:54
VP1Alg::m_cruiseInitialMode
Gaudi::Property< std::string > m_cruiseInitialMode
Definition: VP1Alg.h:61
VP1Alg::m_noGui
Gaudi::Property< bool > m_noGui
Definition: VP1Alg.h:68
VP1Alg::m_mfOn
Gaudi::Property< bool > m_mfOn
Definition: VP1Alg.h:50
xAOD::EventInfo_v1::runNumber
uint32_t runNumber() const
The current event's run number.
TruthTest.itE
itE
Definition: TruthTest.py:25
AthenaPoolTestRead.sc
sc
Definition: AthenaPoolTestRead.py:27
AthCommonDataStore< AthCommonMsg< Algorithm > >::detStore
const ServiceHandle< StoreGateSvc > & detStore() const
The standard StoreGateSvc/DetectorStore Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:95
VP1Alg::execute
virtual StatusCode execute() override
Definition: VP1Alg.cxx:134
AthCommonDataStore< AthCommonMsg< Algorithm > >::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
The standard StoreGateSvc (event store) Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:85
TileAANtupleConfig.inputCollections
inputCollections
Definition: TileAANtupleConfig.py:133
VP1Alg::finalize
virtual StatusCode finalize() override
Definition: VP1Alg.cxx:194
lumiFormat.i
int i
Definition: lumiFormat.py:92
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
VP1Alg::m_mfAvailableLocalInputDirectories
Gaudi::Property< std::vector< std::string > > m_mfAvailableLocalInputDirectories
Definition: VP1Alg.h:58
file
TFile * file
Definition: tile_monitor.h:29
VP1Gui::hasBeenInitialised
bool hasBeenInitialised()
Definition: VP1Gui.cxx:175
xAOD::eventNumber
eventNumber
Definition: EventInfo_v1.cxx:124
getCastorRun.setenv
def setenv()
Definition: getCastorRun.py:9
calibdata.exit
exit
Definition: calibdata.py:236
AthAlgorithm
Definition: AthAlgorithm.h:47
PathResolver.h
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
VP1Gui::userRequestedFiles
std::vector< std::string > userRequestedFiles()
Definition: VP1Gui.cxx:237
VP1Alg::~VP1Alg
~VP1Alg()
Definition: VP1Alg.cxx:77
EventInfo.h
xAOD::EventInfo_v1
Class describing the basic event information.
Definition: EventInfo_v1.h:43
DeMoAtlasDataLoss.runNumber
string runNumber
Definition: DeMoAtlasDataLoss.py:64
CaloSwCorrections.time
def time(flags, cells_name, *args, **kw)
Definition: CaloSwCorrections.py:242
DEBUG
#define DEBUG
Definition: page_access.h:11
AthCommonMsg< Algorithm >::msg
MsgStream & msg() const
Definition: AthCommonMsg.h:24
VP1Alg::m_mfLimit
Gaudi::Property< int > m_mfLimit
Definition: VP1Alg.h:52
xAOD::EventInfo_v1::level1TriggerType
uint16_t level1TriggerType() const
The Level-1 trigger type.
VP1Alg::handle
void handle(const Incident &inc) override
Definition: VP1Alg.cxx:209
xAOD::EventInfo_v1::timeStamp
uint32_t timeStamp() const
POSIX time in seconds from 1970. January 1st.
VP1FileUtilities.h
VP1Gui::argumentsAreValid
bool argumentsAreValid() const
Definition: VP1Gui.cxx:92
VP1Alg::m_toolSvc
IToolSvc * m_toolSvc
Definition: VP1Alg.h:75
VP1Gui::nextRequestedEventFile
std::string nextRequestedEventFile() const
Definition: VP1Gui.cxx:231
merge.status
status
Definition: merge.py:17
VP1Alg.h
VP1Alg::m_mfLocalCopyDir
Gaudi::Property< std::string > m_mfLocalCopyDir
Definition: VP1Alg.h:56
VP1Alg::m_cruiseInitialUpdateSeconds
Gaudi::Property< unsigned > m_cruiseInitialUpdateSeconds
Definition: VP1Alg.h:63
VP1Alg::VP1Alg
VP1Alg(const std::string &name, ISvcLocator *pSvcLocator)
Definition: VP1Alg.cxx:28
ServiceHandle< IEvtSelector >