|
| 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 | + |
0 commit comments