@@ -1319,7 +1319,7 @@ class BlockNectarCAMEventSource:
13191319
13201320 """
13211321
1322- def __init__ (self , block_size = 4 , allowed_blocks = None , ** kwargs ):
1322+ def __init__ (self , block_size = None , allowed_blocks = None , ** kwargs ):
13231323 self ._arguments = kwargs # blocks
13241324 self ._file_names = None
13251325 self ._block_file_names = list ()
@@ -1366,6 +1366,8 @@ def __init__(self, block_size=4, allowed_blocks=None, **kwargs):
13661366 self .show_empty_stats = bool (kwargs ["show_empty_stats" ])
13671367 del self ._arguments ["show_empty_stats" ]
13681368
1369+ if self .block_size is None :
1370+ self .block_size = self .guess_block_size_from_file (self ._file_names [0 ])
13691371 self ._create_blocks ()
13701372 self ._switch_block ()
13711373
@@ -1392,6 +1394,26 @@ def call_wrapper(*args, **kwargs):
13921394 else :
13931395 return attr_val
13941396
1397+ @staticmethod
1398+ def guess_block_size_from_file (filename ):
1399+ # try to open the file
1400+ # read the first events
1401+ # look at the separation between ids
1402+ try :
1403+ with File (str (filename )) as f :
1404+ ids = list ()
1405+ for e in f .Events :
1406+ ids .append (e .event_id )
1407+ if len (ids ) > 100 :
1408+ break
1409+ ids = np .array (ids , dtype = int )
1410+ block_size = int (np .rint (np .median (np .array (ids [1 :] - ids [:- 1 ]))))
1411+ # print(f"{self.block_size = }")
1412+ except Exception :
1413+ print ("Can't guess properly block size !" )
1414+ block_size = 4
1415+ return block_size
1416+
13951417 def _rewind (self ):
13961418 self ._current_block = None
13971419 self ._switch_block ()
0 commit comments