/* Author: Andrey Sukhanov 04/11/07 This ROOT program is to analyse n3c data usage: $ root root [0] gSystem.Load("Tn3c") root [1] Tn3c n("../data/070217192242.dq0"); root [2] TTree *t=n.MakeTree(); root [3] TH2S h("adc-chn","",128,0,128,256,0,256) root [4] gStyle->SetPalette(1); root [5] t->Draw("fadc:fchn>>adc-chn","","colz") */ #include // for ntohl #include #include #include #include #include "Tn3c.h" #include "Tn3cEvent.h" ClassImp(Tn3c) Int_t Tn3c::again() { size_t nitems; Tn3cEvent *ev; Int_t ii,toread; UShort_t id,val; ferr = 0; nitems = fread(fev, sizeof(fevsize), 1, fD); if(nitems!=1) { if(nitems==0) return 0;//EOF printf("Error reading evsize at %ld\n",ftell(fD)); return -1; } fevsize = ntohl(*(ULong_t*)fev); //if(fevsize!=262) ferr |= 1; //error: nonstandard length //printf("fevsize: %08x\n",fevsize); if(fevsize > 0xffffff) {printf("Strange evsize %088888888lx\n",fevsize);} if(fevsize>MAXEVLEN*sizeof(short)) { printf("Wrong evsize %04lx at %08lx\n",fevsize,ftell(fD)); nitems = fread(fev+sizeof(fevsize), sizeof(*fev)-sizeof(fevsize),600, fD); if((Int_t)nitems>0) for(ii=0;ii<(Int_t)nitems;ii++) { if(ii%16 == 0) printf("\n%04x:",ii); printf("%04x ",fev[ii]); } printf("\n"); return -3; } toread = fevsize-sizeof(fevsize); nitems = fread((char*)fev+sizeof(fevsize), sizeof(char), toread, fD); if((Int_t)nitems!=toread) { printf("nitems,toread = %08x,%08x\n",nitems,toread); printf("Error reading event at %08lx\n",ftell(fD)); return -2; } ev = new Tn3cEvent((Char_t*)fev); id = fev[2]; id = ntohs(id); if(id&0x8000==0) printf("Error ID %04x, ev# %08x at %08lx\n", id,fevcount,ftell(fD)); if(fevcount<4) { printf("ID %04x, ev# %08x at %08lx: %04x %04x %04x %04x\n", id,fevcount,ftell(fD),fev[2],fev[3],fev[4],fev[5]); printf("nch = %x, ch: %x %x %x %x %x\n",ev->fnch,ev->fch[0],ev->fch[1],ev->fch[2],ev->fch[3],ev->fch[4]); ev->Print(); } fbunch = id&0xff; //need to be changed //nitems = (ev->fsize - ev->fhlen - ev->ftlen); fnch = ev->fnch; //if(fnch > MAXCH) fnch = MAXCH; for(ii=0;ii=fnch) val = 0; else val = ev->fch[ii]; //val = ntohs(val); fadc[ii] = val&0xff; fchn[ii] = (val>>8)&0xff; } // Error checking if(ev->ftype >=3 ) {// Check for format errors if(ev->fevnum != fevnum+1) { if (fevcount ) { printf("**Error in event %li, Missed event\n",ev->fevnum); ferr |= ERRN3C_MISSED_EVENT; } } if(ev->fstamp!=0x900dc0de) { printf("**Error in event %li, Wrong stamp 0x%08lx != 0x900dc0de\n", ev->fevnum,ev->fstamp); ferr |= ERRN3C_STAMP; } } fevnum = ev->fevnum; ftree->Fill(); delete ev; return 100000-fevcount++; } Tn3c::Tn3c(Char_t *name) { Int_t rc; struct stat statv; const Char_t *tname; fD = NULL; ffile = NULL; ftree = NULL; if((tname = gSystem->ExpandPathName(name))!=0) { strcpy(fname,tname); delete [](char*)tname; } printf("Opening %s\n",fname); fD = fopen(fname,"r"); if(fD==NULL) { perror("Could not open file "); perror(fname); return; } printf("File opened %s\n",fname); rc = stat(fname,&statv); if(rc!=0) { perror("Cannot fstat"); fsize = -1; //return -2; } fsize = statv.st_size; printf("filesize = %d\n",fsize); char *substr = strrchr(fname,'.'); strcpy(substr+1,"root"); printf("Output file %s\n",fname); if(ffile) {printf("deleting file\n");delete ffile;} ffile = new TFile(fname,"recreate"); if(ffile == NULL) return; return; } Tn3c::~Tn3c() { if(fD) {printf("deleting fD\n"); fclose(fD); fD = NULL;} if(ftree) {printf("deleting tree\n"); delete ftree; ftree = NULL;} if(ffile) {printf("deleting file\n"); delete ffile; ffile = NULL;} } TTree* Tn3c::MakeTree() { if(ftree) {printf("deleting tree\n");delete ftree;} ftree = new TTree("n3ctree","n3c analysis tree"); if(ftree==NULL){printf("failed to create tree\n");return ftree;} ftree->Branch("bunch",(Int_t*)&fbunch,"fbunch/b"); ftree->Branch("evsize",(Int_t*)&fevsize,"evsize/i"); ftree->Branch("error",(Int_t*)&ferr,"ferr/s"); ftree->Branch("evnum",(Int_t*)&fevnum,"fevnum/i"); ftree->Branch("adc",(Int_t*)fadc,"fadc[128]/b"); ftree->Branch("chn",(Int_t*)fchn,"fchn[128]/b"); while(again() > 0); ftree->Write(); return ftree; } Bool_t Tn3c::IsOpen() { return fD != 0; }