Skip to content

Commit

Permalink
added the java code examples to the repository
Browse files Browse the repository at this point in the history
  • Loading branch information
gavalian committed Dec 2, 2024
1 parent a9c2869 commit c1ee9dd
Show file tree
Hide file tree
Showing 4 changed files with 252 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ __pycache__
/build*/
/install*/
/.cache
/java/target/
86 changes: 86 additions & 0 deletions java/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>j4np</groupId>
<artifactId>hipo5</artifactId>
<version>${revision}</version>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>j4np</groupId>
<artifactId>j4np-data</artifactId>
<version>${version}</version>
</dependency>
</dependencies>

<repositories>
<repository>
<id>clas12-maven</id>
<url>https://clasweb.jlab.org/clas12maven/j4np/maven</url>
</repository>
</repositories>

<distributionManagement>
<repository>
<id>github</id>
<name>Twig Distribution</name>
<url>https://maven.pkg.github.com/gavalian/hipo</url>
</repository>
</distributionManagement>

<build>
<extensions>
<extension>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-ssh-external</artifactId>
<version>2.8</version>
</extension>
</extensions>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>twig.demo.RunDemo</mainClass>
</manifest>
</archive>
<finalName>hipo5-${revision}-core</finalName>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<!--
<distributionManagement>
<repository>
<id>ssh-clasweb</id>
<url>scpexe://[email protected]/group/clas/www/clasweb/html/jhep/maven</url>
</repository>
</distributionManagement>
-->
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<revision>5.0.0</revision>
<version>1.1.1</version>
</properties>

</project>
62 changes: 62 additions & 0 deletions java/src/main/java/j4np/hipo5/examples/ReadFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package j4np.hipo5.examples;

import j4np.hipo5.data.Bank;
import j4np.hipo5.data.Event;
import j4np.hipo5.io.HipoReader;

/**
*
* @author gavalian
*/
public class ReadFile {

/**
* Reads the file created by code WriteFile.java. Reads the bank
* in each event, and then iterates over the rows of the bank
* and prints out clusters of type==5, and with number of hits
* in the cluster between 5 and 11 inclusive.
*/
public static void readFileWithBank(){
HipoReader r = new HipoReader("clusters.h5");
Bank[] banks = r.getBanks("data::clusters");
while(r.nextEvent(banks)){
int nrows = banks[0].getRows();
for(int row = 0; row < nrows; row++){
if(banks[0].getInt("type", row)==5){
int nhits = banks[0].getInt("n", row);
if(nhits>4&&nhits<12){
System.out.printf("%4d, %5d, %8.5f %8.5f %8.5f\n",
banks[0].getInt("type", row),nhits,
banks[0].getFloat("x", row),
banks[0].getFloat("y", row),
banks[0].getFloat("z", row));
}
}
}
}
}

/**
* Reads each event in the file and prints out the information about the
* content of the event. prints all the objects in the event, with their
* unique identifiers, types and sizes
* @param file - the file name to scan
*/
public static void showFileContent(String file){
HipoReader r = new HipoReader(file);
Event event = new Event();

while(r.next(event)==true){
event.scanShow();
}
}

public static void main(String[] args){
ReadFile.readFileWithBank();
ReadFile.showFileContent("nodes.h5");
}
}
103 changes: 103 additions & 0 deletions java/src/main/java/j4np/hipo5/examples/WriteFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package j4np.hipo5.examples;

import j4np.hipo5.data.Bank;
import j4np.hipo5.data.Event;
import j4np.hipo5.data.Node;
import j4np.hipo5.data.Schema;
import j4np.hipo5.data.Schema.SchemaBuilder;
import j4np.hipo5.io.HipoWriter;
import java.util.Random;

/**
*
* @author gavalian
*/
public class WriteFile {
/**
* This example writes a file with one bank (table like structure)
* in each event, the schema must be first created and added to
* writer's schema factory to be saved. The example core on how to
* read the file created by this code can be found in ReadFile.java
* code.
*/
public static void writeFileWithBank(){

SchemaBuilder b = new SchemaBuilder("data::clusters",12,1)
.addEntry("type","B","cluster type") // B - type Byte
.addEntry("n", "S", "cluster multiplicity") // S - Type Short
.addEntry("x", "F", "x position") // F - type float
.addEntry("y","F","y position") // F - type float
.addEntry("z", "F", "z position"); // F - type Float

Schema schema = b.build();

HipoWriter w = new HipoWriter();
w.getSchemaFactory().addSchema(schema);

w.open("clusters.h5");

Random r = new Random();
r.setSeed(123456);

Event event = new Event();
for(int i = 0; i < 1200; i++){
event.reset();
int nrows = r.nextInt(12)+4; // rows 4-16 randomly generated
Bank bank = new Bank(schema,nrows);
for(int row = 0; row < nrows; row++){
bank.putByte( "type", row, (byte) (r.nextInt(8)+1));
bank.putShort( "n", row, (short) (r.nextInt(15)+1));
bank.putFloat( "x", row, r.nextFloat());
bank.putFloat( "y", row, r.nextFloat());
bank.putFloat( "z", row, r.nextFloat());
}

event.write(bank); // write the bank to the event, event can contain multiple
w.addEvent(event); // add event to the file
}
w.close();
}

public static void writeFileWithNodes(){
Random r = new Random();
r.setSeed(123456);

HipoWriter w = new HipoWriter();
w.open("nodes.h5");
Event event = new Event();

for(int i = 0; i < 200; i++){
int nbytes = r.nextInt(24)+6;
int nfloats = r.nextInt(24)+6;
int nints = r.nextInt(24)+6;

byte[] barray = new byte[nbytes];
int[] iarray = new int[nints];
float[] farray = new float[nfloats];

for(int j = 0; j < barray.length; j++) barray[j] = (byte) r.nextInt(128);
for(int j = 0; j < iarray.length; j++) iarray[j] = r.nextInt(2000) - 1000;
for(int j = 0; j < farray.length; j++) farray[j] = r.nextFloat()*2.0f;

Node nodeb = new Node(12,1,barray);
Node nodei = new Node(12,2,iarray);
Node nodef = new Node(12,3,farray);
event.reset();
event.write(nodeb);
event.write(nodei);
event.write(nodef);
w.addEvent(event);
}
w.close();
}

public static void main(String[] args){

WriteFile.writeFileWithBank();
WriteFile.writeFileWithNodes();
}
}

0 comments on commit c1ee9dd

Please sign in to comment.