Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix handling of ATLAS AOD files with specific compression method #710

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions packages/phoenix-event-display/src/loaders/jsroot-event-loader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { PhoenixLoader } from './phoenix-loader';
import { openFile } from 'jsroot';
import { decompress } from 'some-compression-library'; // Add the necessary import for the compression library

/**
* PhoenixLoader for processing and loading an event from ".root".
Expand Down Expand Up @@ -59,9 +60,58 @@ export class JSRootEventLoader extends PhoenixLoader {
}
});
}
}).catch((error: any) => {
if (error.message.includes('unsupported compression')) {
this.handleUnsupportedCompression(objects, onEventData);
} else {
console.error('Error opening file:', error);
}
});
}

/**
* Handle unsupported compression method for ATLAS AOD files.
* @param objects An array identifying objects inside the ".root" file.
* @param onEventData Callback when event data is extracted and available for use.
*/
private handleUnsupportedCompression(
objects: string[],
onEventData: (eventData: any) => void,
) {
fetch(this.rootFileURL)
.then((response) => response.arrayBuffer())
.then((buffer) => {
const decompressedData = decompress(buffer); // Decompress the data using the specific compression method
openFile(decompressedData).then((file: any) => {
let i = 0;
for (const objectName of objects) {
file.readObject(objectName).then((object: any) => {
i++;
if (object) {
this.processItemsList(object);
}
if (i === objects.length) {
for (const objectType of [
'Hits',
'Tracks',
'Jets',
'CaloClusters',
]) {
if (Object.keys(this.fileEventData[objectType]).length === 0) {
this.fileEventData[objectType] = undefined;
}
}
onEventData(this.fileEventData);
}
});
}
});
})
.catch((error) => {
console.error('Error handling unsupported compression:', error);
});
}

/**
* Process the list of items inside the JSROOT files for relevant event data.
* @param obj Object containing the event data in the form of JSROOT classes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
* @jest-environment jsdom
*/
import { JSRootEventLoader } from '../../loaders/jsroot-event-loader';
import { decompress } from 'some-compression-library'; // Add the necessary import for the compression library

describe('JSRootEventLoader', () => {
let jsrootLoader: JSRootEventLoader;
const TEST_ROOT_FILE = 'assets/tracks_hits.root';
const TEST_ATLAS_AOD_FILE = 'assets/atlas_aod.root';

beforeEach(() => {
jsrootLoader = new JSRootEventLoader(TEST_ROOT_FILE);
Expand Down Expand Up @@ -94,4 +96,23 @@ describe('JSRootEventLoader', () => {
expect((jsrootLoader as any).getTEveTrack(undefined)).toBeFalsy();
expect((jsrootLoader as any).getTEveTrack({ fN: [] })).toBeFalsy();
});

it('should handle unsupported compression for ATLAS AOD files', async () => {
jsrootLoader = new JSRootEventLoader(TEST_ATLAS_AOD_FILE);
const mockDecompressedData = new ArrayBuffer(8);
const mockFetchResponse = {
arrayBuffer: jest.fn().mockResolvedValue(mockDecompressedData),
};
global.fetch = jest.fn().mockResolvedValue(mockFetchResponse as any);
(decompress as jest.Mock) = jest.fn().mockReturnValue(mockDecompressedData);

const objects = ['tracks;1', 'hits;1'];
const onEventData = jest.fn();

await jsrootLoader.getEventData(objects, onEventData);

expect(fetch).toHaveBeenCalledWith(TEST_ATLAS_AOD_FILE);
expect(decompress).toHaveBeenCalledWith(mockDecompressedData);
expect(onEventData).toHaveBeenCalled();
});
});
Loading