Page MenuHomeHEPForge

EventShaker.cpp
No OneTemporary

EventShaker.cpp

#include <stdio.h>
#include <math.h>
#include <time.h>
#include "FROG_Events.h"
std::vector<char*> InputsFileToDelete;
std::vector<char*> InputsFile;
std::vector<int> InputsEvent;
char* Output = NULL;
bool Output_GZip = false;
unsigned short Output_Type = C_PRIMARY;
unsigned int Output_Size = 6;
unsigned short Event_Type = C_EVENTRT;
unsigned int Event_Size = 6;
void copy_data(FILE* in, FILE* out, unsigned int NBytes){
unsigned int BlockSize = 2048;
unsigned char* buffer = new unsigned char[BlockSize];
while(NBytes>BlockSize){
fread (buffer,1,BlockSize,in);
fwrite(buffer,1,BlockSize,out);
NBytes-=BlockSize;
}
BlockSize = NBytes;
fread (buffer,1,BlockSize,in);
fwrite(buffer,1,BlockSize,out);
}
void OutFile_PushEvent(FILE* out, char* inputFile, int EventIndex){
bool Input_GZip = false;
char* input_path = inputFile;
if(FROG_ZLIB_IsGZip(input_path)){
unsigned int len = strlen(inputFile);
input_path = new char[len];
memcpy(input_path,inputFile,len);
Input_GZip = true;
FROG_ZLIB_Inflate(inputFile,true);
InputsFileToDelete.push_back(input_path);
input_path[len-3] = '\0';
}
FILE* in = fopen(input_path,"rb");
if(!in){
printf("Can not open input %s\n",input_path);
printf("The file will be skipped\n");
return;
}
unsigned short In_Type;
unsigned int In_Size;
fread(&In_Type,1,2,in);
fread(&In_Size,1,4,in);
if(In_Type!=C_PRIMARY){
printf("Input %s doesn't seems to be a correct .vis file\n",input_path);
printf("The file will be skipped\n");
return;
}
FROG_Events* InEvents = new FROG_Events();
InEvents->Load(input_path);
for(int i=0;i<InEvents->NumberOfEvents();i++){
if(EventIndex>=0&& (i+1)!=EventIndex)continue;
printf("Pushing Event %i/%i of %s\n",EventIndex,InEvents->NumberOfEvents(),input_path);
FROG_Element_Event* tmp = (FROG_Element_Event*)InEvents->prim->getDaughter(i);
Event_Type = tmp->type();
Event_Size = tmp->EventNBytesToRead_+tmp->sizeOf();
fwrite(&Event_Type ,1,2,out);
fwrite(&Event_Size ,1,4,out);
fseek (in,tmp->EventPosInFile()-(tmp->sizeOf()-6),SEEK_SET);
copy_data(in, out, tmp->EventNBytesToRead_+(tmp->sizeOf()-6));
Output_Size += Event_Size;
}
fclose(in);
}
void algo(){
char* output_path = Output;
if(FROG_ZLIB_IsGZip(Output)){
unsigned int len = strlen(Output);
output_path = new char[len];
memcpy(output_path,Output,len-3);
output_path[len-3] = '\0';
Output_GZip = true;
}
FILE* out = fopen(output_path,"wb");
if(!out){
printf("Can not open/create output %s\n",output_path);
exit(0);
}
fseek (out,6,SEEK_SET);
for(unsigned int i=0;i<InputsFile.size();i++){
OutFile_PushEvent(out,InputsFile[i],InputsEvent[i]);
}
fseek (out,0,SEEK_SET);
fwrite(&Output_Type,1,2,out);
fwrite(&Output_Size,1,4,out);
fclose(out);
if(Output_GZip){
if(!FROG_ZLIB_Deflate(output_path)){
printf("Was not able to gzip the output\n");
}
}
for(unsigned int i=0;i<InputsFileToDelete.size();i++){
printf("Suppression of temporary file %s... ",InputsFileToDelete[i]);
if(remove(InputsFileToDelete[i])!=0){
printf("can not remove the file\n");
}else{
printf("done\n");
}
}
}
int main(int argc, char *argv[])
{
unsigned char Tag = 0; //0=undefined, 1=input, 2=output, 3=event
std::vector<char*> InputsTmp;
std::vector<unsigned int> InputsETmp;
if(argc<2){
printf("The program need at least one input file...\n");
printf("here is an example of the command that you can use : \n");
printf("visfilesmerger -i <input01.vis> -e -1 ... <input09.vis.gz> -o <output.vis>\n");
exit(0);
}
for(int i=1;i<argc;i++){
unsigned int len = strlen(argv[i]);
//Change to lowerCase
char LowCase[2048];
strcpy(LowCase,argv[i]);
for(unsigned int j=0;j<len;j++){
if(LowCase[j]>=65 && LowCase[j]<=90)LowCase[j]+=32;
}
if( strcmp(LowCase,"-i")==0){Tag = 1;
}else if(strcmp(LowCase,"-o")==0){Tag = 2;
}else if(strcmp(LowCase,"-e")==0){Tag = 3;
}else if(strcmp(LowCase,"-v")==0){
printf("FROG : Event Shaker\n");
printf("Version : 0.1\n");
printf("Authors List : \n");
printf("Loic.Quertenmont@cern.ch\n");
exit(0);
}else if(len>=4 && strcmp(LowCase+len-4,".vis" )==0 && Tag==1){InputsTmp.push_back(argv[i]);
}else if(len>=7 && strcmp(LowCase+len-7,".vis.gz")==0 && Tag==1){InputsTmp.push_back(argv[i]);
}else if(len>=4 && strcmp(LowCase+len-4,".vis" )==0 && Tag==2){Output = argv[i];
}else if(len>=7 && strcmp(LowCase+len-7,".vis.gz")==0 && Tag==2){Output = argv[i];
}else if(Tag==3){int Enumber;sscanf(argv[i], "%d", &Enumber);InputsETmp.push_back(Enumber);
if(InputsTmp.size()<InputsETmp.size())InputsTmp.push_back(InputsTmp[InputsTmp.size()-1]);
}else{
printf("\n");
printf("#######################################################################\n");
printf("# FROG .Vis Files Merger is a part of FROG #\n");
printf("# http://projects.hepforge.org/frog/ #\n");
printf("# Please have a look at the website for latest versions and news #\n");
printf("# Also register to the Frog mailing list if you are not a member yet #\n");
printf("#######################################################################\n");
printf("# HELP SCREEN #\n");
printf("#######################################################################\n");
printf("# Supported Commands are : #\n");
printf("# -i <input01.vis> -e 10 15 -i <input09.vis.gz> -e 5 -o <output.vis> #\n");
printf("# --> Create a new file output.vis #\n");
printf("# Fill it with event 10 & 15 of input01.vis #\n");
printf("# and with event 5 of input09.vis.gz #\n");
printf("# Note that files can be in .vis or .vis.gz #\n");
printf("# Default outputname is out.vis.gz #\n");
printf("# -help --> Print this screen #\n");
printf("# -v --> Print Version and Other Info #\n");
printf("#######################################################################\n");
printf("\n");
exit(0);
}
}
if(Output==NULL){
Output = new char[512];
sprintf(Output,"out.vis.gz");
}
for(unsigned int i=0;i<InputsTmp.size();i++){
if(strcmp(InputsTmp[i],Output)==0){
printf("%s is in the input file list, but is also the output file\nthis is not allowed, the file is removed from the input list\n",InputsTmp[i]);
}else{
InputsFile .push_back(InputsTmp [i]);
InputsEvent.push_back(InputsETmp[i]);
}
}
algo();
system("PAUSE");
}
// In case of questions, remarks or any other comments please contact us :
// Loic.Quertenmont@cern.ch

File Metadata

Mime Type
text/x-c
Expires
Sat, Dec 21, 12:33 PM (1 d, 20 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
4022785
Default Alt Text
EventShaker.cpp (6 KB)

Event Timeline