ATLAS Offline Software
VP1LocalEvtRetriever.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 "VP1LocalEvtRetriever.h"
7 
8 #include <QFile>
9 #include <QFileInfo>
10 #include <QTimer>
11 #include <QMutexLocker>
12 
14  , QString sourcedir
15  , QObject* parent)
16  : QThread(parent)
17  , m_availEvts(availEvts)
18  , m_sourcedir(sourcedir)
19  , m_timer(0)
20 {
21 }
22 
24 {
25  delete m_timer;
26 }
27 
29 {
30  m_timer = new QTimer(0);
31  connect(m_timer, SIGNAL(timeout()), this, SLOT(updateLocalDirFromSource()),Qt::DirectConnection);
32  m_timer->start(3000);
34  exec();
35 }
36 
38 {
39  QMutexLocker locker(&m_mutex);
41 }
42 
44 {
45  QList<VP1EventFile> evts = m_availEvts->allEventFilesInDir(m_sourcedir);
46  if (evts.isEmpty()) {
47  m_availEvts->cleanupAndCheckForEventListChanges();//In case we e.g. changed to an empty input directory
48  return;
49  }
50  //We don't copy more than enough to give us 3 fresh files. But if
51  //there are more available, we will check back in 3 seconds.
52 
53  const int nfreshneeded = 3 - m_availEvts->freshEvents().count();
54  if (evts.count()>m_availEvts->maxLocalFilesToKeep()+3&&nfreshneeded<=0) {
55  //Don't copy anything, but schedule cleanup and check back in 3 seconds:
57 // --- todo? QTimer::singleShot(3000, this, SLOT(updateLocalDirFromSource()));
58  return;
59  }
60 
61  static unsigned ntmpdlcount = 0;
62 
63  QList<VP1EventFile> freshEvtsInSource = m_availEvts->freshEvents(evts.front(), evts);
64 
65  QString ad = m_availEvts->tmpActiveRetrievalDir();
66  QString tmplocal = m_availEvts->tmpLocalFileDir();
67  bool changestmplocal(false), changesad(false);
68  if (!ad.isEmpty()&&!tmplocal.isEmpty()) {
69  int ncopied(0);
70  for (VP1EventFile evt : freshEvtsInSource) {
71  QString fn = evt.fileName();
72  QString basefn = QFileInfo(fn).fileName();
73  QString target = tmplocal + basefn;
74  if (!QFile::exists(target)) {
75  QString tmptarget = ad+basefn+"_"+QString::number(ntmpdlcount++);
76  if (!QFile::copy(fn,tmptarget)) {
77  m_availEvts->message("Problems copying "+fn+" to "+tmptarget);
78  changesad = true;
79  } else {
80  if (!QFile::rename(tmptarget,target)) {
81  m_availEvts->message("Problems moving "+tmptarget+" to "+target+" directory");
82  changesad = true;
83  } else {
84  if (!QFile::exists(target)) {
85  m_availEvts->message("Inconsistent copy of "+basefn+" to local tmp directory");
86  } else {
87  ++ncopied;
88  m_availEvts->messageDebug("Copied "+basefn+" to local tmp directory");//Fixme: verbose!
89  changestmplocal = true;
90  }
91  }
92  }
93  }
94  if (ncopied>=nfreshneeded) {
95 // -- todo? QTimer::singleShot(3000, this, SLOT(updateLocalDirFromSource()));
96  break;
97  }
98  }
99  } else {
100  m_availEvts->message("Problems with temporary local directories.");
101  }
102  if (changestmplocal)
103  m_availEvts->invalidateDirCache(tmplocal);
104  if (changesad)
107 }
108 
110 {
111  QMutexLocker locker(&m_mutex);
112 
113  if (!dir.endsWith("/"))
114  dir += "/";
115  if (m_sourcedir==dir)
116  return;
117 
118  //Ensure that the next event is from the new directory by removing
119  //all cached fresh events:
120 
121  while (true) {
122  QList<VP1EventFile> freshevts = m_availEvts->freshEvents();
123  if (freshevts.isEmpty())
124  break;
125  for(VP1EventFile evt : freshevts) {
126  QString fn = evt.fileName();
127  if (!QFile::remove(fn))
128  m_availEvts->message("ERROR: Could not remove "+fn);
129  }
130  }
131 
132  //Now change directory
133  m_sourcedir = dir;
134  updateLocalDir();
135 }
VP1LocalEvtRetriever::VP1LocalEvtRetriever
VP1LocalEvtRetriever(VP1AvailEvtsLocalDir *, QString, QObject *parent=0)
Definition: VP1LocalEvtRetriever.cxx:13
VP1LocalEvtRetriever::updateLocalDirFromSource
void updateLocalDirFromSource()
Definition: VP1LocalEvtRetriever.cxx:37
VP1LocalEvtRetriever.h
VP1AvailEvents::tmpLocalFileDir
QString tmpLocalFileDir() const
Definition: VP1AvailEvents.cxx:283
VP1LocalEvtRetriever::updateLocalDir
void updateLocalDir()
Definition: VP1LocalEvtRetriever.cxx:43
VP1LocalEvtRetriever::m_availEvts
VP1AvailEvtsLocalDir * m_availEvts
Definition: VP1LocalEvtRetriever.h:37
fillPileUpNoiseLumi.connect
string connect
Definition: fillPileUpNoiseLumi.py:70
VP1AvailEvents::cleanupAndCheckForEventListChanges
void cleanupAndCheckForEventListChanges()
Definition: VP1AvailEvents.cxx:176
VP1AvailEvents::tmpActiveRetrievalDir
QString tmpActiveRetrievalDir()
Definition: VP1AvailEvents.cxx:277
LArG4FSStartPointFilter.evt
evt
Definition: LArG4FSStartPointFilter.py:42
VP1AvailEvents::message
void message(const QString &) const
VP1AvailEvents::freshEvents
QList< VP1EventFile > freshEvents() const
Definition: VP1AvailEvents.cxx:129
PixelModuleFeMask_create_db.remove
string remove
Definition: PixelModuleFeMask_create_db.py:83
python.getCurrentFolderTag.fn
fn
Definition: getCurrentFolderTag.py:65
VP1LocalEvtRetriever::run
void run()
Definition: VP1LocalEvtRetriever.cxx:28
VP1LocalEvtRetriever::m_mutex
QMutex m_mutex
Definition: VP1LocalEvtRetriever.h:40
VP1AvailEvtsLocalDir.h
LArG4FSStartPointFilter.exec
exec
Definition: LArG4FSStartPointFilter.py:103
test_pyathena.parent
parent
Definition: test_pyathena.py:15
VP1LocalEvtRetriever::~VP1LocalEvtRetriever
~VP1LocalEvtRetriever()
Definition: VP1LocalEvtRetriever.cxx:23
beamspotman.dir
string dir
Definition: beamspotman.py:623
python.selection.number
number
Definition: selection.py:20
VP1LocalEvtRetriever::m_timer
QTimer * m_timer
Definition: VP1LocalEvtRetriever.h:39
VP1LocalEvtRetriever::setSourceDir
void setSourceDir(QString)
Definition: VP1LocalEvtRetriever.cxx:109
VP1AvailEvents::maxLocalFilesToKeep
int maxLocalFilesToKeep() const
Definition: VP1AvailEvents.cxx:98
VP1EventFile
Definition: VP1EventFile.h:23
copySelective.target
string target
Definition: copySelective.py:37
VP1AvailEvents::allEventFilesInDir
QList< VP1EventFile > allEventFilesInDir(const QString &dir) const
Definition: VP1AvailEvents.cxx:202
calibdata.copy
bool copy
Definition: calibdata.py:27
python.dummyaccess.exists
def exists(filename)
Definition: dummyaccess.py:9
VP1AvailEvents::messageDebug
void messageDebug(const QString &) const
Definition: VP1AvailEvents.cxx:351
python.TrigInDetArtSteps.timeout
timeout
Definition: TrigInDetArtSteps.py:35
VP1AvailEvtsLocalDir
Definition: VP1AvailEvtsLocalDir.h:24
VP1AvailEvents::invalidateDirCache
void invalidateDirCache(const QString &dir)
Definition: VP1AvailEvents.cxx:194
VP1LocalEvtRetriever::m_sourcedir
QString m_sourcedir
Definition: VP1LocalEvtRetriever.h:38