Here you can find the source of LoadGhostMapFile(String filename, int n_ids, int n_ghosts)
Parameter | Description |
---|---|
filename | The name of the binary file containing the ghost mapping table. |
n_ids | The number of ids listed in the file. NOTE: the file must contain information for all ids from 0 to n_ids-1. The information for each id must be n_ghosts pairs of (int,double) in that order. |
n_ghosts | The number of DAS ids that are affected by each event. This corresponds to the number of "columns" of ghost histograms in Jason Hodges Python code. |
public static Vector LoadGhostMapFile(String filename, int n_ids, int n_ghosts)
//package com.java2s; /* /*from w ww . j av a 2 s. com*/ * File: FileUtil.java * * Copyright (C) 2010, Dennis Mikkelson * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. * * Contact : Dennis Mikkelson <mikkelsond@uwstout.edu> * Department of Mathematics, Statistics and Computer Science * University of Wisconsin-Stout * Menomonie, WI 54751, USA * * This work was supported by the Spallation Neutron Source Division * of Oak Ridge National Laboratory, Oak Ridge, TN, USA. * * Last Modified: * * $Author:$ * $Date:$ * $Revision:$ */ import java.io.*; import java.util.*; public class Main { /** * Get the table of (id,weight) pairs from the specified file. The * information is returned as two, two dimensional arrays in a Vector. * The first array is an NxM array of ints and the second array is an NxM * array of doubles. N is the number of detector pixels for which the * ghost mapping table was made. M is the number of affected pixels * for each event. The kth row of the array of ints lists the DAS * ID numbers of pixels that affected when an event is detected in * DAS pixel k. The kth row of the array of doubles lists the fractional * weight of the event to be added to the histogram for that DAS ID. * The file MUST be stored in little endian format as written by PYTHON * on a PC. This is NOT the form of a JAVA binary file, so this * method reverses the bytes before converting to int and double values. * * @param filename The name of the binary file containing the ghost * mapping table. * * @param n_ids The number of ids listed in the file. NOTE: the * file must contain information for all ids from * 0 to n_ids-1. The information for each id must * be n_ghosts pairs of (int,double) in that order. * * @param n_ghosts The number of DAS ids that are affected by each * event. This corresponds to the number of "columns" * of ghost histograms in Jason Hodges Python code. * * @return A vector containing a two dimensional array of ints in the * first entry and a two-dimensional array of doubles in the * second entry. The arrays have the number of rows specified * by n_ids and the number of columns specified by n_ghosts. */ public static Vector LoadGhostMapFile(String filename, int n_ids, int n_ghosts) { int bytes_per_record = n_ghosts * 12; // assuming each pair // is a 4-byte int and // an 8-byte double CheckFile(filename); File ghost_file = new File(filename); long file_size = ghost_file.length(); if (file_size % bytes_per_record != 0) throw new IllegalArgumentException(filename + " is not a ghost map."); if (file_size < bytes_per_record * n_ghosts) throw new IllegalArgumentException(filename + " only has records for " + file_size / bytes_per_record + " but needs records for " + n_ids + " detectors"); byte[] buffer = new byte[(int) file_size]; int[][] ids = new int[n_ids][n_ghosts]; double[][] weights = new double[n_ids][n_ghosts]; try { RandomAccessFile r_file = new RandomAccessFile(filename, "r"); r_file.seek(0); long bytes_read = r_file.read(buffer); if (bytes_read != file_size) throw new IllegalArgumentException(filename + " NOT read properly."); } catch (Exception ex) { throw new IllegalArgumentException("Error loading Ghosts: " + filename); } int index = 0; for (int id = 0; id < n_ids; id++) for (int ghost = 0; ghost < n_ghosts; ghost++) { ids[id][ghost] = getInt_32(buffer, index); index += 4; weights[id][ghost] = getDouble_64(buffer, index); index += 8; } Vector result = new Vector(); result.add(ids); result.add(weights); return result; } /** * Check that the specified name is the name of a file that exists and * can be read by the user. Throw an exception if the file can't be * read. * * @param filename * * @throws IllegalArgumentException if the file name is null, the * file doesn't exist, or the file can't be read. */ public static void CheckFile(String filename) { if (filename == null) throw new IllegalArgumentException("Filename String is NULL"); File file = new File(filename); if (!file.exists()) throw new IllegalArgumentException("File doesn't exist: " + filename); if (!file.canRead()) throw new IllegalArgumentException("File can't be read: " + filename); } /** * Decode the integer value stored in a sequence of * four bytes in the buffer. The four bytes determining * the Integer value are stored in the file and buffer in the * sequence: b0, b1, b2, b3, with the lowest order byte, b0, first * and the the highest order byte, b3, last. * * @param i The index of the first byte in the buffer * * @return The integer value represented by four successive bytes from * the file. */ public static int getInt_32(byte[] buffer, int i) { int val = 0; i += 3; // NOTE: When the signed byte is val |= buffer[i--] & 0xFF; // converted to int, it is sign val <<= 8; // extended, so the $0xFF is // needed. val |= buffer[i--] & 0xFF; val <<= 8; val |= buffer[i--] & 0xFF; val <<= 8; val |= buffer[i] & 0xFF; return val; } /** * Decode the double value stored in a sequence of eight bytes in * the buffer. The eight bytes determining the double value are * stored in the file and buffer in the sequence: b0,...,b3, with * the lowest order byte, b0, first and the the highest order byte, * b7, last. * NOTE: This method reverses the action of setDouble_64. * * @param i The index of the first byte in the buffer * * @return The double value represented by eight successive bytes from * the file. */ public static double getDouble_64(byte[] buffer, int i) { long long_val = 0; for (int shift = 0; shift < 64; shift += 8) long_val |= ((long) buffer[i++] & 0xFF) << shift; return Double.longBitsToDouble(long_val); } }