Exemplo n.º 1
0
uint8_t *
zip_read(app_t *app, const char *key, size_t *size)
{
	struct zip_stat stat;
	if(!zip_stat(app->io, key, 0, &stat))
	{
		size_t fsize = stat.size;
		struct zip_file *f = zip_fopen(app->io, key, 0);
		if(f)
		{
			uint8_t *str = malloc(fsize);
			if(str)
			{
				if(zip_fread(f, str, fsize) == -1)
				{
					free(str);
					str = NULL;
					fsize = 0;
				}
				else
				{
					; //success
				}
			}
			zip_fclose(f);

			*size = fsize;
			return str;
		}
	}

	*size = 0;
	return NULL;
}
Exemplo n.º 2
0
extern int FileStat( const char *path, struct stat *buf )
{
    int                 rc;
    struct zip_stat     zs;

    rc = -1;
    if( srcType == DS_ZIP ) {
        char        *alt_path;

        /* First try a file inside a ZIP archive */
        alt_path = flipBackSlashes( path );
        if( alt_path != NULL ) {
            rc = zip_stat( srcZip, alt_path, 0, &zs );
            if( rc == 0 ) {
                memset( buf, 0, sizeof( *buf ) );
                buf->st_ino   = zs.index;
                buf->st_size  = zs.size;
                buf->st_mtime = zs.mtime;
            }
            free( alt_path );
        }
    }
    if( rc != 0 ) {
        /* If that fails, try local file */
        rc = stat( path, buf );
    }
    return( rc );
}
Exemplo n.º 3
0
QJsonArray File::get_decoders()
{
    struct zip *archive;
    struct zip_file *zf;
    struct zip_stat zs;
    int ret;
    char *dec_file;
    QJsonArray dec_array;
    QJsonParseError error;

    archive = zip_open(_path.toLocal8Bit().data(), 0, &ret);
    if (archive) {
        /* read "decoders" */
        if (zip_stat(archive, "decoders", 0, &zs) != -1) {
            dec_file = (char *)g_try_malloc(zs.size);
            if (dec_file) {
                zf = zip_fopen_index(archive, zs.index, 0);
                zip_fread(zf, dec_file, zs.size);
                zip_fclose(zf);

                //QString sessionData = QString::fromUtf8(dec_file);
                QJsonDocument sessionDoc = QJsonDocument::fromJson(QByteArray::fromRawData(dec_file, zs.size), &error);
                dec_array = sessionDoc.array();
            }
        }
    }

    return dec_array;
}
Exemplo n.º 4
0
/*************************************************
 Function:		readZipFileName
 Descroption:
 Input:
	1.zip* z
	2.filetype
 Output:
 Return:
 Other:
*************************************************/
int readZipFileName(struct zip* z, char* filetype)
{
    int i;
    struct zip_stat fstat;

    if(z != NULL)
    {
        zip_stat_init(&fstat);
        int c = zip_get_num_files(z);
        if(c > 0)
        {
            for (i = 0 ; i < c; i++)
            {
                const char* name = zip_get_name(z, i, 0);
                if(name != NULL)
                {
                    zip_stat(z, name,0,&fstat);
                    //LOGI("File %i:%s Size: %lld Size2: %lld\n", i,fstat.name,fstat.size ,fstat.comp_size);
                }
            }
        }
    }
    else
    {
        return -1;
    }

    return 0;
}
Exemplo n.º 5
0
const bool CCZipFile::open(const char *file)
{
	// Grab the APK (zip) file to access resource
	m_apkArchive = zip_open( CCDeviceFileManager::apkPath.buffer, 0, NULL );
	if( m_apkArchive == NULL )
	{
		DEBUGLOG( "Error loading APK" );
		return false;
	}

    // It's not an image, so add res/raw/ to the path to look in the correct directory
	CCText fullFilePath = "res/raw/";
    fullFilePath += file;

	// Open the supplied file as read-only
	m_File = zip_fopen( m_apkArchive, fullFilePath.buffer, 0 );
	if( m_File != NULL )
	{
		// Get the file size
		struct zip_stat stat;
		zip_stat( m_apkArchive, fullFilePath.buffer, 0, &stat );
		m_Size = stat.size;
		return true;
	}

	DEBUGLOG( "Error loading file %s: %s", fullFilePath.buffer, strerror( errno ) );
    return false;
}
Exemplo n.º 6
0
ZipFileReader::ZipFileReader(ZipArchive& archive, const StringSlice& path) {
    struct zip_stat st;
    CString c_str(path);
    if (zip_stat(archive.c_obj(), c_str.data(), 0, &st) != 0) {
        throw Exception(format(
                    "{0}: {1}: {2}", archive.path(), path, zip_error(archive.c_obj())));
    }
    initialize(archive, st);
}
Exemplo n.º 7
0
bool ZIPProvider::ZIPHandle::isFile(const FileName & file) {
	struct zip_stat sb;
	zip_stat_init(&sb);
	if (zip_stat(handle, file.getPath().c_str(), 0, &sb) == -1) {
		return false;
	}
	std::string entry(sb.name);
	return (entry.back() != '/' && sb.size != 0);
}
Exemplo n.º 8
0
size_t ZIPProvider::ZIPHandle::fileSize(const FileName & file) {
	struct zip_stat sb;
	zip_stat_init(&sb);
	if (zip_stat(handle, file.getPath().c_str(), 0, &sb) == -1) {
		WARN(zip_strerror(handle));
		return 0;
	}
	return static_cast<size_t>(sb.size);
}
Exemplo n.º 9
0
/** @private */
SR_PRIV int sr_sessionfile_check(const char *filename)
{
	struct zip *archive;
	struct zip_file *zf;
	struct zip_stat zs;
	uint64_t version;
	int ret;
	char s[11];

	if (!filename)
		return SR_ERR_ARG;

	if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
		sr_err("Not a regular file: %s.", filename);
		return SR_ERR;
	}

	if (!(archive = zip_open(filename, 0, NULL)))
		/* No logging: this can be used just to check if it's
		 * a sigrok session file or not. */
		return SR_ERR;

	/* check "version" */
	if (!(zf = zip_fopen(archive, "version", 0))) {
		sr_dbg("Not a sigrok session file: no version found.");
		zip_discard(archive);
		return SR_ERR;
	}
	ret = zip_fread(zf, s, sizeof(s) - 1);
	if (ret < 0) {
		sr_err("Failed to read version file: %s",
			zip_file_strerror(zf));
		zip_fclose(zf);
		zip_discard(archive);
		return SR_ERR;
	}
	zip_fclose(zf);
	s[ret] = '\0';
	version = g_ascii_strtoull(s, NULL, 10);
	if (version == 0 || version > 2) {
		sr_dbg("Cannot handle sigrok session file version %" PRIu64 ".",
			version);
		zip_discard(archive);
		return SR_ERR;
	}
	sr_spew("Detected sigrok session file version %" PRIu64 ".", version);

	/* read "metadata" */
	if (zip_stat(archive, "metadata", 0, &zs) < 0) {
		sr_dbg("Not a valid sigrok session file.");
		zip_discard(archive);
		return SR_ERR;
	}
	zip_discard(archive);

	return SR_OK;
}
Exemplo n.º 10
0
char *get_contents_zip(const char *path, const char *name, time_t *last_modified) {

    zip_t *archive = zip_open(path, ZIP_RDONLY, NULL);

    if (archive == NULL) {
        char buffer[1024];
        snprintf(buffer, 1024, "Could not open %s\n", path);
        engine_print(buffer);
        return NULL;
    }

    zip_stat_t stat;
    if (zip_stat(archive, name, 0, &stat) < 0) {
        goto close_archive;
    }

    zip_file_t *f = zip_fopen(archive, name, 0);
    if (f == NULL) {
        print_zip_err("zip_fopen", archive);
        goto close_archive;
    }

    if (last_modified != NULL) {
        *last_modified = stat.mtime;
    }

    char *buf = malloc(stat.size + 1);
    if (!buf) {
        engine_println("zip malloc");
        goto close_f;
    }

    if (zip_fread(f, buf, stat.size) < 0) {
        print_zip_err("zip_fread", archive);
        goto free_buf;
    }
    buf[stat.size] = '\0';

    zip_fclose(f);
    zip_close(archive);

    return buf;

    free_buf:
    free(buf);

    close_f:
    zip_fclose(f);

    close_archive:
    zip_close(archive);

    return NULL;
}
Exemplo n.º 11
0
void ZipByteReader::Register(size_t seqId, const std::string& path)
{
    auto zipFile = m_zips.pop_or_create([this]() { return OpenZip(); });
    zip_stat_t stat;
    zip_stat_init(&stat);
    int err = zip_stat(zipFile.get(), path.c_str(), 0, &stat);
    if (ZIP_ER_OK != err)
        RuntimeError("Failed to get file info of %s, zip library error: %s", path.c_str(), GetZipError(err).c_str());
    m_seqIdToIndex[seqId] = std::make_pair(stat.index, stat.size);
    m_zips.push(std::move(zipFile));
}
Exemplo n.º 12
0
bool ZIPProvider::ZIPHandle::isDir(const FileName & directory) {
	if (directory.getDir().empty()) {
		// Always return true for the root archive directory.
		return true;
	}
	struct zip_stat sb;
	zip_stat_init(&sb);
	if (zip_stat(handle, directory.getDir().c_str(), 0, &sb) == -1) {
		return false;
	}
	std::string entry(sb.name);
	return (entry.back() == '/' && sb.size == 0);
}
Exemplo n.º 13
0
bool KaraokePlayable_ZIP::extract(const QString &object, QIODevice *out)
{
    // Retrieve the file size
    struct zip_stat fileinfo;

    // http://www.nih.at/libzip/zip_stat.html
    if ( zip_stat( m_zip, object.toUtf8().constData(), 0, &fileinfo) != 0 )
    {
        m_errorMsg = zip_strerror( m_zip );
        return false;
    }

    // Make sure the size field is valid
    if ( (fileinfo.valid & ZIP_STAT_SIZE) == 0 || (fileinfo.valid & ZIP_STAT_INDEX) == 0 )
    {
        m_errorMsg = "not a valid file";
        return false;
    }

    // Open the file
    struct zip_file * zgh = zip_fopen_index( m_zip, fileinfo.index, 0 );

    if ( !zgh )
    {
        m_errorMsg = "cannot open zip file";
        return false;
    }

    // Extract the content
    char buf[32768];
    unsigned int offset = 0;

    while ( offset < fileinfo.size )
    {
        int toread = qMin( (qint64) sizeof(buf), (qint64) fileinfo.size - offset );
        int ret = zip_fread( zgh, buf, toread );

        if ( ret != toread )
        {
            m_errorMsg = "extraction failed";
            zip_fclose( zgh );
            return false;
        }

        out->write( buf, ret );
        offset += ret;
    }

    zip_fclose( zgh );
    return true;
}
Exemplo n.º 14
0
/** @private */
SR_PRIV int sr_sessionfile_check(const char *filename)
{
	struct stat st;
	struct zip *archive;
	struct zip_file *zf;
	struct zip_stat zs;
	int version, ret;
	char s[11];

	if (!filename)
		return SR_ERR_ARG;

	if (stat(filename, &st) == -1) {
		sr_err("Couldn't stat %s: %s", filename, g_strerror(errno));
		return SR_ERR;
	}

	if (!(archive = zip_open(filename, 0, &ret)))
		/* No logging: this can be used just to check if it's
		 * a sigrok session file or not. */
		return SR_ERR;

	/* check "version" */
	if (!(zf = zip_fopen(archive, "version", 0))) {
		sr_dbg("Not a sigrok session file: no version found.");
		return SR_ERR;
	}
	if ((ret = zip_fread(zf, s, 10)) == -1)
		return SR_ERR;
	zip_fclose(zf);
	s[ret] = 0;
	version = strtoull(s, NULL, 10);
	if (version > 2) {
		sr_dbg("Cannot handle sigrok session file version %d.", version);
		return SR_ERR;
	}
	sr_spew("Detected sigrok session file version %d.", version);

	/* read "metadata" */
	if (zip_stat(archive, "metadata", 0, &zs) == -1) {
		sr_dbg("Not a valid sigrok session file.");
		return SR_ERR;
	}

	if ((ret = zip_close(archive)) == -1) {
		sr_dbg("error closing zipfile: %s", zip_strerror(archive));
		return SR_ERR;
	}

	return SR_OK;
}
Exemplo n.º 15
0
static int S_archive_stat(lua_State* L) {
    struct zip**    ar        = check_archive(L, 1);
    const char*     path      = (lua_isnumber(L, 2)) ? NULL : luaL_checkstring(L, 2);
    int             path_idx  = (lua_isnumber(L, 2)) ? luaL_checkint(L, 2)-1 : -1;
    int             flags     = (lua_gettop(L) < 3)  ? 0    : luaL_checkint(L, 3);
    struct zip_stat stat;
    int             result;

    if ( ! *ar ) return 0;

    if ( NULL == path ) {
        result = zip_stat_index(*ar, path_idx, flags, &stat);
    } else {
        result = zip_stat(*ar, path, flags, &stat);
    }

    if ( result != 0 ) {
        lua_pushnil(L);
        lua_pushstring(L, zip_strerror(*ar));
        return 2;
    }

    lua_createtable(L, 0, 8);

    lua_pushstring(L, stat.name);
    lua_setfield(L, -2, "name");

    lua_pushinteger(L, stat.index+1);
    lua_setfield(L, -2, "index");

    lua_pushnumber(L, stat.crc);
    lua_setfield(L, -2, "crc");

    lua_pushnumber(L, stat.size);
    lua_setfield(L, -2, "size");

    lua_pushnumber(L, stat.mtime);
    lua_setfield(L, -2, "mtime");

    lua_pushnumber(L, stat.comp_size);
    lua_setfield(L, -2, "comp_size");

    lua_pushnumber(L, stat.comp_method);
    lua_setfield(L, -2, "comp_method");

    lua_pushnumber(L, stat.encryption_method);
    lua_setfield(L, -2, "encryption_method");

    return 1;
}
Exemplo n.º 16
0
struct VFile* _vdzOpenFile(struct VDir* vd, const char* path, int mode) {
	UNUSED(mode);
	// TODO: support truncating, appending and creating, and write
	struct VDirZip* vdz = (struct VDirZip*) vd;

	if ((mode & O_RDWR) == O_RDWR) {
		// libzip doesn't allow for random access, so read/write is impossible without
		// reading the entire file first. This approach will be supported eventually.
		return 0;
	}

	if (mode & O_WRONLY) {
		// Write support is not yet implemented.
		return 0;
	}

	struct zip_stat s;
	if (zip_stat(vdz->z, path, 0, &s) < 0) {
		return 0;
	}

	struct zip_file* zf = zip_fopen(vdz->z, path, 0);
	if (!zf) {
		return 0;
	}

	struct VFileZip* vfz = malloc(sizeof(struct VFileZip));
	vfz->zf = zf;
	vfz->buffer = 0;
	vfz->offset = 0;
	vfz->bufferSize = 0;
	vfz->readSize = 0;
	vfz->fileSize = s.size;

	vfz->d.close = _vfzClose;
	vfz->d.seek = _vfzSeek;
	vfz->d.read = _vfzRead;
	vfz->d.readline = VFileReadline;
	vfz->d.write = _vfzWrite;
	vfz->d.map = _vfzMap;
	vfz->d.unmap = _vfzUnmap;
	vfz->d.truncate = _vfzTruncate;
	vfz->d.size = _vfzSize;
	vfz->d.sync = _vfzSync;

	return &vfz->d;
}
Exemplo n.º 17
0
uint8_t *ReadFromZip(zip *archive, const char* filename, size_t *size) {
	// Figure out the file size first.
	struct zip_stat zstat;
	zip_file *file = zip_fopen(archive, filename, ZIP_FL_NOCASE|ZIP_FL_UNCHANGED);
	if (!file) {
		ELOG("Error opening %s from ZIP", filename);
		return 0;
	}
	zip_stat(archive, filename, ZIP_FL_NOCASE|ZIP_FL_UNCHANGED, &zstat);

	uint8_t *contents = new uint8_t[zstat.size + 1];
	zip_fread(file, contents, zstat.size);
	zip_fclose(file);
	contents[zstat.size] = 0;

	*size = zstat.size;
	return contents;
}
Exemplo n.º 18
0
char *get_contents_zip(char *path, char *name, time_t *last_modified) {
	zip_t *archive = zip_open(path, ZIP_RDONLY, NULL);
	if (archive == NULL) {
		print_zip_err("zip_open", archive);
		return NULL;
	}

	zip_stat_t stat;
	if (zip_stat(archive, name, 0, &stat) < 0) {
		print_zip_err("zip_stat", archive);
		goto close_archive;
	}

	zip_file_t *f = zip_fopen(archive, name, 0);
	if (f == NULL) {
		print_zip_err("zip_fopen", archive);
		goto close_archive;
	}

	if (last_modified != NULL) {
		*last_modified = stat.mtime;
	}

	char *buf = malloc(stat.size + 1);
	if (zip_fread(f, buf, stat.size) < 0) {
		print_zip_err("zip_fread", archive);
		goto free_buf;
	}
	buf[stat.size] = '\0';

	zip_fclose(f);
	zip_close(archive);

	return buf;

free_buf:
	free(buf);
	zip_fclose(f);
close_archive:
	zip_close(archive);

	return NULL;
}
Exemplo n.º 19
0
size_t fs_file_size(const char *filename)
{
    if (is_cwd_zip()) {
        struct directory *d;
        struct zip_stat sb;
        char buf[NAME_MAX];

        d = sf_list_tail(&fs.directories);

        get_zip_cwd(buf, NAME_MAX);
        strcat(buf, filename);

        zip_stat(d->zip, buf, 0, &sb);
        return sb.size;
    } else {
        struct stat sb;
        stat(filename, &sb);
        return sb.st_size;
    }
}
Exemplo n.º 20
0
wxImage ZipEntry::LoadImage() {
  mutex->Lock();
  if (IsDirectory())
    return wxImage();

  struct zip_stat stat;
  zip_stat(zipFile, innerPath, 0, &stat);

  auto file = zip_fopen(zipFile, innerPath, 0);
  auto size = stat.size;
  auto buffer = new unsigned char[size];
  auto read = zip_fread(file, buffer, size);

  wxMemoryInputStream stream(buffer, size);
  wxImage output(stream);

  delete[] buffer;
  mutex->Unlock();
  return output;
}
Exemplo n.º 21
0
bool ZipAssetReader::GetFileInfo(const char *path, FileInfo *info) {
	struct zip_stat zstat;
	char temp_path[1024];
	strcpy(temp_path, in_zip_path_);
	strcat(temp_path, path);
	if (0 != zip_stat(zip_file_, temp_path, ZIP_FL_NOCASE, &zstat)) {
		// ZIP files do not have real directories, so we'll end up here if we
		// try to stat one. For now that's fine.
		info->exists = false;
		info->size = 0;
		return false;
	}

	info->fullName = path;
	info->exists = true; // TODO
	info->isWritable = false;
	info->isDirectory = false;    // TODO
	info->size = zstat.size;
	return true;
}
Exemplo n.º 22
0
//-------------------------------------------------------------------------------------------------------
void Unpack::Decompress(const std::string& from, const std::string& to)
{
	struct zip_stat zs;
	zip_stat(m_pAPKArchive, from.c_str(), ZIP_FL_UNCHANGED , &zs);//读取zip属性
	DEBUGLOG("%s size is %d ", from.c_str(), zs.size);

	byte* data = NEW byte[zs.size];

	zip_file* zipfile;
	zipfile = zip_fopen(m_pAPKArchive, from.c_str(), 0); //打开文件流
	zip_fread(zipfile, data, zs.size); //读取文件

	DEBUGLOG("save to %s", to.c_str());

	FilePtr writeto;
	File::Instance().OpenFile(to.c_str(), File::FILE_WRITE, writeto);
	File::Instance().WriteFile(data, zs.size, 1, writeto);
	File::Instance().CloseFile(writeto);

	zip_fclose(zipfile);//关闭文件
}
Exemplo n.º 23
0
/*************************************************
 Function:		readDexFile
 Descroption:
 Input:
	1.zip* z
	2.filename
	3.char* buf
 Output:
 Return:
 Other:
*************************************************/
long readDexFile(struct zip* z, char* filename, unsigned char** buf)
{
    int i;
    long ReadNum = 0;
    struct zip_stat fstat;

    if(z != NULL && NULL != buf)
    {
        zip_stat_init(&fstat);
        int c = zip_get_num_files(z);
        if(c > 0)
        {
            for (i = 0 ; i < c; i++)
            {
                const char* name = zip_get_name(z, i, 0);
                if(0 == strcmp(name,filename))
                {
                    zip_stat(z, name,0,&fstat);
                    //LOGI("File %i:%s Size1: %lld Size2: %lld\n", i,fstat.name,fstat.size ,fstat.comp_size);

                    struct zip_file* file = zip_fopen(z, filename, 0);
                    if (file)
                    {
                        *buf =(unsigned char *)malloc(fstat.size+1);
                        memset(*buf,0,(fstat.size+1));
                        ReadNum = zip_fread(file, *buf,fstat.size);
                        zip_fclose(file);
                    }
                    break;
                }
            }
        }
    }
    else
    {
        return 0;
    }

    return ReadNum;
}
static VALUE zipruby_stat_initialize(int argc, VALUE *argv, VALUE self) {
  VALUE archive, index, flags;
  struct zipruby_archive *p_archive;
  struct zipruby_stat *p_stat;
  char *fname = NULL;
  int i_index = -1, i_flags = 0;

  rb_scan_args(argc, argv, "21", &archive, &index, &flags);

  if (!rb_obj_is_instance_of(archive, Archive)) {
    rb_raise(rb_eTypeError, "wrong argument type %s (expected ZipRuby::Archive)", rb_class2name(CLASS_OF(archive)));
  }

  switch (TYPE(index)) {
  case T_STRING: fname = RSTRING_PTR(index); break;
  case T_FIXNUM: i_index = NUM2INT(index); break;
  default:
    rb_raise(rb_eTypeError, "wrong argument type %s (expected String or Fixnum)", rb_class2name(CLASS_OF(index)));
  }

  if (!NIL_P(flags)) {
    i_flags = NUM2INT(flags);
  }

  Data_Get_Struct(archive, struct zipruby_archive, p_archive);
  Check_Archive(p_archive);
  Data_Get_Struct(self, struct zipruby_stat, p_stat);

  if (fname) {
    if (zip_stat(p_archive->archive, fname, i_flags, p_stat->sb) != 0) {
      rb_raise(Error, "Obtain file status failed - %s: %s", fname, zip_strerror(p_archive->archive));
    }
  } else {
    if (zip_stat_index(p_archive->archive, i_index, i_flags, p_stat->sb) != 0) {
      rb_raise(Error, "Obtain file status failed at %d: %s", i_index, zip_strerror(p_archive->archive));
    }
  }

  return Qnil;
}
Exemplo n.º 25
0
bool ZipFileManager::GetFileBuffer(const str_type::string &fileName, FileBuffer &out)
{
	if (!IsLoaded())
		return false;

	str_type::string fixedPath = fileName;
	FixSlashesForUnix(fixedPath);

	zip_file *file = zip_fopen(m_archive, fixedPath.c_str(), 0);

	if (file == NULL)
		return false;

	struct zip_stat stat;
	zip_stat(m_archive, fixedPath.c_str(), 0, &stat);

	out = FileBuffer(new _FileBuffer<unsigned char>(static_cast<unsigned long>(stat.size)));
	zip_fread(file, out->GetAddress(), stat.size);

	zip_fclose(file);
	return true;
}
Exemplo n.º 26
0
static int dev_acquisition_start(const struct sr_dev_inst *sdi,
		void *cb_data)
{
	struct zip_stat zs;
	struct session_vdev *vdev;
	int ret;

	vdev = sdi->priv;

	sr_info("Opening archive %s file %s", vdev->sessionfile,
		vdev->capturefile);

	if (!(vdev->archive = zip_open(vdev->sessionfile, 0, &ret))) {
		sr_err("Failed to open session file '%s': "
		       "zip error %d\n", vdev->sessionfile, ret);
		return SR_ERR;
	}

	if (zip_stat(vdev->archive, vdev->capturefile, 0, &zs) == -1) {
		sr_err("Failed to check capture file '%s' in "
		       "session file '%s'.", vdev->capturefile, vdev->sessionfile);
		return SR_ERR;
	}

	if (!(vdev->capfile = zip_fopen(vdev->archive, vdev->capturefile, 0))) {
		sr_err("Failed to open capture file '%s' in "
		       "session file '%s'.", vdev->capturefile, vdev->sessionfile);
		return SR_ERR;
	}

	/* Send header packet to the session bus. */
    std_session_send_df_header(sdi, LOG_PREFIX);

	/* freewheeling source */
    sr_session_source_add(-1, 0, 0, receive_data, sdi);

	return SR_OK;
}
Exemplo n.º 27
0
 void ZipFileOutput::dumpFile()
 {
     if(m_currentFile)
     {
         m_bufferedFiles.push_back(m_currentFile->str());
         std::string & fileContent = m_bufferedFiles.back();
         
         zip_source* source = zip_source_buffer(m_archiveHandle, fileContent.c_str(), fileContent.size(), 0);
         if(! source)
             throw FileAccessFailed(m_currentFilename, m_archive, "Failed to allocate ZLib source.");
         
         // check if the file exists
         struct zip_stat stat;
         if(zip_stat(m_archiveHandle, m_currentFilename.c_str(), 0, &stat) < 0)
         { 
             // the file does not exist
             // add it to the archive
             if(zip_add(m_archiveHandle, m_currentFilename.c_str(), source) < 0)
             {
                 zip_source_free(source);
                 throw FileAccessFailed(m_currentFilename, m_archive, "Failed to add file to zip archive.");
             }
         }
         else
         {
             // the file exists
             // replace it in the archive
             if(zip_replace(m_archiveHandle, stat.index, source) < 0)
             {
                 zip_source_free(source);
                 throw FileAccessFailed(m_currentFilename, m_archive, "Failed to replace file in zip archive.");
             }
         }
         
         delete m_currentFile;
         m_currentFile = 0;
     }
 }
Exemplo n.º 28
0
 std::istream& ZipFileInput::openFile(const stromx::runtime::InputProvider::OpenMode mode)
 {            
     if(! m_initialized)
         throw WrongState("Zip file input has not been initialized.");
     
     if(m_currentFile)
         throw WrongState("File has already been opened.");
     
     if(! hasFile())
         throw NoInputFile();
     
     std::ios_base::openmode iosmode = std::ios_base::in;
     if(mode == BINARY)
         iosmode |= std::ios_base::binary;
     
     struct zip_stat stat;
     if(zip_stat(m_archiveHandle, m_currentFilename.c_str(), 0, &stat) < 0)
         throw FileAccessFailed(m_currentFilename, m_archive, "Failed to access file in zip archive.");
     
     if(stat.size == 0)
         throw FileAccessFailed(m_currentFilename, m_archive, "File in zip archive has zero size.");
     
     unsigned int fileSize = (unsigned int)(stat.size);
     std::vector<char> content(fileSize);
         
     zip_file* file = zip_fopen(m_archiveHandle, m_currentFilename.c_str(), 0);
     if(! file)
         throw FileAccessFailed(m_currentFilename, m_archive, "Failed to open file in zip archive.");
     
     if((unsigned int)(zip_fread(file, &content[0], fileSize)) != fileSize)
         throw FileAccessFailed(m_currentFilename, m_archive, "Failed to read file in zip archive.");
     
     m_currentFile = new std::istringstream(std::string(&content[0], fileSize), iosmode);
     
     return *m_currentFile;
 }
Exemplo n.º 29
0
int main(int argc, char* argv[])
{
	// Single byte to be read from the data files
	char c;

	// Counters to keep track of cuts
	unsigned long waveformCtr = 0;
	unsigned long linearGateCtr = 0;
	unsigned long muonVetoCtr = 0;
	unsigned long overflowCtr = 0;
	unsigned long bgAnalysisCtr = 0;
	unsigned long sAnalysisCtr = 0;

	// Variables for the background output files
	int bg_counter = 0;
	int bg_file_number = 0;
	std::ofstream bg_out_file;

	// Variables for the signal output files
	int s_counter = 0;
	int s_file_number = 0;
	std::ofstream s_out_file;

	// Run Info output
	std::ofstream infoOut;

	// Saved waveforms
	std::ofstream waveformOut;

	// LabView headers
	int no_samples = 0;
	int no_channels = 0;

	// Timestamp of current trigger
	std::string timestamp;

	// Waveform buffers
	int csi [35000] = {} ;
	int csi_raw[35000] = {} ;
	int mv [35000]  = {} ;

	// Buffer to store bit shifted samples
	int _tmpC = 0;
	
	// Medians of CsI and muon veto
	int med_csi = 0;
	int med_mv = 0;

	// Buffers used for median calculation as well as overflow detection
	unsigned int med_csi_sum = 0;
	unsigned int med_mv_sum = 0;
	unsigned int med_csi_arr [256] = {} ;
	unsigned int med_mv_arr [256] = {};   
	bool med_csi_found = false;
	bool med_mv_found = false;
	bool overflow = false;

	// Buffers for SPE integration
	int spe_charge_dist[300] = {};
	int spe_integration_ctr = 0;
	int spe_integration_charge = 0;

	// Buffers for peaks in pretrace
	int bg_pe_pt[52] = {};
	int s_pe_pt[52] = {};

	// Buffer for baseline
	int baseline_hist[32] = {};

	// Rising and falling threshold crossings used for linear gate detection
	int _previous_c = 0;
	int gate_down = 0;
	int gate_up = 0;

	// Waveform contains a gate or muon veto fired more than three times
	bool linear_gate = false;
	bool muon_veto_flag = false;

	// Photoelectron counter for all interesting regions
	// pt = pretrace, roi = region of interest, iw = integration window
	unsigned int s_pt_ct = 0;
	unsigned int bg_pt_ct = 0;
	unsigned int s_roi_ct = 0;
	unsigned int bg_roi_ct = 0;
	unsigned int s_iw_ct = 0;
	unsigned int bg_iw_ct = 0;
	unsigned int PE_max_PT = 10;

	// Buffers to store current peak width in CsI and muon veto waveform
	int above_pe_threshold = 0;
	int current_peak_width = 0;
	int current_pe_width = 0;
	int m_peak_width = 0;

	// Peak thresholds
	int peak_height_threshold = 3;
	int peak_width_threshold = 4;

	// Peak locations in CsI, Muon locations in muon veto
	std::vector<int> peaks;
	std::vector<int> peak_heights;
	int peak_height_dist[100] = {};
	int peak_amplitude = 0;
	std::vector<int> pe_beginnings;
	std::vector<int> pe_endings;
	std::vector<int> muon_peaks; 
	std::vector<int> muon_onset_arr;

	// Keep track of all peak/pe locations in the full minute
	int peak_distribution[350][350] = {};
	int charge_distribution[350][350] = {};
	for (int i1 = 0; i1 < 350; i1++)
	{
		for (int i2 = 0; i2 < 350; i2++)
		{
			peak_distribution[i1][i2] = 0;
			charge_distribution[i1][i2] = 0;
		}
	}
	// Get peak width distribution
	int peak_width_distribution[51] = {};

	// Charge of PE in PT
	int current_spe_q = 0;

	// Integration window buffers
	int s_q_arr [1500] = {};
	int bg_q_arr [1500] = {};
	int running_charge = 0;

	// LogLikelihood prefactors & estimators
	double lnL_pf_real[1500] = {};
	double lnL_pf_flat[1500] = {};
	double lnL_real = 0;
	double lnL_flat = 0;

	// Big pulse detection
	bool bP_detected = false;
	int _t_bP_idx = 0;
	std::vector<int> bP_onset_arr;
	std::vector<int> bP_charge_arr;

	// Calculate prefactors for loglikelihood analysis
	// timing and fraction from NIMA paper
	double r = 0.41;
	double tf = 527.0;
	double ts = 5600.0;

	// total integration window is 3 us long -> tMax = 1500
	double _tFast = 1.0 / ((1.0 + r) * tf * (1 - exp(-1500.0 / tf)));
	double _tSlow = r / ((1.0 + r) * ts * (1 - exp(-1500.0 / ts)));
		
	// Calculate prefactor for each time step
	for (int i = 0; i < 1500; i++)
	{
		lnL_pf_real[i] = log(_tFast * exp(-(double)i / tf) + _tSlow * exp(-(double)i / ts));
		lnL_pf_flat[i] = log(1.0 / 1500.0);
	}

	// Threshold buffer and measured rise times
	double thresholds [3] = {};
	double bg_rt [3] = {};
	double s_rt [3] = {};

	// Buffers used during window analysis
	int idx_0 = 0;
	int idx_w_offset = 0;
	int i_peak = 0;
	int i_pe = 0;
	int q_int = 0;
	double _t1 = 0.0;
	double _t2 = 0.0;

	std::string main_dir;
	int current_time;
	int single_time;
	std::string out_dir;
	std::string current_zip_file;
	std::string time_name_in_zip;
	
	// Save waveforms as ascii - Counter,cuts etc
	int save_wf_ctr = 0;
	int no_total_peaks[2] = { 100, 200 };
	int minimum_no_peaks_iw = 6;
	int rt1090_bottom_left[2] = { 750, 1150 };
	int rt050_bottom_left[2] = { 235, 345 };
	int rt1090_upper_right[2] = { 1080, 1280 };
	int rt050_upper_right[2] = { 520, 760 };
	bool save_waveforms = false;
	bool passed_cuts_bg = false;
	bool passed_cuts_s = false;
	bool passed_cut = false;

	// Set main run directory, e.g. Run-15-10-02-27-32-23/151002
	// Set current time to be analzyed as index of sorted number of total files in folder, e.g. 0-1439 for a full day
	// Set output directory, eg Output/ Run-15-10-02-27-32-23/151002
	int data_set = 0;
	if (argc == 6) 
	{
		data_set = atoi(argv[1]);
		main_dir = std::string(argv[2]); 
		current_time = atoi(argv[3]);
		out_dir = std::string(argv[4]);
		single_time = atoi(argv[5]);
	}
	else
	{
		std::cout << "Arguments not matching! Aborting now!" << std::endl;
		return 1;
	}

	unsigned int BG_PT[2] = {};
	unsigned int BG_ROI[2] = {};
	unsigned int S_PT[2] = {};
	unsigned int S_ROI[2] = {};

	switch (data_set)
	{
	case 1: BG_PT[0]  = 0;
			BG_PT[1]  = 19975;
			BG_ROI[0] = 19975;
			BG_ROI[1] = 27475;
			S_PT[0]   = 7500;
			S_PT[1]   = 27475;
			S_ROI[0]  = 27475;
			S_ROI[1]  = 34975;
			PE_max_PT = 10;
			break;
	case 2: BG_PT[0]  = 0;
			BG_PT[1]  = 20000;
			BG_ROI[0] = 20000;
			BG_ROI[1] = 27400;
			S_PT[0]   = 7400;
			S_PT[1]   = 27400;
			S_ROI[0]  = 27400;
			S_ROI[1]  = 35000;
			PE_max_PT = 20;
			break;
	case 3: BG_PT[0]  = 0;
			BG_PT[1]  = 17500;
			BG_ROI[0] = 17500;
			BG_ROI[1] = 25000;
			S_PT[0]   = 7500;
			S_PT[1]   = 25000;
			S_ROI[0]  = 25000;
			S_ROI[1]  = 32500;
			PE_max_PT = 30;
			break;
	default: std::cout << "Arguments not matching! Aborting now!" << std::endl;
			return 1;
	}

	// Full analysis -> Converts $(Process) from condor submit to the current time file
	if (single_time == 0)
	{
		// Buffers used to process data files and to step through directories
		std::vector<std::string> time_files;
		DIR *dpdf;
		struct dirent *epdf;

		// Find all time files and sort them
		time_files.clear();
		dpdf = opendir(main_dir.c_str());
		if (dpdf != NULL)
		{
			std::string _tmp;
			while (epdf = readdir(dpdf))
			{
				_tmp = epdf->d_name;
				if (_tmp != "." && _tmp != ".." && _tmp.substr(7) == "zip")
				{
					time_files.push_back(_tmp.substr(0, 6));
				}
			}
		}

		// Sort time files by time in ascending order to convert current_time index to proper file
		std::sort(time_files.begin(), time_files.end(), timeSort);

		// Prepare paths to zipped and unzipped files
		current_zip_file = main_dir + "/" + time_files[current_time] + ".zip";

		time_name_in_zip = time_files[current_time];
	}

	// Single file analysis -> Directly convert input to current time file
	else
	{
		current_zip_file = main_dir + "/" + zeroPad(current_time,6) +".zip";
		time_name_in_zip = zeroPad(current_time,6);
	}

	//Open the ZIP archive
	int err = 0;
	int zidx = 0;
	int fileSize = 0;
	zip *z = zip_open(current_zip_file.c_str(), 0, &err);
	//Search for the file of given name
	const char *name = time_name_in_zip.c_str();
	struct zip_stat st;
	zip_stat_init(&st);
	zip_stat(z, name, 0, &st);

	//Alloc memory for its uncompressed contents
	char *contents = new char[st.size];

	// Unzip the compressed file into memory
	zip_file *f = zip_fopen(z, time_name_in_zip.c_str(), 0);
	fileSize = st.size;
	zip_fread(f, contents, fileSize);
	zip_fclose(f);
	//And close the archive
	zip_close(z);

	// Create signal, background and info output files
	bg_out_file.open((out_dir + "/" + fileName(atoi(time_name_in_zip.c_str()), "B-")).c_str(), std::ofstream::out | std::ofstream::trunc);
	s_out_file.open((out_dir + "/" + fileName(atoi(time_name_in_zip.c_str()), "S-")).c_str(), std::ofstream::out | std::ofstream::trunc);
	infoOut.open((out_dir + "/" + fileName(atoi(time_name_in_zip.c_str()), "I-")).c_str(), std::ofstream::out | std::ofstream::trunc);
	if (save_waveforms)
	{
		waveformOut.open((out_dir + "/" + fileName(atoi(time_name_in_zip.c_str()), "W-")).c_str(), std::ofstream::out | std::ofstream::trunc);
	}

	// Begin data processing if file has been properly opened
	if(err == 0)
	{
		waveformCtr = 0;
		zidx = 0;
		// Begin reading byte-stream
		while (zidx < fileSize)
		{   
			// Read LabView header and get the total number of samples written for each channel in the next chunk of data
			for (int i=0;i<4;i++)
			{
				c = contents[zidx++];
				no_samples = no_samples << 8 | (unsigned char) c;
			}
			
			// Read LabView header and get the total number of channels written in next chunk of data (always 2 in our case)
			for (int i=0;i<4;i++)
			{
				c = contents[zidx++];
				no_channels = no_channels << 8 | (unsigned char) c;
			}

			// Takes care of LabViews closing bit...
			if (no_samples > 350070)
			{
				break;
			}

			// ----------------------------------------------------------------
			//  Process XYZ consecutive waveforms without encountering another
			// LabView header inbetween
			// ----------------------------------------------------------------
			for (int wf=0; wf < (int) (no_samples/35007); wf++)
			{
				// A new waveform begins
			 	waveformCtr += 1;

				// -------------------------------------------------------------
				//    Reset all major waveform specific variables
				// -------------------------------------------------------------
				timestamp.clear();
				overflow = false;
				linear_gate = false;
				muon_veto_flag = false;
				_previous_c = 128;
				gate_down = 0;
				gate_up = 0;
				memset(med_csi_arr,0,sizeof med_csi_arr);
				memset(med_mv_arr,0,sizeof med_mv_arr );      
				med_csi_found = false;
				med_mv_found = false;
				med_csi_sum = 0;
				med_mv_sum = 0;     
				above_pe_threshold = 0;
				current_peak_width = 0;
				current_pe_width = 0;
				current_spe_q = 0;
				m_peak_width = 0;
				peaks.clear();
				peak_heights.clear();
				pe_beginnings.clear();
				pe_endings.clear();
				muon_peaks.clear();
				spe_integration_ctr = 0;
				spe_integration_charge = 0;
				passed_cuts_s = false;
				passed_cuts_bg = false;
				passed_cut = false;
				peak_amplitude = 0;
				bP_detected = false;
				_t_bP_idx = 0;

				// -------------------------------------------------------------
				//  Read current timestamp
				// -------------------------------------------------------------
				for(int i=0; i<7; i++)
				{
					c = contents[zidx++];
					c = contents[zidx++];
					timestamp += zeroPad((int) c, 2);
				}

				// ---------------------------------------------------------------
				// Read the full CsI and muon veto waveforms from the zip-stream
				// + Apply bit transformation
				// + Determine if a linear gate is present
				// ---------------------------------------------------------------
				for(int i=0; i<35000; i++)
				{
					//--------------
					//      CsI
					//--------------
					c = contents[zidx++];
					// bit transformation to get rid of empty bins
					_tmpC = (int) c - (int) floor(((double) c + 5.0)/11.0);
					if (i<20000){ med_csi_arr[_tmpC + 128] += 1; }
					csi[i] = _tmpC;
					csi_raw[i] = _tmpC;
					if (i == 0) { _previous_c = _tmpC; }

					// Gate check
					if (_tmpC <= 18 && _previous_c > 18) { gate_down++; }
					if (_previous_c <= 18 && _tmpC > 18) { gate_up++; }
					_previous_c = _tmpC;

					// Overflow check
					if (!overflow && (c >= 127 || c == -128))
					{
						overflow = true;
						overflowCtr += 1;
					}

					//--------------
					//  Muon Veto
					//--------------
					c = contents[zidx++];
					_tmpC = (int) c + (int) ((signbit((int) c) ? -1 : 1 ) * floor((4.0 - abs((double) c))/11.0));
					med_mv_arr[_tmpC + 128] += 1;
					mv[i] = _tmpC;
				}

				// ---------------------------------------
				//  Calculate the median of both channels
				// ---------------------------------------
				for(int i=0; i<256; i++)
				{
					if (!med_csi_found)
					{
						med_csi_sum += med_csi_arr[i];
						if (med_csi_sum >= 10000)
						{
							med_csi = i-128;
							med_csi_found=true;
						}
					}

					if (!med_mv_found)
					{
						med_mv_sum += med_mv_arr[i];
						if (med_mv_sum >= 10000)
						{
							med_mv = i-128;
							med_mv_found=true;
						}
					}
				} 

				// ----------------------------------------------
				//  Adjust linear gate counter if wf is gated
				// ----------------------------------------------
				if (gate_down != gate_up || med_csi < 50)
				{
					linear_gate = true;
					linearGateCtr += 1;
				}

				// -----------------------------------------------
				//     Find peaks and photoelectrons in waveforms
				// -----------------------------------------------
				current_peak_width = 0;
				peak_amplitude = 0;
				bP_detected = false;
				for (int i = 0; i < 35000; i++)
				{
					// -------------------------------------------
					//          Analyze CsI[Na] waveform
					// -------------------------------------------
					csi[i] = med_csi - csi[i];

					// Simple peak finder using threshold crossing with history
					if (csi[i] >= peak_height_threshold)
					{
						current_peak_width++;
						peak_amplitude = csi[i] > peak_amplitude ? csi[i] : peak_amplitude;
					}
					else
					{
						if (current_peak_width >= peak_width_threshold)
						{
							peaks.push_back(i - current_peak_width);
							pe_beginnings.push_back((i - current_peak_width - 2) >= 0 ? (i - current_peak_width - 2) : 0);
							pe_endings.push_back((i + 1) <= 34999 ? (i + 1) : 34999);
							peak_width_distribution[(current_peak_width < 50) ? current_peak_width : 50] += 1;
							peak_heights.push_back(peak_amplitude);

							// Check for large energy depositions
							if (!bP_detected && current_peak_width >= 35 && !linear_gate && !overflow)
							{
								bP_detected = true;
								bP_onset_arr.push_back(i - current_peak_width);
							}
						}
						current_peak_width = 0;
						peak_amplitude = 0;
					}

					
					// -------------------------------------------
					//        Analyze muon veto waveform
					// -------------------------------------------
					mv[i] = med_mv - mv[i];

					// Peak finder
					if (mv[i] >= 10) { m_peak_width++; }
					else
					{
						if (m_peak_width >= 3)
						{
							muon_peaks.push_back(i-m_peak_width);
						}
						m_peak_width = 0;
					}
				}

				// If there was a big pulse in the trace integrate it
				// But only if there was no linear gate or overflow
				if (bP_detected && !linear_gate && !overflow)
				{
					int _t_charge = 0;
					for (int i = bP_onset_arr.back(); i < (1500 + bP_onset_arr.back()); i++)
					{
						_t_charge += (csi[i] >= peak_height_threshold) ? csi[i] : 0;
					}
					bP_charge_arr.push_back(_t_charge);
				}

				// Raise muon veto flag if more than three muons have been found
				// If less than 3 have been found fill the vector with -1 for postprocessing
				int muons_found = muon_peaks.size();

				if (muons_found > 0)
				{
					if (linear_gate)
					{
						muon_onset_arr.push_back(muon_peaks[0]);
					}
					else
					{
						for (std::vector<int>::size_type idx = 0; idx < muons_found; idx++)
						{
							muon_onset_arr.push_back(muon_peaks[idx]);
						}
					}
				}
				if (muons_found > 3)
				{
					muon_veto_flag = true;
					muonVetoCtr += 1;
				}
				else { muon_peaks.resize(3, -1); }

				// Add PE peaks to peak height distribution
				if (peak_heights.size() > 0 && !overflow && !linear_gate)
				{
					for (int idx = 0; idx < peak_heights.size(); idx++)
					{
						peak_height_dist[(peak_heights[idx] < 100) ? peak_heights[idx] : 99]++;
					}
				}

				// ========================================================================
				// Check that there is at least one PE in the trace, there is no overflow, 
				// no linear gate and no muon veto flag, otherwise continue to next
				// waveform without bothering to analyze this one
				// ========================================================================
				bool analyze = false;
				if (data_set == 1 && !overflow && !linear_gate && !muon_veto_flag) { analyze = true; }
				if (data_set == 2 && !overflow && !linear_gate) { analyze = true; }
				if (data_set == 3 && !overflow && !linear_gate) { analyze = true; }
				if (analyze && pe_beginnings.size() > 0)
				{
					// -------------------------------------------------------------
					// Integrate all SPE found in the PT and histogram their charge
					// -------------------------------------------------------------
					for (std::vector<int>::size_type idx = 0; idx < pe_beginnings.size(); idx++)
					{
						if (pe_beginnings[idx] < BG_PT[1])
						{
							current_spe_q = 0;
							for (int i = pe_beginnings[idx]; i <= pe_endings[idx]; i++)
							{
								current_spe_q += csi[i];
							}
							if (current_spe_q >= -50 && current_spe_q < 250) { spe_charge_dist[(current_spe_q+50)] += 1; }
						}
						else { break; }
					}

					// -------------------------------------------------------------
					//       Determine number of PE in different regions
					// -------------------------------------------------------------
					bg_pt_ct = 0;
					bg_roi_ct = 0;
					bg_iw_ct = 0;
					s_pt_ct = 0;
					s_roi_ct = 0;
					s_iw_ct = 0;
					if (peaks.size() > 0)
					{
						for (std::vector<int>::size_type idx = 0; idx < peaks.size(); idx++)
						{
							if (peaks[idx] >= BG_PT[0] && peaks[idx] < BG_PT[1]) { bg_pt_ct += 1; }
							if (peaks[idx] >= S_PT[0] && peaks[idx] < S_PT[1]) { s_pt_ct += 1; }
							if (peaks[idx] >= BG_ROI[0] && peaks[idx] < BG_ROI[1]) { bg_roi_ct += 1; }
							if (peaks[idx] >= S_ROI[0] && peaks[idx] < S_ROI[1]) { s_roi_ct += 1; }
						}

						// Distribution of charge (only add >= 3)
						if (true)
						{
							int sz = peaks.size() / 2;
							if (sz < 350)
							{
								int cpi = 0;
								for (int idx = 0; idx < 35000; idx++)
								{
									// Add sample if it is within one of the PE regions identified previously
									if (idx >= pe_beginnings[cpi] && idx <= pe_endings[cpi])
									{
										charge_distribution[sz][idx / 100] += csi[idx];
										if (idx >= pe_endings[cpi]) 
										{ 
											if (++cpi >= pe_beginnings.size()){ break; }
										}
										
									}
								}
							}
						}
					}

					// Histogram baseline and peaks in pretrace
					if (!overflow && !linear_gate && !muon_veto_flag)
					{
						int _t_med_csi_bin = med_csi - 85;
						if (_t_med_csi_bin >= 0 && _t_med_csi_bin <=30) { baseline_hist[_t_med_csi_bin]++; }
						else { baseline_hist[31]++; }

						if (bg_pt_ct <= 50) { bg_pe_pt[bg_pt_ct]++; }
						else { bg_pe_pt[51]++; }

						if (s_pt_ct <= 50) { s_pe_pt[s_pt_ct]++; }
						else { s_pe_pt[51]++; }
					}

					// -------------------------------------------------------------
					//    Only analzye BG region if there are a maximum of PE_max_PT
					//    & at least one PE in ROI
					// -------------------------------------------------------------
					if (bg_pt_ct <= PE_max_PT && bg_roi_ct > 0)
					{
						bgAnalysisCtr += 1;

						// Reset window parameters
						idx_0 = 0;
						i_peak = 0;
						i_pe = 0;
						q_int = 0;
						lnL_real = 0.0;
						lnL_flat = 0.0;
						_t1 = 0.0;
						_t2 = 0.0;

						// -------------------------------------------------------------
						//  Determine onset of integration window by finding the first
						//  PE in the region of interest
						// -------------------------------------------------------------      
						for (int i = 0; i < peaks.size(); i++)
						{
							if (peaks[i] >= BG_ROI[0])
							{
								idx_0 = peaks[i];
								i_peak = i;
								break;
							}
						}
						idx_0 -= 3;

						// -------------------------------------------------------------
						// Only analyze if the full integration window is within the ROI
						// -------------------------------------------------------------
						if (idx_0 < (BG_ROI[1] - 1500))
						{

							// --------------------------------------------------------------
							// Determine number of peaks in integration window (magic bullet)
							// --------------------------------------------------------------
							for (int i = i_peak; i < peaks.size(); i++)
							{
								bg_iw_ct += (peaks[i] - idx_0 < 1500) ? 1 : 0;
							}

							// -------------------------------------------------------------
							//    Integrate over all PE found in the integration window
							// -------------------------------------------------------------
							i_pe = i_peak;
							for (int i = 0; i < 1500; i++)
							{
								// Get proper 'real' index that includes the onset offset
								idx_w_offset = i + idx_0;

								// Add sample if it is within one of the PE regions identified previously as well as update loglikelihood estimators
								if (i_pe < pe_beginnings.size() && idx_w_offset >= pe_beginnings[i_pe] && idx_w_offset <= pe_endings[i_pe])
								{
									q_int += csi[idx_w_offset];
									lnL_real += lnL_pf_real[i] * csi[idx_w_offset];
									lnL_flat += lnL_pf_flat[i] * csi[idx_w_offset];
									if (idx_w_offset == pe_endings[i_pe]) { i_pe++; }
								}

								// Keep track of charge integration to determine rise times later
								bg_q_arr[i] = q_int;
							}

							// -------------------------------------------------------------
							//    Determine rise times
							// -------------------------------------------------------------

							// Calculate charge thresholds
							thresholds[0] = 0.1*bg_q_arr[1499];
							thresholds[1] = 0.5*bg_q_arr[1499];
							thresholds[2] = 0.9*bg_q_arr[1499];

							// Determine threshold crossing times
							for (int i = 0; i < 1499; i++)
							{
								_t1 = (double)bg_q_arr[i];
								_t2 = (double)bg_q_arr[i + 1];

								for (int j = 0; j < 3; j++)
								{
									if (_t1 < thresholds[j] && _t2 >= thresholds[j]){ bg_rt[j] = i + (thresholds[j] - _t1) / (_t2 - _t1); }
								}
							}

							// -------------------------------------------------------------
							//  If waveforms are being saved check whether cuts are passed
							// -------------------------------------------------------------
							if (save_waveforms)
							{
								int rt1090 = (bg_rt[2] - bg_rt[0]);
								int rt050 = bg_rt[1];

								bool bottom_left_cut = (rt1090 >= rt1090_bottom_left[0]) && (rt1090 <= rt1090_bottom_left[1]) && (rt050 >= rt050_bottom_left[0]) && (rt050 <= rt050_bottom_left[1]);
								bool upper_right_cut = (rt1090 >= rt1090_upper_right[0]) && (rt1090 <= rt1090_upper_right[1]) && (rt050 >= rt050_upper_right[0]) && (rt050 <= rt050_upper_right[1]);
								bool min_peak_cut = (bg_iw_ct >= minimum_no_peaks_iw);
								bool total_peak_cut = (peaks.size() >= no_total_peaks[0]) && (peaks.size() <= no_total_peaks[1]);

								passed_cuts_bg = (bottom_left_cut || upper_right_cut) && min_peak_cut && total_peak_cut;
							}
							// -------------------------------------------------------------
							//    Write analysis results to file
							// -------------------------------------------------------------
							bg_out_file << timestamp << " " << med_csi << " " << med_mv << " ";
							bg_out_file << bg_pt_ct << " " << bg_roi_ct << " ";
							bg_out_file << bg_iw_ct << " " << idx_0 << " " << bg_q_arr[1499] << " " << lnL_real << " " << lnL_flat << " ";
							bg_out_file << bg_rt[0] << " " << bg_rt[1] << " " << bg_rt[2] << " ";
							bg_out_file << muon_peaks[0] << " " << muon_peaks[1] << " " << muon_peaks[2] << " " << peaks.size() << std::endl;

							// Keeps track of how many BG waveforms have actually been analyzed
							bg_counter++;
						}
					}

					// -------------------------------------------------------------
					//    Only analzye S region if there are a maximum of PE_max_PT
					//    & at least one PE in ROI
					// -------------------------------------------------------------
					if (s_pt_ct <= PE_max_PT && s_roi_ct > 0)
					{
						sAnalysisCtr += 1;

						// Reset window parameters
						idx_0 = 0;
						i_peak = 0;
						i_pe = 0;
						q_int = 0;
						lnL_real = 0.0;
						lnL_flat = 0.0;
						_t1 = 0.0;
						_t2 = 0.0;

						// -------------------------------------------------------------
						//  Determine onset of integration window by finding the first
						//  PE in the region of interest
						// -------------------------------------------------------------      
						for (int i = 0; i < peaks.size(); i++)
						{
							if (peaks[i] >= S_ROI[0])
							{
								idx_0 = peaks[i];
								i_peak = i;
								break;
							}
						}
						idx_0 -= 3;

						// -------------------------------------------------------------
						// Only analyze if the full integration window is within the ROI
						// -------------------------------------------------------------
						if (idx_0 < (S_ROI[1] - 1500))
						{

							// --------------------------------------------------------------
							// Determine number of peaks in integration window (magic bullet)
							// --------------------------------------------------------------
							for (int i = i_peak; i < peaks.size(); i++)
							{
								s_iw_ct += (peaks[i] - idx_0 < 1500) ? 1 : 0;
							}

							i_pe = i_peak;
							// -------------------------------------------------------------
							//    Integrate over all PE found in the integration window
							// -------------------------------------------------------------
							for (int i = 0; i < 1500; i++)
							{
								// Get proper 'real' index that includes the onset offset
								idx_w_offset = i + idx_0;

								// Add sample if it is within one of the PE regions identified previously & update loglikelihood estimators
								if (i_pe < pe_beginnings.size() && idx_w_offset >= pe_beginnings[i_pe] && idx_w_offset <= pe_endings[i_pe])
								{
									q_int += csi[idx_w_offset];
									lnL_real += lnL_pf_real[i] * csi[idx_w_offset];
									lnL_flat += lnL_pf_flat[i] * csi[idx_w_offset];
									if (idx_w_offset == pe_endings[i_pe]) { i_pe++; }
								}

								// Keep track of charge integration to determine rise times later
								s_q_arr[i] = q_int;
							}

							// -------------------------------------------------------------
							//    Determine rise times
							// -------------------------------------------------------------

							// Calculate charge thresholds
							thresholds[0] = 0.1*s_q_arr[1499];
							thresholds[1] = 0.5*s_q_arr[1499];
							thresholds[2] = 0.9*s_q_arr[1499];

							// Determine threshold crossing times
							for (int i = 0; i < 1499; i++)
							{
								_t1 = (double)s_q_arr[i];
								_t2 = (double)s_q_arr[i + 1];

								for (int j = 0; j < 3; j++)
								{
									if (_t1 < thresholds[j] && _t2 >= thresholds[j]){ s_rt[j] = i + (thresholds[j] - _t1) / (_t2 - _t1); }
								}
							}

							// -------------------------------------------------------------
							//  If waveforms are being saved check whether cuts are passed
							// -------------------------------------------------------------
							if (save_waveforms)
							{
								int rt1090 = (s_rt[2] - s_rt[0]);
								int rt050 = s_rt[1];

								bool bottom_left_cut = (rt1090 >= rt1090_bottom_left[0]) && (rt1090 <= rt1090_bottom_left[1]) && (rt050 >= rt050_bottom_left[0]) && (rt050 <= rt050_bottom_left[1]);
								bool upper_right_cut = (rt1090 >= rt1090_upper_right[0]) && (rt1090 <= rt1090_upper_right[1]) && (rt050 >= rt050_upper_right[0]) && (rt050 <= rt050_upper_right[1]);
								bool min_peak_cut = (s_iw_ct >= minimum_no_peaks_iw);
								bool total_peak_cut = (peaks.size() >= no_total_peaks[0]) && (peaks.size() <= no_total_peaks[1]);

								passed_cuts_s = (bottom_left_cut || upper_right_cut) && min_peak_cut && total_peak_cut;
							}
							// -------------------------------------------------------------
							//    Write analysis results to file
							// -------------------------------------------------------------
							s_out_file << timestamp << " " << med_csi << " " << med_mv << " ";
							s_out_file << s_pt_ct << " " << s_roi_ct << " ";
							s_out_file << s_iw_ct << " " << idx_0 << " " << s_q_arr[1499] << " " << lnL_real << " " << lnL_flat << " ";
							s_out_file << s_rt[0] << " " << s_rt[1] << " " << s_rt[2] << " ";
							s_out_file << muon_peaks[0] << " " << muon_peaks[1] << " " << muon_peaks[2] << " " << peaks.size() << std::endl;

							// Keeps track of how many BG waveforms have actually been analyzed
							s_counter++;
						}
					}
				}
				// -------------------------------------------------------------
				//  Save waveform if cuts have been passed for either ROI
				// -------------------------------------------------------------
				if (save_waveforms && passed_cut)
				{
					for (int idx = 0; idx < 35000; idx++)
					{
						waveformOut << csi_raw[idx] << " ";
					}
					waveformOut << gate_up << " " << gate_down << " " << med_csi << " ";
					for (int idx = 0; idx < pe_beginnings.size(); idx++)
					{
					waveformOut << pe_beginnings[idx] << " " << pe_endings[idx] << " ";
					}
					waveformOut << std::endl;
				}
			}
		}
	}

	// Before exiting, make sure that both output files are properly closed to prevent data loss.
	if (bg_out_file.is_open()) { bg_out_file.close(); }
	if (s_out_file.is_open()) { s_out_file.close(); }


	// Write run info
	if (infoOut.is_open())
	{
		infoOut << "Total number of Waveforms processed" << std::endl;
		infoOut << waveformCtr << std::endl;
		infoOut << "Number of triggers with linear gates in CsI channel" << std::endl;
		infoOut << linearGateCtr << std::endl;
		infoOut << "Number of triggers with overflows in either channel" << std::endl;
		infoOut << overflowCtr << std::endl;
		infoOut << "Number of triggers with more than 3 muons in muon veto channel" << std::endl;
		infoOut << muonVetoCtr << std::endl;
		infoOut << "Number of triggers that have actually been analyzed in the background window (less than x PE/peaks in PT + at least one PE/peak in ROI)" << std::endl;
		infoOut << bgAnalysisCtr << std::endl;
		infoOut << "Number of triggers that have actually been analyzed in the signal window (less than x PE/peaks in PT + at least one PE/peak in ROI)" << std::endl;
		infoOut << sAnalysisCtr << std::endl;
		infoOut << "Maximum number of PE/peaks in the pretrace" << std::endl;
		infoOut << PE_max_PT << std::endl;
		infoOut << "Background pretrace start" << std::endl;
		infoOut << BG_PT[0] << std::endl;
		infoOut << "Background pretrace stop" << std::endl;
		infoOut << BG_PT[1] << std::endl;
		infoOut << "Background ROI start" << std::endl;
		infoOut << BG_ROI[0] << std::endl;
		infoOut << "Background ROI stop" << std::endl;
		infoOut << BG_ROI[1] << std::endl;
		infoOut << "Signal pretrace start" << std::endl;
		infoOut << S_PT[0] << std::endl;
		infoOut << "Signal pretrace stop" << std::endl;
		infoOut << S_PT[1] << std::endl;
		infoOut << "Signal ROI start" << std::endl;
		infoOut << S_ROI[0] << std::endl;
		infoOut << "Signal ROI stop" << std::endl;
		infoOut << S_ROI[1] << std::endl;
		infoOut << "Unzipped file size" << std::endl;
		infoOut << fileSize << std::endl;
		infoOut << "SPE charge histogram" << std::endl;
		for (int idx = 0; idx < 300; idx++)
		{
			infoOut << spe_charge_dist[idx] << " ";
		}
		infoOut << std::endl;
		infoOut << "Background - Peaks in pretrace histogram" << std::endl;
		for (int idx = 0; idx < 52; idx++)
		{
			infoOut << bg_pe_pt[idx] << " ";
		}
		infoOut << std::endl;
		infoOut << "Signal - Peaks in pretrace histogram" << std::endl;
		for (int idx = 0; idx < 52; idx++)
		{
			infoOut << s_pe_pt[idx] << " ";
		}
		infoOut << std::endl;
		infoOut << "CsI baseline histogram" << std::endl;
		for (int idx = 0; idx < 32; idx++)
		{
			infoOut << baseline_hist[idx] << " ";
		}
		infoOut << std::endl;
		infoOut << "Peak width distribution" << std::endl;
		for (int idx = 0; idx < 51; idx++)
		{
			infoOut << peak_width_distribution[idx] << " ";
		}
		infoOut << std::endl;
		infoOut << "PE amplitudes" << std::endl;
		for (int idx = 0; idx < 100; idx++)
		{
			infoOut << peak_height_dist[idx] << " ";
		}
		infoOut << std::endl;
		infoOut << "Big pulse onsets" << std::endl;
		for (int idx = 0; idx < bP_onset_arr.size(); idx++)
		{
			infoOut << bP_onset_arr[idx] << " ";
		}
		infoOut << std::endl;
		infoOut << "Big pulse charges" << std::endl;
		for (int idx = 0; idx < bP_charge_arr.size(); idx++)
		{
			infoOut << bP_charge_arr[idx] << " ";
		}
		infoOut << std::endl;
		infoOut << "All muon onsets - If linear gate present only the first is recorded" << std::endl;
		for (int idx = 0; idx < muon_onset_arr.size(); idx++)
		{
			infoOut << muon_onset_arr[idx] << " ";
		}
		infoOut << std::endl;
		infoOut << "Charge distribution in full waveform" << std::endl;
		for (int idx_1 = 0; idx_1 < 350; idx_1++)
		{
			bool printLine = false;
			for (int idx_2 = 0; idx_2 < 350; idx_2++)
			{
				if (charge_distribution[idx_1][idx_2] > 0)
				{
					printLine = true;
					break;
				}
			}
			if (printLine)
			{
				infoOut << idx_1 << " ";
				for (int idx_2 = 0; idx_2 < 350; idx_2++)
				{
					if (charge_distribution[idx_1][idx_2] > 0) { infoOut << idx_2 << " " << charge_distribution[idx_1][idx_2] << " "; }
				}
				infoOut << std::endl;
			}
		}
		infoOut.close();
	}
	return 0;
}
Exemplo n.º 30
0
static int zip_append(const struct sr_output *o, unsigned char *buf,
		int unitsize, int length)
{
	struct out_context *outc;
	struct zip *archive;
	struct zip_source *logicsrc;
	zip_int64_t num_files;
	struct zip_file *zf;
	struct zip_stat zs;
	struct zip_source *metasrc;
	GKeyFile *kf;
	GError *error;
	gsize len;
	int chunk_num, next_chunk_num, tmpfile, ret, i;
	const char *entry_name;
	char *metafile, tmpname[32], chunkname[16];

	outc = o->priv;
	if (!(archive = zip_open(outc->filename, 0, &ret)))
		return SR_ERR;

	if (zip_stat(archive, "metadata", 0, &zs) == -1)
		return SR_ERR;

	metafile = g_malloc(zs.size);
	zf = zip_fopen_index(archive, zs.index, 0);
	zip_fread(zf, metafile, zs.size);
	zip_fclose(zf);

	/*
	 * If the file was only initialized but doesn't yet have any
	 * data it in, it won't have a unitsize field in metadata yet.
	 */
	error = NULL;
	kf = g_key_file_new();
	if (!g_key_file_load_from_data(kf, metafile, zs.size, 0, &error)) {
		sr_err("Failed to parse metadata: %s.", error->message);
		return SR_ERR;
	}
	g_free(metafile);
	tmpname[0] = '\0';
	if (!g_key_file_has_key(kf, "device 1", "unitsize", &error)) {
		if (error && error->code != G_KEY_FILE_ERROR_KEY_NOT_FOUND) {
			sr_err("Failed to check unitsize key: %s", error ? error->message : "?");
			return SR_ERR;
		}
		/* Add unitsize field. */
		g_key_file_set_integer(kf, "device 1", "unitsize", unitsize);
		metafile = g_key_file_to_data(kf, &len, &error);
		strcpy(tmpname, "sigrok-meta-XXXXXX");
		if ((tmpfile = g_mkstemp(tmpname)) == -1)
			return SR_ERR;
		if (write(tmpfile, metafile, len) < 0) {
			sr_dbg("Failed to create new metadata: %s", strerror(errno));
			g_free(metafile);
			unlink(tmpname);
			return SR_ERR;
		}
		close(tmpfile);
		if (!(metasrc = zip_source_file(archive, tmpname, 0, -1))) {
			sr_err("Failed to create zip source for metadata.");
			g_free(metafile);
			unlink(tmpname);
			return SR_ERR;
		}
		if (zip_replace(archive, zs.index, metasrc) == -1) {
			sr_err("Failed to replace metadata file.");
			g_free(metafile);
			unlink(tmpname);
			return SR_ERR;
		}
		g_free(metafile);
	}
	g_key_file_free(kf);

	next_chunk_num = 1;
	num_files = zip_get_num_entries(archive, 0);
	for (i = 0; i < num_files; i++) {
		entry_name = zip_get_name(archive, i, 0);
		if (strncmp(entry_name, "logic-1", 7))
			continue;
		if (strlen(entry_name) == 7) {
			/* This file has no extra chunks, just a single "logic-1".
			 * Rename it to "logic-1-1" * and continue with chunk 2. */
			if (zip_rename(archive, i, "logic-1-1") == -1) {
				sr_err("Failed to rename 'logic-1' to 'logic-1-1'.");
				unlink(tmpname);
				return SR_ERR;
			}
			next_chunk_num = 2;
			break;
		} else if (strlen(entry_name) > 8 && entry_name[7] == '-') {
			chunk_num = strtoull(entry_name + 8, NULL, 10);
			if (chunk_num >= next_chunk_num)
				next_chunk_num = chunk_num + 1;
		}
	}
	snprintf(chunkname, 15, "logic-1-%d", next_chunk_num);
	if (!(logicsrc = zip_source_buffer(archive, buf, length, FALSE))) {
		unlink(tmpname);
		return SR_ERR;
	}
	if (zip_add(archive, chunkname, logicsrc) == -1) {
		unlink(tmpname);
		return SR_ERR;
	}
	if ((ret = zip_close(archive)) == -1) {
		sr_info("error saving session file: %s", zip_strerror(archive));
		unlink(tmpname);
		return SR_ERR;
	}
	unlink(tmpname);

	return SR_OK;
}