Evio2nt

From New IAC Wiki
Revision as of 19:21, 17 October 2016 by Burgjeff (talk | contribs)
Jump to navigation Jump to search

The program is supplied a .dat file, e.g. "r3245.dat", and creates a ROOT file named r3245.root containing a tree named "R1DC".

The TTree has a single branch, which contains the following structure:

 
struct evt_t
{
  int EventNumber;
  int TDC1190[100][129]; 
};

TDC1190[y][x] stores the hit number and channel number, where y is the hit number x is the TDC channel. TDC1190[0][x] stores number of hits in channel x.


code

#include <fstream>
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;


#ifndef __CINT__
#include "TFile.h"
#include "TH1.h"
#include "TH2.h"
#include "TProfile.h"
#include "TNtuple.h"
#include "TTree.h"
#include "TRandom.h"

#include "TApplication.h"
#include "TCanvas.h"
#include "TLine.h"
#include "TPaveLabel.h"

/* for posix */
#define _POSIX_SOURCE_ 1
#define __EXTENSIONS__


/*  misc macros, etc. */
#define MAXEVIOBUF   100000
#define MAXXMLSTRING 500000

/* include files */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <signal.h>
//#include "DecodeData.h"

/*  misc variables */
const char * filename;
//static char *filename;
static char *dictfilename = NULL;
static char *outfilename  = NULL;
static int gzip           = 0;
static char *main_tag     = (char*)"evio-data";
static int nevent         = 0;
static int skip_event     = 0;
static int max_event      = 0;
static int nevok          = 0;
static int evok[100];
static int nnoev          = 0;
static int noev[100];
static int nfragok        = 0;
static int fragok[100];
static int nnofrag        = 0;
static int nofrag[100];
static int pause1          = 0;
static int debug          = 0;
static int done           = 0;
static char xml[MAXXMLSTRING];


/* prototypes */
extern "C"
{
void decode_command_line(int argc, char **argv);
void writeit(FILE *f, char *s, int len);
int user_event_select(unsigned int *buf);
int user_frag_select(int tag);
int evOpen(const char *filename, const char *mode, int *handle);
int evRead(int handle, unsigned int *buf, int maxbuflen );
int evClose(int handle);
FILE *gzopen(char*,char*);
void gzclose(FILE*);
void gzwrite(FILE*,char*,int);
int set_event_tag(char *tag);
int set_bank2_tag(char *tag);
int set_n8(int val);
int set_n16(int val);
int set_n32(int val);
int set_n64(int val);
int set_w8(int val);
int set_w16(int val);
int set_w32(int val);
int set_w64(int val);
int set_xtod(int val);
int set_indent_size(int val);
int set_max_depth(int val);
int set_no_typename(int val);
int set_verbose(int val);
}



float GetData();

//
//  Tree format
//
//

struct evt_t
{
  int EventNumber;
/* 
first index is the hit number second is the channel number
TDC1190[0][x] = stored number of hits in channel x
TDC1190[y][x] = y is the hit number x is the TDC channel
*/
  int TDC1190[100][129]; 
};

//______________________________________________________________________________
//int main(int argc, char *argv[])
int main(int argc, char** argv)
{
  int i, j;
  int ChannelNum,buffLength,EventNum,EventType,EventLength;
  int HeaderID;
  int BunchID;
  evt_t evt;

  int handle,status;
  //size_t buf[MAXEVIOBUF];
  unsigned int buf[MAXEVIOBUF];
  FILE *out = NULL;
  char s[256];

  string teststring;
  string teststring2;
  const char * rootfilename;
  
 if(argc>1)
	{
//	  ChannelNum=atoi(argv[2]);
}
 else
{
	cout << "Please provide 1 arguments " << endl;
	cout << "Usage: evio2root -ffilename " << endl;
	exit(EXIT_FAILURE);
}

    cout << "Running program " << argv[0];

    cout << " the supplied arguments are:" << endl;

/*
    for (int i = 1; i < argc; i++) {
        cout << "arg " << i << " ";
        cout << argv[i] << endl;
    }

*/

   teststring=argv[1];
   teststring=string(argv[1],2,teststring.length());
   filename=teststring.c_str();
  cout << "filename = " << filename << endl;

  cout << "ADC " << ChannelNum << "for file " << filename << endl;	


//open root output file

  teststring2=string(argv[1],2,teststring.length()-4);
  cout << "teststring2 = " << teststring2 << endl;

   rootfilename=teststring2.c_str();

  cout << "root filename = " << rootfilename << endl;

  TFile *hfile  = new TFile(rootfilename,"RECREATE","DAQ");

  TTree *tree=new TTree("R1DC","Region 1 DC");

  tree->Branch("evt",&evt.EventNumber,"EventNumber/I:TDC1190[100][129]/I");

  float* NtupleEventArray = new float[66];


  //   ntuple = new TNtuple("ntuple","ADCs",NtupleColumnNames);
   NtupleEventArray[0]=0;

  /* decode command line */

//  decode_command_line(argc,argv);


  /* open evio input file */
  if((status=evOpen(filename,"r",&handle))!=0) {
    printf("\n ?Unable to open file %s, status=%d\n\n",filename,status);
    exit(EXIT_FAILURE);
  }

  /* loop over events, perhaps skip some, dump up to max_event events */
  nevent=0;
  NtupleEventArray[0]=0;

  while ((status=evRead(handle,buf,MAXEVIOBUF))==0) {
    nevent++;
    if(skip_event>=nevent)continue;
    if(user_event_select(buf)==0)continue;

    //DecodeData(buf,MAXXMLSTRING);


	for(i=1;i<66;i++)
	    NtupleEventArray[i]=0.0;
	NtupleEventArray[0]++;
	for(j=0;j<100;j++)
	  for(i=0;i<129;i++)
	    evt.TDC1190[j][i]=0;

	/*  The first word is a header and the last word is and event counter for the ADC output data*/

	buffLength=buf[0]; /* number of entries in the buf[buffLength] array*/
	EventType=(buf[1]>>16);

	if(EventType==1)
	  {
	    printf("\n\tBuffer Length=:\t%d\n",buf[0]);
	    
	    printf("Event type=%d\t%x\n",EventType,buf[1]);
	    
	    /*
	      dumb data array
	    for(i=0;i<buffLength;i++)
	    for(i=0;i<20;i++)
	      {
		printf("buf[%d]==%d\t%x\n",i,buf[i],buf[i]);
	      }
	    */
	    EventLength=buf[7];
	    printf("Found the following DAT banks\n");
	    for(i=9;i<8+EventLength;i++)
	      {
		/* fill histograms here     */
		HeaderID=((buf[i]>>27)&0x1F);

          if(HeaderID==8)/*General TDC header*/
            {
	      //	      printf("General TDC header\n");
              //printf("\tEventNumber=0x%x\t%d\n",(buf[i]>>5)&0x3FFFFF,(buf[i]>>5)&0x3FFFFF);
	      printf("Event:%d\n",(buf[i]>>5)&0x3FFFFF);
              
	      NtupleEventArray[0]=((buf[i]>>5)&0x3FFFFF);
	      evt.EventNumber=((buf[i]>>5)&0x3FFFFF);
            }
          if(HeaderID==1)/*TDC Header*/
            {

              /*
               printf("TDC header\n");
              printf("\tEventID=0x%x\t%d\n",(buf[i]>>12)&0xFFF,(buf[i]>>12)&0xFFF);
              printf("\tTDCnum=0x%x\t%d\n",(buf[11]>>24)&0x03,(buf[11]>>24)&0x3);
              */
	      BunchID=(buf[11])&0xfff;
              printf("\tBunchID=0x%x\t%d=%d\n",(buf[11])&0xfff,(buf[11])&0xfff,BunchID);
            }
          if(HeaderID==3)/*TDC trailer*/
            {
              /*
              printf("TDC trailer\n");
              printf("\tEventID=0x%x\t%d\n",(buf[i]>>12)&0xFFF,(buf[i]>>12)&0xFFF);
              printf("\tWord Count=0x%x\t%d\n",(buf[i]&0xFFF),(buf[i]&0xFFF));
              */
            }
          if(HeaderID==0)/*TDC data*/
            {
              //              printf("TDC data\n");
              //              printf("\tChan=0x%x\t%d\n",(buf[i]>>19)&0x7F,(buf[i]>>19)&0x7F);
              //printf("\tMeasurement=0x%x\t%d\n",(buf[i]&0x7FFFF),(buf[i]&0x7FFFF));
              printf("buf[%d]=%x\t",i,buf[i]);
              printf("\tChan:%d\t",1+(buf[i]>>21)&0x1F);
              printf("Measurement:%d\t",(buf[i]&0x1FFFFF));
              printf("%x\n",(buf[i]&0x1FFFFF));
              printf("Measurement-BunchID :%d\n",(buf[i]&0x1FFFFF)-BunchID);
		  //		  NtupleEventArray[((buf[i]>>19)&0x7F)-79]=((buf[i]&0x7FFFF));
	      //	      if(((buf[i]>>19)&0x7F)>0 && ((buf[i]>>19)&0x7F) < 129 && i==12)
	      //		    evt.TDC1190[((buf[i]>>19)&0x7F)]=(buf[i]&0x7FFFF)-BunchID;
	      if(((buf[i]>>21)&0x1F)>=0 && ((buf[i]>>21)&0x1F) < 129 )
		{
		  evt.TDC1190[0][1+((buf[i]>>21)&0x1F)]++; /* this counts the number of triggers*/
		  evt.TDC1190[evt.TDC1190[0][1+((buf[i]>>21)&0x1F)]][1+((buf[i]>>21)&0x1F)]=(buf[i]&0x1FFFFF);
		  printf("Channel %d has %d hits with last hit = %d\n", 1+(buf[i]>>21)&0x1F,evt.TDC1190[0][1+((buf[i]>>21)&0x1F)],buf[i]&0x1FFFFF);

		}
	      //	      if(((buf[i]>>19)&0x7F)>0 && ((buf[i]>>19)&0x7F) < 129 )
	      //		    evt.TDC1190[((buf[i]>>19)&0x7F)]=((buf[i]&0x1FFFF));

            }
          if(HeaderID==4)/*TDC Error*/
            {
              printf("TDC Error\n");
            }
	      
	  //		i+=buf[i];

	    
	      }
	    if((done!=0)||((nevent>=max_event+skip_event)&&(max_event!=0)))break;
	    
	    //	    ntuple->Fill(NtupleEventArray);
	    tree->Fill();
	  }
	printf("Event %d Summary\n",evt.EventNumber);
	for(j=0;j<129;j++)
	  {
	    //	    for(i=0;i<129;i++)
	      {
		if(evt.TDC1190[0][j]>0)
		  printf("Channel %d has %d hits with last hit = %d\n", j,evt.TDC1190[0][j],evt.TDC1190[evt.TDC1190[0][j]][j]);
	      }
	  }

  }
//  printf("Data for Channel # %d\n",ChannelNum);
//Hist1->Draw();
//c->Update();


  /* done */

  hfile->Write();

  // Close the file. Note that this is automatically done when you leave
  // the application.
  hfile->Close();

  evClose(handle);
  if((out!=NULL)&&(gzip!=0))gzclose(out);


//  theApp.Run();

  exit(EXIT_SUCCESS);

}
#endif

float GetData()
{
  Float_t random = gRandom->Rndm(1);
  
  return (2.5+random*5.0);
}



void writeit(FILE *f, char *s, int len) {

  if(f==NULL) {
    printf("%s",s);
  } else if (gzip==0) {
    fprintf(f,s,len);
  } else {
    gzwrite(f,s,len);
  }

}


/*---------------------------------------------------------------- */


int user_event_select(unsigned int *buf) {

  int i;
  int event_tag = buf[1]>>16;


  if((nevok<=0)&&(nnoev<=0)) {
    return(1);

  } else if(nevok>0) {
    for(i=0; i<nevok; i++) if(event_tag==evok[i])return(1);
    return(0);
    
  } else {
    for(i=0; i<nnoev; i++) if(event_tag==noev[i])return(0);
    return(1);
  }

}


/*---------------------------------------------------------------- */


int user_frag_select(int tag) {

  int i;

  if((nfragok<=0)&&(nnofrag<=0)) {
    return(1);

  } else if(nfragok>0) {
    for(i=0; i<nfragok; i++) if(tag==fragok[i])return(1);
    return(0);
    
  } else {
    for(i=0; i<nnofrag; i++) if(tag==nofrag[i])return(0);
    return(1);
  }

}




/*---------------------------------------------------------------- */


void decode_command_line(int argc, char**argv) {
  
  const char *help = 
    "\nusage:\n\n  evio2xml [-max max_event] [-pause] [-skip skip_event] [-dict dictfilename]\n"
    "           [-ev evtag] [-noev evtag] [-frag frag] [-nofrag frag] [-max_depth max_depth]\n"
    "           [-n8 n8] [-n16 n16] [-n32 n32] [-n64 n64]\n"
    "           [-w8 w8] [-w16 w16] [-w32 w32] [-w64 w64]\n"
    "           [-verbose] [-xtod] [-m main_tag] [-e event_tag]\n"
    "           [-indent indent_size] [-no_typename] [-debug]\n"
    "           [-out outfilenema] [-gz] filename\n";
  int i;
    
  printf("\nargc=%d\n",argc);    
  if(argc<2) {
    printf("%s\n",help);
    exit(EXIT_SUCCESS);
  } 


  /* loop over arguments */
  i=1;
  while (i<argc) {
    if (strncasecmp(argv[i],"-h",2)==0) {
      printf("%s\n",help);
      exit(EXIT_SUCCESS);

    } else if (strncasecmp(argv[i],"-pause",6)==0) {
      pause1=1;
      i=i+1;

    } else if (strncasecmp(argv[i],"-out",4)==0) {
      outfilename=strdup(argv[i+1]);
      i=i+2;

    } else if (strncasecmp(argv[i],"-debug",6)==0) {
      debug=1;
      i=i+1;

    } else if (strncasecmp(argv[i],"-gz",3)==0) {
      gzip=1;
      i=i+1;

    } else if (strncasecmp(argv[i],"-verbose",8)==0) {
      //      set_verbose(1);
      i=i+1;

    } else if (strncasecmp(argv[i],"-no_typename",12)==0) {
      //      set_no_typename(1);
      i=i+1;

    } else if (strncasecmp(argv[i],"-max_depth",10)==0) {
      //      set_max_depth(atoi(argv[i+1]));
      i=i+2;

    } else if (strncasecmp(argv[i],"-indent",7)==0) {
      //      set_indent_size(atoi(argv[i+1]));
      i=i+2;

    } else if (strncasecmp(argv[i],"-max",4)==0) {
      max_event=atoi(argv[i+1]);
      i=i+2;

    } else if (strncasecmp(argv[i],"-skip",5)==0) {
      skip_event=atoi(argv[i+1]);
      i=i+2;

    } else if (strncasecmp(argv[i],"-dict",5)==0) {
      dictfilename=strdup(argv[i+1]);
      i=i+2;

    } else if (strncasecmp(argv[i],"-xtod",5)==0) {
      //      set_xtod(1);
      i=i+1;

    } else if (strncasecmp(argv[i],"-ev",3)==0) {
      if(nevok<(sizeof(evok)/sizeof(int))) {
	evok[nevok++]=atoi(argv[i+1]);
	i=i+2;
      } else {
	printf("?too many ev flags: %s\n",argv[i+1]);
      }

    } else if (strncasecmp(argv[i],"-noev",5)==0) {
      if(nnoev<(sizeof(noev)/sizeof(int))) {
	noev[nnoev++]=atoi(argv[i+1]);
	i=i+2;
      } else {
	printf("?too many noev flags: %s\n",argv[i+1]);
      }

    } else if (strncasecmp(argv[i],"-frag",5)==0) {
      if(nfragok<(sizeof(fragok)/sizeof(int))) {
	fragok[nfragok++]=atoi(argv[i+1]);
	i=i+2;
      } else {
	printf("?too many frag flags: %s\n",argv[i+1]);
      }

    } else if (strncasecmp(argv[i],"-nofrag",7)==0) {
      if(nnofrag<(sizeof(nofrag)/sizeof(int))) {
	nofrag[nnofrag++]=atoi(argv[i+1]);
	i=i+2;
      } else {
	printf("?too many nofrag flags: %s\n",argv[i+1]);
      }

    } else if (strncasecmp(argv[i],"-n8",3)==0) {
      //      set_n8(atoi(argv[i+1]));
      i=i+2;

    } else if (strncasecmp(argv[i],"-n16",4)==0) {
      //      set_n16(atoi(argv[i+1]));
      i=i+2;

    } else if (strncasecmp(argv[i],"-n32",4)==0) {
      //      set_n32(atoi(argv[i+1]));
      i=i+2;

    } else if (strncasecmp(argv[i],"-n64",4)==0) {
      //      set_n64(atoi(argv[i+1]));
      i=i+2;

    } else if (strncasecmp(argv[i],"-w8",3)==0) {
      //      set_w8(atoi(argv[i+1]));
      i=i+2;

    } else if (strncasecmp(argv[i],"-w16",4)==0) {
      //      set_w16(atoi(argv[i+1]));
      i=i+2;

    } else if (strncasecmp(argv[i],"-w32",4)==0) {
      //      set_w32(atoi(argv[i+1]));
      i=i+2;

    } else if (strncasecmp(argv[i],"-w64",4)==0) {
      //      set_w64(atoi(argv[i+1]));
      i=i+2;

    } else if (strncasecmp(argv[i],"-m",2)==0) {
      main_tag=argv[i+1];
      i=i+2;

    } else if (strncasecmp(argv[i],"-e",2)==0) {
      //      set_event_tag(strdup(argv[i+1]));
      i=i+2;

    } else if (strncasecmp(argv[i],"-",1)==0) {
      printf("\n  ?unknown command line arg: %s\n\n",argv[i]);
      exit(EXIT_FAILURE);

    } else {
      break;
    }
  }
  
  /* last arg better be filename */
  //  filename=argv[argc];
//  filename="temp.dat";

  return;
}


/*---------------------------------------------------------------- */