Skip to content

Commit e098621

Browse files
committed
add a little ROOT
1 parent eacda83 commit e098621

6 files changed

Lines changed: 262 additions & 0 deletions

File tree

common-tools/clas-io/pom.xml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,43 @@
6060
<scope>compile</scope>
6161
</dependency>
6262

63+
<dependency>
64+
<groupId>org.jlab</groupId>
65+
<artifactId>groot</artifactId>
66+
<version>4.0.5</version>
67+
</dependency>
68+
6369
</dependencies>
6470

71+
<build>
72+
<plugins>
73+
<plugin>
74+
<groupId>org.codehaus.mojo</groupId>
75+
<artifactId>exec-maven-plugin</artifactId>
76+
<version>3.6.1</version>
77+
<configuration>
78+
<executable>scons</executable>
79+
<skip>false</skip>
80+
</configuration>
81+
<executions>
82+
<execution>
83+
<id>scons-1</id>
84+
<phase>package</phase>
85+
<goals><goal>exec</goal></goals>
86+
<configuration>
87+
<arguments><argument>target/native</argument></arguments>
88+
</configuration>
89+
</execution>
90+
<execution>
91+
<id>scons-2</id>
92+
<phase>package</phase>
93+
<goals><goal>exec</goal></goals>
94+
<configuration>
95+
<arguments><argument></argument></arguments>
96+
</configuration>
97+
</execution>
98+
</executions>
99+
</plugin>
100+
</plugins>
101+
</build>
65102
</project>

common-tools/clas-io/sconscript

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Import('env')
2+
3+
javah = env.Command('native', 'src/main/java/org/jlab/io/root/HoistJNI.java', ['javac -h $TARGET $SOURCE', Delete('src/main/java/org/jlab/io/root/HoistJNI.class')])
4+
AlwaysBuild(javah)
5+
6+
if 'JAVA_HOME' in env['ENV']:
7+
env['JAVA_HOME'] = env['ENV']['JAVA_HOME']
8+
env.Append(CPPPATH = ["$JAVA_HOME/include", "$JAVA_HOME/include/linux"])
9+
10+
elif env['PLATFORM'] == 'darwin':
11+
env.ParseConfig("echo -I`/usr/libexec/java_home`/include")
12+
env.ParseConfig("echo -I`/usr/libexec/java_home`/include/darwin")
13+
14+
if env['PLATFORM'] == 'darwin':
15+
env.ParseConfig("echo -Wl,-rpath,`root-config --libdir`")
16+
17+
env.Append(CPPPATH = ['native'])
18+
env.ParseConfig("root-config --libs --cflags")
19+
env.SharedLibrary('hoistJNI', Glob('*.cpp'))

common-tools/clas-io/sconstruct

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import os
2+
3+
env = Environment(ENV=os.environ)
4+
Export('env')
5+
env.SConscript('sconscript', variant_dir='target', duplicate=0)
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
#include <iostream>
2+
#include <unordered_map>
3+
#include <mutex>
4+
#include <jni.h>
5+
#include <stdio.h>
6+
#include <TROOT.h>
7+
#include <TSystem.h>
8+
#include <TString.h>
9+
#include <TFile.h>
10+
#include <TNtuple.h>
11+
#include <TString.h>
12+
#include <TH1F.h>
13+
#include <TH2F.h>
14+
#include <TMap.h>
15+
#include <TGraphErrors.h>
16+
#include "org_jlab_io_root_HoistJNI.h"
17+
18+
class concurrentMap
19+
{
20+
std::mutex m_;
21+
std::unordered_map<std::string, TObject*> objs;
22+
23+
public:
24+
TObject* get(std::string k) {
25+
std::unique_lock<decltype(m_)> lock(m_);
26+
return objs[k];
27+
}
28+
29+
void set(std::string k, TObject* v) {
30+
std::unique_lock<decltype(m_)> lock(m_);
31+
objs[k] = v;
32+
}
33+
};
34+
35+
concurrentMap objects;
36+
37+
static __attribute__((constructor)) void init() {
38+
gSystem->ResetSignals();
39+
ROOT::EnableThreadSafety();
40+
gROOT->SetBatch(true);
41+
}
42+
43+
JNIEXPORT void JNICALL Java_org_jlab_io_root_HoistJNI_createFile (JNIEnv *env, jobject thisObj, jstring jfname) {
44+
const char *fname = env->GetStringUTFChars(jfname, NULL);
45+
TFile* ff = new TFile(fname, "RECREATE");
46+
objects.set(fname, ff);
47+
env->ReleaseStringUTFChars(jfname, fname);
48+
}
49+
50+
JNIEXPORT void JNICALL Java_org_jlab_io_root_HoistJNI_closeFile (JNIEnv *env, jobject thisObj, jstring jfname) {
51+
const char *fname = env->GetStringUTFChars(jfname, NULL);
52+
((TFile*) objects.get(fname))->Close();
53+
env->ReleaseStringUTFChars(jfname, fname);
54+
}
55+
56+
JNIEXPORT void JNICALL Java_org_jlab_io_root_HoistJNI_mkdir( JNIEnv *env, jobject thisObj, jstring jfname, jstring jpath) {
57+
const char *fname = env->GetStringUTFChars(jfname, NULL);
58+
const char *path = env->GetStringUTFChars(jpath, NULL);
59+
((TFile*) objects.get(fname))->mkdir(path);
60+
env->ReleaseStringUTFChars(jfname, fname);
61+
env->ReleaseStringUTFChars(jpath, path);
62+
}
63+
64+
JNIEXPORT void JNICALL Java_org_jlab_io_root_HoistJNI_writeH1F (JNIEnv *env, jobject thisObj, jstring jfname, jstring jpath,
65+
jstring jname, jstring jtitle, jint nbins, jdouble xmin, jdouble xmax, jfloatArray jdata) {
66+
67+
const char *fname = env->GetStringUTFChars(jfname, NULL);
68+
const char *path = env->GetStringUTFChars(jpath, NULL);
69+
const char *name = env->GetStringUTFChars(jname, NULL);
70+
const char *title = env->GetStringUTFChars(jtitle, NULL);
71+
72+
float *cdata = env->GetFloatArrayElements(jdata, NULL);
73+
74+
TFile* ff = (TFile*) objects.get(fname);
75+
if(!ff->Get(path))
76+
ff->mkdir(path);
77+
ff->cd(path);
78+
79+
TH1F* h1 = new TH1F(name, title, nbins, xmin, xmax);
80+
jsize length = env->GetArrayLength(jdata);
81+
double nentries = 0;
82+
83+
for(int ib=0;ib<length;ib++) {
84+
h1->SetBinContent(ib+1, cdata[ib]);
85+
nentries += cdata[ib];
86+
}
87+
88+
h1->SetEntries(nentries);
89+
h1->Write("",TObject::kOverwrite);
90+
91+
env->ReleaseFloatArrayElements(jdata, cdata, 0);
92+
env->ReleaseStringUTFChars(jfname, fname);
93+
env->ReleaseStringUTFChars(jpath, path);
94+
env->ReleaseStringUTFChars(jname, name);
95+
env->ReleaseStringUTFChars(jtitle, title);
96+
}
97+
98+
JNIEXPORT void JNICALL Java_org_jlab_io_root_HoistJNI_writeH2F (JNIEnv *env, jobject thisObj, jstring jfname, jstring jpath,
99+
jstring jname, jstring jtitle, jint nxbins, jdouble xmin, jdouble xmax, jint nybins, jdouble ymin, jdouble ymax, jfloatArray jdata) {
100+
101+
const char *fname = env->GetStringUTFChars(jfname, NULL);
102+
const char *path = env->GetStringUTFChars(jpath, NULL);
103+
const char *name = env->GetStringUTFChars(jname, NULL);
104+
const char *title = env->GetStringUTFChars(jtitle, NULL);
105+
106+
float *cdata = env->GetFloatArrayElements(jdata, NULL);
107+
108+
TFile* ff = (TFile*) objects.get(fname);
109+
if(!ff->Get(path))
110+
ff->mkdir(path);
111+
ff->cd(path);
112+
113+
TH2F* h2 = new TH2F(name, title, nxbins, xmin, xmax, nybins, ymin, ymax);
114+
double nentries = 0;
115+
int ii = 0;
116+
117+
for(int ix=0;ix<nxbins;ix++)
118+
for(int iy=0;iy<nybins;iy++) {
119+
h2->SetBinContent(ix+1, iy+1, cdata[ii]);
120+
nentries += cdata[ii++];
121+
}
122+
123+
h2->SetEntries(nentries);
124+
h2->Write("",TObject::kOverwrite);
125+
126+
env->ReleaseFloatArrayElements(jdata, cdata, 0);
127+
env->ReleaseStringUTFChars(jfname, fname);
128+
env->ReleaseStringUTFChars(jpath, path);
129+
env->ReleaseStringUTFChars(jname, name);
130+
env->ReleaseStringUTFChars(jtitle, title);
131+
}
132+
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package org.jlab.io.root;
2+
3+
import org.jlab.groot.data.H1F;
4+
import org.jlab.groot.data.H2F;
5+
6+
public class Hoist {
7+
8+
HoistJNI hoist = new HoistJNI();
9+
10+
private String fname;
11+
private String path = "";
12+
13+
public Hoist(String fname) {
14+
this.fname = fname;
15+
hoist.createFile(fname);
16+
}
17+
18+
public void close() {
19+
hoist.closeFile(fname);
20+
}
21+
22+
public void mkdir(String path) {
23+
path = path.replaceFirst("^/*","").replaceAll("/*\\$","");
24+
hoist.mkdir(fname, path);
25+
this.path = path;
26+
}
27+
28+
public void cd(String path) {
29+
this.path = path.replaceFirst("^/*","").replaceAll("/*\\$","");
30+
}
31+
32+
public void write(H1F h1) {
33+
String fullpath = path + "/" + h1.getName();
34+
int ind = fullpath.lastIndexOf("/");
35+
String relpath = fullpath.substring(0,ind);
36+
relpath = relpath.replaceFirst("^/*","").replaceAll("/*\\$","");
37+
String name = fullpath.substring(ind+1);
38+
hoist.writeH1F(fname, relpath, name,
39+
h1.getXaxis().getNBins(), h1.getXaxis().min(), h1.getXaxis().max(),
40+
h1.getData());
41+
}
42+
43+
public void write(H2F h2) {
44+
String fullpath = path + "/" + h2.getName();
45+
int ind = fullpath.lastIndexOf("/");
46+
String relpath = fullpath.substring(0,ind);
47+
relpath = relpath.replaceFirst("^/*","").replaceAll("/*\\$","");
48+
String name = fullpath.substring(ind+1);
49+
int xsize = h2.getDataSize(0), ysize = h2.getDataSize(1), ii = 0;
50+
float[] data = new float[xsize*ysize];
51+
for(int ix=0;ix<xsize;ix++)
52+
for(int iy=0;iy<ysize;iy++)
53+
data[ii++] = (float) h2.getData(ix,iy);
54+
hoist.writeH2F(fname, relpath, name,
55+
h2.getXAxis().getNBins(), h2.getXAxis().min(), h2.getXAxis().max(),
56+
h2.getYAxis().getNBins(), h2.getYAxis().min(), h2.getYAxis().max(),
57+
data);
58+
}
59+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.jlab.io.root;
2+
3+
public class HoistJNI {
4+
static { System.loadLibrary("hoistJNI"); }
5+
public native void createFile(String fname);
6+
public native void closeFile(String fname);
7+
public native void mkdir(String fname,String path);
8+
public native void writeH1F(String fname, String path, String hname, int nbins, double xmin, double xmax, float[] arr);
9+
public native void writeH2F(String fname, String path, String hname, int nxbins, double xmin, double xmax, int nybins, double ymin, double ymax, float[] arr);
10+
}

0 commit comments

Comments
 (0)