@@ -259,32 +259,39 @@ AlgorithmSpec AODJAlienReaderHelpers::rootFileReaderCallback(ConfigContext const
259259 TRY_NEXT_FILE ,
260260 TIMEFRAME_READ ,
261261 INVALID_TIMEFRAME ,
262- END_OF_INPUT ,
263262 };
264263 auto readState = TFReaderState::READ_FIRST_TABLE ;
265264 size_t routeIndex = 0 ;
266- while (readState == TFReaderState::READ_FIRST_TABLE ||
267- readState == TFReaderState::READ_FIRST_TABLE_FROM_NEXT_FILE ||
268- readState == TFReaderState::READ_NEXT_TABLE ||
269- readState == TFReaderState::TRY_NEXT_FILE ) {
270- if (readState == TFReaderState::TRY_NEXT_FILE ) {
271- fcnt += device.maxInputTimeslices ;
272- if (didir->atEnd (fcnt)) {
273- readState = TFReaderState::END_OF_INPUT ;
274- break ;
275- }
276- ntf = 0 ;
277- routeIndex = 0 ;
278- readState = TFReaderState::READ_FIRST_TABLE_FROM_NEXT_FILE ;
265+ auto reportTimeframe = [&](header::DataHeader const & dh) {
266+ if (reportTFN) {
267+ // TF number
268+ auto timeFrameNumber = didir->getTimeFrameNumber (dh, fcnt, ntf);
269+ auto o = Output (TFNumberHeader);
270+ outputs.make <uint64_t >(o) = timeFrameNumber;
279271 }
280272
273+ if (reportTFFileName) {
274+ // Origin file name for derived output map
275+ auto o2 = Output (TFFileNameHeader);
276+ auto fileAndFolder = didir->getFileFolder (dh, fcnt, ntf);
277+ auto rootFS = std::dynamic_pointer_cast<TFileFileSystem>(fileAndFolder.filesystem ());
278+ auto * f = dynamic_cast <TFile*>(rootFS->GetFile ());
279+ std::string currentFilename (f->GetFile ()->GetName ());
280+ if (strcmp (f->GetEndpointUrl ()->GetProtocol (), " file" ) == 0 && f->GetEndpointUrl ()->GetFile ()[0 ] != ' /' ) {
281+ // This is not an absolute local path. Make it absolute.
282+ static std::string pwd = gSystem ->pwd () + std::string (" /" );
283+ currentFilename = pwd + std::string (f->GetName ());
284+ }
285+ outputs.make <std::string>(o2) = currentFilename;
286+ }
287+ };
288+ auto tryReadTable = [&](TFReaderState currentState) -> TFReaderState {
281289 while (routeIndex < requestedTables.size () &&
282290 (device.inputTimesliceId % requestedTables[routeIndex].maxTimeslices ) != requestedTables[routeIndex].timeslice ) {
283291 ++routeIndex;
284292 }
285293 if (routeIndex == requestedTables.size ()) {
286- readState = TFReaderState::TIMEFRAME_READ ;
287- break ;
294+ return TFReaderState::TIMEFRAME_READ ;
288295 }
289296
290297 auto & route = requestedTables[routeIndex];
@@ -294,66 +301,56 @@ AlgorithmSpec AODJAlienReaderHelpers::rootFileReaderCallback(ConfigContext const
294301
295302 try {
296303 if (!didir->readTree (outputs, dh, fcnt, ntf, totalSizeCompressed, totalSizeUncompressed, wasAOD)) {
297- if (readState == TFReaderState::READ_FIRST_TABLE ) {
298- readState = TFReaderState::TRY_NEXT_FILE ;
299- continue ;
300- }
301- LOGP (fatal, " Can not retrieve tree for table {}: fileCounter {}, timeFrame {}" , concrete.origin .as <std::string>(), fcnt, ntf);
302- throw std::runtime_error (" Processing is stopped!" );
304+ return TFReaderState::TRY_NEXT_FILE ;
303305 }
304306 } catch (InvalidAODReadError const & e) {
305307 if (!skipInvalidReads) {
306308 throw ;
307309 }
308310 skipInvalidRead (concrete.origin , e);
309- readState = TFReaderState::INVALID_TIMEFRAME ;
310- break ;
311+ return TFReaderState::INVALID_TIMEFRAME ;
311312 }
312313
313- if (readState == TFReaderState::READ_FIRST_TABLE || readState == TFReaderState::READ_FIRST_TABLE_FROM_NEXT_FILE ) {
314- if (reportTFN) {
315- // TF number
316- auto timeFrameNumber = didir->getTimeFrameNumber (dh, fcnt, ntf);
317- auto o = Output (TFNumberHeader);
318- outputs.make <uint64_t >(o) = timeFrameNumber;
319- }
320-
321- if (reportTFFileName) {
322- // Origin file name for derived output map
323- auto o2 = Output (TFFileNameHeader);
324- auto fileAndFolder = didir->getFileFolder (dh, fcnt, ntf);
325- auto rootFS = std::dynamic_pointer_cast<TFileFileSystem>(fileAndFolder.filesystem ());
326- auto * f = dynamic_cast <TFile*>(rootFS->GetFile ());
327- std::string currentFilename (f->GetFile ()->GetName ());
328- if (strcmp (f->GetEndpointUrl ()->GetProtocol (), " file" ) == 0 && f->GetEndpointUrl ()->GetFile ()[0 ] != ' /' ) {
329- // This is not an absolute local path. Make it absolute.
330- static std::string pwd = gSystem ->pwd () + std::string (" /" );
331- currentFilename = pwd + std::string (f->GetName ());
332- }
333- outputs.make <std::string>(o2) = currentFilename;
334- }
314+ if (currentState == TFReaderState::READ_FIRST_TABLE || currentState == TFReaderState::READ_FIRST_TABLE_FROM_NEXT_FILE ) {
315+ reportTimeframe (dh);
335316 }
336317 ++routeIndex;
337- readState = TFReaderState::READ_NEXT_TABLE ;
338- }
339-
340- switch (readState) {
341- case TFReaderState::TIMEFRAME_READ :
342- break ;
343- case TFReaderState::INVALID_TIMEFRAME :
344- return ;
345- case TFReaderState::END_OF_INPUT :
346- LOGP (info, " No input files left to read for reader {}!" , device.inputTimesliceId );
347- didir->closeInputFiles ();
348- monitoring.flushBuffer ();
349- control.endOfStream ();
350- control.readyToQuit (QuitRequest::Me);
351- return ;
352- case TFReaderState::READ_FIRST_TABLE :
353- case TFReaderState::READ_FIRST_TABLE_FROM_NEXT_FILE :
354- case TFReaderState::READ_NEXT_TABLE :
355- case TFReaderState::TRY_NEXT_FILE :
356- throw std::logic_error (" Invalid timeframe read state" );
318+ return TFReaderState::READ_NEXT_TABLE ;
319+ };
320+ while (readState != TFReaderState::TIMEFRAME_READ ) {
321+ switch (readState) {
322+ case TFReaderState::READ_FIRST_TABLE :
323+ readState = tryReadTable (readState);
324+ break ;
325+ case TFReaderState::READ_FIRST_TABLE_FROM_NEXT_FILE :
326+ case TFReaderState::READ_NEXT_TABLE :
327+ readState = tryReadTable (readState);
328+ if (readState == TFReaderState::TRY_NEXT_FILE ) {
329+ // Once a file has been selected, every requested table must exist.
330+ auto concrete = DataSpecUtils::asConcreteDataMatcher (requestedTables[routeIndex].matcher );
331+ LOGP (fatal, " Can not retrieve tree for table {}: fileCounter {}, timeFrame {}" , concrete.origin .as <std::string>(), fcnt, ntf);
332+ throw std::runtime_error (" Processing is stopped!" );
333+ }
334+ break ;
335+ case TFReaderState::TRY_NEXT_FILE :
336+ fcnt += device.maxInputTimeslices ;
337+ if (didir->atEnd (fcnt)) {
338+ LOGP (info, " No input files left to read for reader {}!" , device.inputTimesliceId );
339+ didir->closeInputFiles ();
340+ monitoring.flushBuffer ();
341+ control.endOfStream ();
342+ control.readyToQuit (QuitRequest::Me);
343+ return ;
344+ }
345+ ntf = 0 ;
346+ routeIndex = 0 ;
347+ readState = TFReaderState::READ_FIRST_TABLE_FROM_NEXT_FILE ;
348+ break ;
349+ case TFReaderState::INVALID_TIMEFRAME :
350+ return ;
351+ case TFReaderState::TIMEFRAME_READ :
352+ break ;
353+ }
357354 }
358355 int64_t stopSize = totalSizeCompressed;
359356 int64_t bytesDelta = stopSize - startSize;
0 commit comments