Back to home page

EIC code displayed by LXR

 
 

    


Warning, /firebird/firebird-ng/src/app/components/io-options-dialog/io-options-dialog.component.ts is written in an unsupported language. File is not indexed.

0001 import { OnInit, Component, Input } from '@angular/core';
0002 import {
0003   CMSLoader,
0004   JiveXMLLoader,
0005   readZipFile,
0006   Edm4hepJsonLoader,
0007 } from 'phoenix-event-display';
0008 
0009 import { MatDialogRef } from '@angular/material/dialog';
0010 import {EventDataFormat, EventDataImportOption, EventDisplayService, ImportOption} from "phoenix-ui-components";
0011 import {Cache} from "three";
0012 import files = Cache.files;
0013 import {EicEdm4hepJsonLoader} from "../../phoenix-overload/eic-edm4hep-json-loader";
0014 
0015 @Component({
0016   selector: 'app-io-options-dialog',
0017   templateUrl: './io-options-dialog.component.html',
0018   styleUrls: ['./io-options-dialog.component.scss'],
0019   standalone: true,
0020 })
0021 export class IOOptionsDialogComponent implements OnInit {
0022 
0023   constructor(
0024     private eventDisplay: EventDisplayService,
0025     public dialogRef: MatDialogRef<IOOptionsDialogComponent>,
0026   ) {}
0027 
0028   ngOnInit() {
0029 
0030   }
0031 
0032 
0033   onClose(): void {
0034     this.dialogRef.close();
0035   }
0036 
0037   getFirstFileFromEvent(event: Event): File|null {
0038 
0039     // Check if the target is actually an HTMLInputElement
0040     if (!(event.target instanceof HTMLInputElement)) {
0041       console.error("Event target is not an HTML input element.");
0042       return null;
0043     }
0044 
0045     const files = event.target.files;
0046     if (!files || files.length === 0) {
0047       console.error("No files selected.");
0048       return null;
0049     }
0050 
0051     return files[0];
0052 
0053   }
0054 
0055   handleJSONEventDataInput(event: Event) {
0056     let file = this.getFirstFileFromEvent(event);
0057     if (!file) return;   // If not file it is already reported
0058 
0059     this.readTextFile(file, (content: string) => {
0060       this.eventDisplay.parsePhoenixEvents(JSON.parse(content));
0061     });
0062   }
0063 
0064   handleEdm4HepJsonEventDataInput(event: Event) {
0065     let file = this.getFirstFileFromEvent(event);
0066     if (!file) return;   // If not file it is already reported
0067     const callback = (content: any) => {
0068       const json = typeof content === 'string' ? JSON.parse(content) : content;
0069       const edm4hepJsonLoader = new EicEdm4hepJsonLoader();
0070       edm4hepJsonLoader.setRawEventData(json);
0071       edm4hepJsonLoader.processEventData();
0072       this.eventDisplay.parsePhoenixEvents(edm4hepJsonLoader.getEventData());
0073     };
0074     this.readTextFile(file,  callback);
0075   }
0076 
0077   handleJiveXMLDataInput(element: HTMLInputElement| null) {
0078     let files = element?.files;
0079     if(!files) return;
0080 
0081     if(!files) return;
0082     const callback = (content: any) => {
0083       const jiveloader = new JiveXMLLoader();
0084       jiveloader.process(content);
0085       const eventData = jiveloader.getEventData();
0086       this.eventDisplay.buildEventDataFromJSON(eventData);
0087     };
0088     this.readTextFile(files[0], callback);
0089   }
0090 
0091   handleOBJInput(element: HTMLInputElement| null) {
0092     let files = element?.files;
0093     if(!files) return;
0094 
0095     const callback = (content: any) => {
0096       this.eventDisplay.parseOBJGeometry(content, files[0].name);
0097     };
0098     if(files) {
0099       this.readTextFile(files[0], callback);
0100     }
0101   }
0102 
0103   handleSceneInput(files: FileList) {
0104     const callback = (content: any) => {
0105       this.eventDisplay.parsePhoenixDisplay(content);
0106     };
0107     this.readTextFile(files[0], callback);
0108   }
0109 
0110   handleGLTFInput(files: FileList) {
0111     const callback = (content: any) => {
0112       this.eventDisplay.parseGLTFGeometry(content, files[0].name);
0113     };
0114     this.readTextFile(files[0], callback);
0115   }
0116 
0117   handlePhoenixInput(files: FileList| null) {
0118     if(!files) return;
0119     const callback = (content: any) => {
0120       this.eventDisplay.parsePhoenixDisplay(content);
0121     };
0122     this.readTextFile(files[0], callback);
0123   }
0124 
0125   async handleROOTInput(files: FileList) {
0126     const rootObjectName = prompt('Enter object name in ROOT file');
0127 
0128     await this.eventDisplay.loadRootGeometry(
0129       URL.createObjectURL(files[0]),
0130       rootObjectName ?? "",
0131       files[0].name.split('.')[0],
0132     );
0133 
0134     this.onClose();
0135   }
0136 
0137   async handleRootJSONInput(files: FileList) {
0138 
0139     const name = files[0].name.split('.')[0];
0140     await this.eventDisplay.loadRootJSONGeometry(
0141       URL.createObjectURL(files[0]),
0142       name,
0143     );
0144 
0145     this.onClose();
0146   }
0147 
0148   handleIgEventDataInput(files: FileList) {
0149     const cmsLoader = new CMSLoader();
0150     cmsLoader.readIgArchive(files[0], (allEvents: any[]) => {
0151       const allEventsData = cmsLoader.getAllEventsData(allEvents);
0152       this.eventDisplay.parsePhoenixEvents(allEventsData);
0153       this.onClose();
0154     });
0155   }
0156 
0157   async handleZipEventDataInput(files: FileList) {
0158 
0159     const allEventsObject = {};
0160     let filesWithData: { [fileName: string]: string };
0161 
0162     // Using a try catch block to catch any errors in Promises
0163     try {
0164       filesWithData = await readZipFile(files[0]);
0165     } catch (error) {
0166       console.error('Error while reading zip', error);
0167       this.eventDisplay.getInfoLogger().add('Could not read zip file', 'Error');
0168       return;
0169     }
0170 
0171     // JSON event data
0172     Object.keys(filesWithData)
0173       .filter((fileName) => fileName.endsWith('.json'))
0174       .forEach((fileName) => {
0175         Object.assign(allEventsObject, JSON.parse(filesWithData[fileName]));
0176       });
0177 
0178     // JiveXML event data
0179     const jiveloader = new JiveXMLLoader();
0180     Object.keys(filesWithData)
0181       .filter((fileName) => {
0182         return fileName.endsWith('.xml') || fileName.startsWith('JiveXML');
0183       })
0184       .forEach((fileName) => {
0185         jiveloader.process(filesWithData[fileName]);
0186         const eventData = jiveloader.getEventData();
0187         Object.assign(allEventsObject, { [fileName]: eventData });
0188       });
0189     // For some reason the above doesn't pick up JiveXML_XXX_YYY.zip
0190 
0191     this.eventDisplay.parsePhoenixEvents(allEventsObject);
0192 
0193     this.onClose();
0194   }
0195 
0196   readTextFile(file: File, callback: (result: string) => void) {
0197     const reader = new FileReader();
0198     reader.onload = () => {
0199         callback(reader?.result?.toString() ?? "");
0200       };
0201     reader.readAsText(file);
0202     this.onClose();
0203   }
0204 
0205   saveScene() {
0206     this.eventDisplay.exportPhoenixDisplay();
0207   }
0208 
0209   exportOBJ() {
0210     this.eventDisplay.exportToOBJ();
0211   }
0212 
0213   protected readonly HTMLInputElement = HTMLInputElement;
0214 }