/* $Id: GTSort.cxx,v 1.2 2011/03/08 17:00:02 tl Exp $ */ /* on--line sorter for gretina data */ #include #include #include #ifdef SOLARIS #include #endif #ifdef LINUX #include #endif #include #include #include #include #include #include #include #include #include #include #include "Rtypes.h" #include "TROOT.h" #include "TFile.h" #include "TRandom.h" #include "TH1.h" #include "TH2.h" #include "TObjArray.h" #include "TObject.h" #include "TKey.h" #include "TSystem.h" #include "TCutG.h" #include "TTree.h" #include "TMapFile.h" #include "GSudpReceiver.h" #include "efftape.h" #include "gsII.h" #define CC_ID1 10 /* instead of 29 as default */ #define CC_ID2 11 /* instead of 39 as default */ #define GRETINA 0 #include "GTSort.h" /* global variables */ int nEvents = 1000000000; char ROOTFileOption[STRLEN] = "UNDEFINED"; char GTSortInputFile[STRLEN]; int UseShareMemFile = FALSE; unsigned int StartMapAddress = 0; char ShareMemFile[STRLEN] = "GTSort.map"; int InputSrc = NOTDEF; int InputSourceOnCommandLine = 0; int HaveRootFileName = 0; int WeWereSignalled = FALSE; /* signal */ int GTudpPort = 1101; int UseRootFile = FALSE, SizeShareMemFile = FALSE; int UpdateRootFile = 0; char ROOTFile[STRLEN] = "GTSort.root"; char spname[STRLEN]; int firstEvent = 0; int GSudpPort = 1101; float Beta = 0; int NumToPrint = 25; int DumpEvery = 10; int GTDataFile; float HiResDataMultFactor = 1.0; int CurEvNo = 0, NprintEvNo = 0; int bufPos = 0; int nBadTestPat = 0; int egemin = 2; double oldLEDTs = 0; double oldCFDTs = 0; time_t tdmp = 0, tdmplast; float ehiGeOffset[NGE]; float ehiGeGain[NGE]; int ehiDoGainCor = 0; int idOffset = 0; char CommandFileName[STRLEN] = "GSSort.command"; #include "GTUdpReceiver.h" int time_stamp (); TH1D *mkTH1D (char *, char *, int, double, double); TH2F *mkTH2F (char *, char *, int, double, double, int, double, double); /* macros */ #include "GTMacros.h" /* user */ #include "UserInclude.h" #define DEBUG1 0 /*----------------------------------------------------------------------------*/ int GTGetNetEv (GTEVENT * Event) { /* function will read the gretina UDP data sender and */ /* extract the events, but not interpret them. */ /* That is done higher up. All this function does */ /* is to pull out the header and the trace */ /* and send them back to to calling function */ /* through the GTEVENT Event structure */ /* this function is specific to network processing */ /* declarations */ static int newbuf = 1, pos = 0, len = 0; static size_t siz; /* siz, len, and SEG_SIZE in bytes. pos in 2-byte words. */ static unsigned short int buffer[SEG_SIZE / sizeof (unsigned short)]; int i; unsigned short int t1, t2, t3, t4; static int seqNum, lastSeqNum; #if(DEBUG1) printf ("\n\nGTGetNetEv called, newbuf=%i,pos=%i\n", newbuf, pos); #endif /* should we read a new buffer? */ if (!newbuf) { if (!len || (pos * 2) >= (siz / len) * len) { newbuf = 1; } } if (newbuf) { /* get a raw buffer from network */ UdpReceiveBuf ((char *) buffer, &siz, SEG_SIZE); if (siz != SEG_SIZE) { return 2; } newbuf = 0; /* sequence number ? */ seqNum = ntohl (*((int *) buffer)); if (lastSeqNum != -1) { if (seqNum != (lastSeqNum + 1)) { printf ("Receiver Sequence mismatch %d %d \n", lastSeqNum + 1, seqNum); } } lastSeqNum = seqNum; /* reset current position */ pos = 2; #if(DEBUG1) /* debug */ printf ("read network buffer of size %i\n", siz); fflush (stdout); #endif } else { #if(0) printf ("continue event hunt from pos=%i\n", pos); #endif } /* check for beginning of event */ if (buffer[pos] != 0xaaaa || buffer[pos + 1] != 0xaaaa) { newbuf = 1; #if(DEBUG1) printf ("separator not found %hx \t %hx \n", buffer[pos], buffer[pos + 1]); for (int i = 0; i < 20; i++) printf ("buffer %hx \t %hx \n", buffer[i], buffer[i + 1]); fflush (stdout); #endif return (2); } /* store postition of start of event in "event stream" */ Event->pos += pos; #if(DEBUG1) printf ("new event starts at pos %i in this buffer \n", Event->pos); fflush (stdout); #endif /* copy header */ pos += 2; /* skip sep */ for (i = 0; i < HDRLENWORDS; i++) Event->hdr[i] = buffer[pos + i]; pos += HDRLENWORDS; #ifdef LINUX for (i = 0; i < HDRLENWORDS; i += 2) { t1 = (Event->hdr[i] & 0x00ff) << 8; t2 = (Event->hdr[i] & 0xff00) >> 8; t3 = (Event->hdr[i + 1] & 0x00ff) << 8; t4 = (Event->hdr[i + 1] & 0xff00) >> 8; Event->hdr[i] = t3 + t4; Event->hdr[i + 1] = t1 + t2; }; #endif /* find event length in bytes */ #ifdef ACQ_8CH Event->len = 4 * Event->hdr[1] + 4; #endif // ACQ_8CH #ifdef ACQ_10CH Event->len = 4 * (Event->hdr[1] & 0x7ff) + 4; //in bytes #endif if (len != Event->len) { len = Event->len; printf ("Event length %d found\n", len); } Event->traceLen = Event->len - HDRLENBYTES - 4; //in bytes #if(DEBUG1) printf ("DATA HEADER: Event->len=%i bytes, Event->traceLen=%i bytes\n", Event->len, Event->traceLen); #endif /* copy trace */ for (i = 0; i < (Event->traceLen / 2); i++) Event->trace[i] = buffer[pos + i]; pos += Event->traceLen / 2; #ifdef LINUX for (i = 0; i < Event->traceLen / 2; i += 2) { t1 = (Event->trace[i] & 0x00ff) << 8; t2 = (Event->trace[i] & 0xff00) >> 8; t3 = (Event->trace[i + 1] & 0x00ff) << 8; t4 = (Event->trace[i + 1] & 0xff00) >> 8; Event->trace[i] = t3 + t4; Event->trace[i + 1] = t1 + t2; }; #endif Event->traceLen /= 2; #if(DEBUG1) printf ("REAL EVENT: Event->traceLen=%i words\n", Event->traceLen); #endif /* done */ #if(DEBUG1) printf ("\n\nGTGetNetEv returns, newbuf=%i,pos=%i\n", newbuf, pos); #endif return (0); } /*----------------------------------------------------------------------------*/ int GTGetDiskEv (GTEVENT * Event) { /* function will read the gretina data file and */ /* extract the events, but not interpret them. */ /* That is done higher up. All this function does */ /* is to pull out the header and the trace */ /* and send them back to to calling function */ /* through the GTEVENT Event structure */ /* this function is specific to data file processing */ /* declarations */ static int nn = 0; int siz, i, i1, neoe; int testPattern = 0; int Counter = 0; int TEMP[MAXLENINTS]; int t1, t2, t3, t4; /* normally we have one EOE marks */ /* when we start reading......... */ /* note: no need to swap bytes... */ while (testPattern != EOE) { siz = read (GTDataFile, (char *) &testPattern, 4); if (siz != 4) return (5); if (Counter < MAXLENINTS) TEMP[Counter] = testPattern; bufPos += siz / 4; Counter++; //Counter = EventLength + 1 }; if (Counter == 1) { printf ("** File Header or Double Event Header **\n"); return (1); } #ifdef LINUX //Swap bytes for (i = 0; i < Counter - 1; i++) if (i < MAXLENINTS) { t1 = (TEMP[i] & 0x000000ff) << 24; t2 = (TEMP[i] & 0x0000ff00) << 8; t3 = (TEMP[i] & 0x00ff0000) >> 8; t4 = (TEMP[i] & 0xff000000) >> 24; TEMP[i] = t1 + t2 + t3 + t4; } #endif Event->hdr[0] = (unsigned short int) (TEMP[0] & 0xffff); Event->hdr[1] = (unsigned short int) ((TEMP[0] >>= 16) & 0xffff); /* find event length */ #ifdef ACQ_8CH Event->len = Event->hdr[1] + 1; #endif #ifdef ACQ_10CH Event->len = (Event->hdr[1] & 0x7ff) + 1; #endif if (Event->len != Counter) { //inconsistent event length if (Counter <= HDRLENINTS) { //unreasonable EOE markers nBadTestPat++; if (nBadTestPat == 10) printf ("** suspending warnings about bad EOE markers...\n"); if (nBadTestPat < 10) { printf ("ooops: after event # %i (Counter = %i)", CurEvNo, Counter); printf (" at bufPos %6i, bad event appeared, search ahead\n", bufPos); fflush (stdout); } return (2); } if (Counter > HDRLENINTS) { //reasonable EOE markers, we can try to get data out nBadTestPat++; if ((nBadTestPat % 1000) == 0) printf ("** warning: %i inconsistant EOE markers\n", nBadTestPat); } } for (i = 1; i < HDRLENINTS; i++) { Event->hdr[2 * i] = (unsigned short int) (TEMP[i] & 0xffff); Event->hdr[2 * i + 1] = (unsigned short int) ((TEMP[i] >>= 16) & 0xffff); } Event->traceLen = 0; for (i = HDRLENINTS; i < Counter - 1; i++) if (i < MAXLENINTS) { Event->trace[2 * (i - HDRLENINTS)] = (unsigned short int) (TEMP[i] & 0xffff); Event->trace[2 * (i - HDRLENINTS) + 1] = (unsigned short int) ((TEMP[i] >>= 16) & 0xffff); Event->traceLen += 2; } #if(0) if (NprintEvNo <= NumToPrint) printf ("Event->traceLen=%i, Event->len=%i, Counter=%i\n", Event->traceLen, Event->len, Counter); #endif Event->pos = bufPos; /* print header */ #if(0) if (NprintEvNo <= NumToPrint) for (i = 0; i < HDRLENWORDS; i++) printf ("Event->hdr[%2i]=0x%4.4x, %6i\n", i, Event->hdr[i], Event->hdr[i]); #endif /* print trace */ #if(0) if (NprintEvNo <= NumToPrint) for (i = 0; i < Event->traceLen; i++) printf ("Event->trace[%4i]=0x%4.4x, %6i\n", i, Event->trace[i], Event->trace[i]); #endif /* done */ nn++; return (0); } /*----------------------------------------------------------------------------*/ int main (int argc, char **argv) { /*--------------*/ /* declarations */ /*--------------*/ int j, i, HaveChatFile = 0; char *p; char ChatFileName[STRLEN]; int GTacq (char *); int time_stamp (); char str2[STRLEN], str3[STRLEN], str4[STRLEN]; /* inform of version of root used */ printf ("from %s on %s\n", __FILE__, __DATE__); printf ("RCS version $Id: GTSort.cxx,v 1.2 2011/03/08 17:00:02 tl Exp $\n"); /*------*/ /* help */ /*------*/ if (argc < 2) { printf ("\n"); printf ("use: %s -chat file [-help] [-version] [-input disk|net|tape file|drive|port UPDATE|RECREATE -rootfile name.root]\n", argv[0]); printf ("\n"); return (0); }; /*--------------------*/ /* parse command line */ /* and call GTacq */ /*--------------------*/ j = 1; /* current command line arg position */ if (argc > 1) while (j < argc) { if ((p = strstr (argv[j], "-version")) != NULL) { printf ("$Id: GTSort.cxx,v 1.2 2011/03/08 17:00:02 tl Exp $\n"); printf ("This version has Float 2D matrices\n"); exit (0); } else if ((p = strstr (argv[j], "-help")) != NULL) { printf ("\n"); printf ("GTSort is documented on the WWW, URL:\n"); printf ("\n"); printf (" http://www.phy.anl.gov/gs/doc/GTSort\n"); printf ("\n"); exit (0); } else if ((p = strstr (argv[j], "-input")) != NULL) { /* check that user specified enough arguments */ j++; if ((argc - j) < 3) { printf ("you must specify -input disk|net|tape file|drive|port UPDATE|RECREATE\n"); exit (0); }; /* input source will be specified on command line. */ /* Set flag to prevent processing of */ /* input source in chat script */ InputSourceOnCommandLine = 1; /* determine input source */ strcpy (str2, argv[j++]); strcpy (str3, argv[j++]); strcpy (ROOTFileOption, argv[j++]); printf ("\n"); printf ("*** GSSort will take \"input source\" from command line\n"); if (strcmp ("tape", str2) == 0) { printf ("will take input from tape\n"); InputSrc = TAPE; fflush (stdout); } else if (strcmp ("disk", str2) == 0) { printf ("will take input from disk\n"); strcpy (GTSortInputFile, str3); InputSrc = DISK; fflush (stdout); } else if (strcmp ("net", str2) == 0) { printf ("will take input from UDP sender\n"); GTudpPort = atoi (str3); SetupUdpReceiver (GTudpPort); printf ("__using port %i\n", GTudpPort); InputSrc = NET; fflush (stdout); } else { printf ("unknown input option: %s\n", str2); printf ("aborting\n"); fflush (stdout); exit (0); }; if ((p = strstr (ROOTFileOption, "UPDATE")) != NULL) { UpdateRootFile = TRUE; printf ("will update root file\n"); } else if ((p = strstr (ROOTFileOption, "RECREATE")) != NULL) { UpdateRootFile = FALSE; printf ("will recreate root file\n"); } else { printf ("option <%s> not valid!\n", str4); printf ("valid options are: RECREATE or UPDATE\n\n"); exit (0); }; printf ("\n"); } else if ((p = strstr (argv[j], "-chat")) != NULL) { j++; strcpy (ChatFileName, argv[j++]); printf ("will read sorting instructions from chatfile: %s\n", ChatFileName); system ("ulimit -a"); HaveChatFile = 1; } else if ((p = strstr (argv[j], "-rootfile")) != NULL) { j++; strcpy (ROOTFile, argv[j++]); printf ("rootfile name specified on command line\n"); printf ("__will store spectra in rootfile: %s\n", ROOTFile); HaveRootFileName = 1; UseRootFile = 1; } else { printf ("command line argument not understood!\n"); printf ("%s: I was called as: \n--->[", argv[0]); for (i = 0; i < argc; i++) { printf ("%s ", argv[i]); fflush (stdout); } printf ("]\non "); time_stamp (); exit (0); } }; /* now start the sorter */ if (HaveChatFile) GTacq (ChatFileName); else { printf ("you must specify a chat script\n"); exit (0); } } /*--------------------------------------------------------------------------*/ void signal_catcher (int sigval) { int time_stamp (); printf ("\nGSSort/GTacq received signal <%i> on ", sigval); time_stamp (); WeWereSignalled = TRUE; fflush (stdout); } /*---------------------------------------------------------------------------*/ void sdummyload (Long_t size) { /* dummy load a shared memory to find out what */ /* start address it chooses................... */ TMapFile *m; m = TMapFile::Create ("dummy.map", "recreate", size); StartMapAddress = (unsigned int) m->GetMmallocDesc (); m->Print (); /* close and remove dummy map file */ m->Close (); gSystem->Exec ("\\rm dummy.map"); } int GSSort_read_chat (char *name) { /* declarations */ FILE *fp, *fp1; char *pc, *pc1, str[STRLEN] = { '0' }, str1[STRLEN] = { '0'}, str2[STRLEN] = { '0'}; char str3[STRLEN], str4[STRLEN], str5[STRLEN], str6[STRLEN]; int nn = 0, nni = 0, st, PType; char *p; int i, k, i1, i2, i3, i4, i5, i6; int j1, j2, j3, j4, j5, j6, j7; float f1, f2, f3, f4; int echo = 0, nret; double d1; /* prototypes */ TCutG *rd2dwin (Char_t *); int FindPEvMatNo (char *, int *); void FindCondNo (char *, int *); int SetFERAVSN (char *, int); void InitializeFERAvsntable (); void ZeroFERAtypeVSN (); void PrintFERATypes (); void SetNPosWarn (int); void SetRecordVer_tape (int); void SetRecordVer_disk (int); void SetRecordVer_net (int); int str_decomp (char *, int, int *); void FindTimeMaskNo (char *, int *); int RdOffFile (char *, int *); int RdGeCalFile (char *, float *, float *); void CheckNoArgs (int, int, char *); void SetSpecial (char *str); void SetExportModNWords (char *, int); void SetlongRangeTDCNWords (char *, int); void setIsomerIDs (int); void SetClockPar (int, float, float); void SetFERAvsntable (int, int); void SetnFeraDebug (int); /* initialize */ /* open chat file */ if ((fp = fopen (name, "r")) == NULL) { printf ("error: could not open chat file: <%s>\n", name); exit (0); }; printf ("chat file: <%s> open\n", name); printf ("\n"); fflush (stdout); /* read content and act */ pc = fgets (str, STRLEN, fp); /* rmEndComment(str, STRLEN); */ while (pc != NULL) { if (echo) printf ("chat->%s", str); fflush (stdout); /* attemp to interpret the line */ if ((p = strstr (str, "nevents")) != NULL) { nret = sscanf (str, "%s %i", str1, &nEvents); CheckNoArgs (nret, 2, str); printf ("will sort a max of %i events\n", nEvents); fflush (stdout); } else if (str[0] == 35) { /* '#' comment line, do nothing */ nni--; /* don't count as instruction */ } else if (str[0] == 59) { /* ';' comment line, do nothing */ nni--; /* don't count as instruction */ } else if (str[0] == 10) { /* empty line, do nothing */ nni--; /* don't count as instruction */ } else if ((p = strstr (str, "firstevent")) != NULL) { nret = sscanf (str, "%s %i", str1, &firstEvent); CheckNoArgs (nret, 2, str); printf ("will start sorting at event %d\n", firstEvent); fflush (stdout); } else if ((p = strstr (str, "abort")) != NULL) { printf ("will abort\n"); fclose (fp); printf ("\n"); printf ("chat file: <%s> closed\n", name); printf ("processed %i sort instructions and %i lines\n", nni, nn); printf ("\n"); fflush (stdout); exit (0); } else if ((p = strstr (str, "output")) != NULL) { if (!HaveRootFileName) { nret = sscanf (str, "%s %s", str1, ROOTFile); CheckNoArgs (nret, 2, str); printf ("will store spectra in rootfile: %s\n", ROOTFile); UseRootFile = 1; fflush (stdout); } else { printf ("ignoring chatfile option - since rootfile name is specified on commandline\n"); }; } else if ((p = strstr (str, "sharedmem")) != NULL) { st = nret = sscanf (str, "%s %s %i", str1, ShareMemFile, &SizeShareMemFile); CheckNoArgs (nret, 3, str); printf ("will use shared memory file: %s\n", ShareMemFile); printf ("__of max size: %i bytes\n", SizeShareMemFile); UseShareMemFile = 1; fflush (stdout); } else if ((p = strstr (str, "startmapaddress")) != NULL) { nret = sscanf (str, "%s %s", str1, str2); if ((strstr (str2, "auto")) != NULL) { printf ("%s\n", str2); // nret = sscanf(str, "%s 0x%x", str1, &StartMapAddress); sdummyload (600000000); CheckNoArgs (nret, 2, str); } else { nret = sscanf (str, "%s 0x%x", str1, &StartMapAddress); CheckNoArgs (nret, 2, str); } printf ("will start shared mem at address: 0x%8.8x\n", StartMapAddress); fflush (stdout); } else if ((p = strstr (str, "include")) != NULL) { nret = sscanf (str, "%s %s", str1, str2); CheckNoArgs (nret, 2, str); printf ("will now include chatscript %s\n", str2); fflush (stdout); GSSort_read_chat (str2); printf ("done including chatscript %s\n", str2); fflush (stdout); } else if ((p = strstr (str, "rootfileoption")) != NULL) { if (!InputSourceOnCommandLine) { nret = sscanf (str, "%s %s", str1, ROOTFileOption); CheckNoArgs (nret, 2, str); printf ("will update %s with option <%s>\n", ROOTFile, ROOTFileOption); if ((p = strstr (ROOTFileOption, "UPDATE")) != NULL) { UpdateRootFile = TRUE; printf ("will update root file\n"); } else if ((p = strstr (ROOTFileOption, "RECREATE")) != NULL) { UpdateRootFile = FALSE; printf ("will recreate root file\n"); } else { printf ("option <%s> not valid!\n", ROOTFileOption); printf ("valid options are: RECREATE or UPDATE\n\n"); exit (0); }; } else { printf ("__ignoring chatscript option since it is specified on command line\n"); }; fflush (stdout); } else if ((p = strstr (str, "printevents")) != NULL) { nret = sscanf (str, "%s %i", str1, &NumToPrint); CheckNoArgs (nret, 2, str); printf ("will print details of the first %i events\n", NumToPrint); fflush (stdout); } else if ((p = strstr (str, "beta")) != NULL) { nret = sscanf (str, "%s %f", str1, &Beta); CheckNoArgs (nret, 2, str); printf ("will use Beta (v/c) correction of %f\n", Beta); fflush (stdout); } else if ((p = strstr (str, "dumpevery")) != NULL) { nret = sscanf (str, "%s %i", str1, &DumpEvery); CheckNoArgs (nret, 2, str); printf ("will dump to output file every %i minutes\n", DumpEvery); fflush (stdout); } else if ((p = strstr (str, "hiresdatamult")) != NULL) { nret = sscanf (str, "%s %f", str1, &HiResDataMultFactor); CheckNoArgs (nret, 2, str); printf ("will multiply hi res data with factor %f\n", HiResDataMultFactor); fflush (stdout); } else if ((p = strstr (str, "egemin")) != NULL) { nret = sscanf (str, "%s %i", str1, &egemin); CheckNoArgs (nret, 2, str); printf ("will require %i minimum germanium signal\n", egemin); fflush (stdout); } else if ((p = strstr (str, "exit")) != NULL) { printf ("will skip rest of chat file\n"); fclose (fp); printf ("\n"); printf ("chat file: <%s> closed\n", name); printf ("processed %i sort instructions and %i lines\n", nni, nn); printf ("\n"); fflush (stdout); return (0); } else if ((p = strstr (str, "hiresdatamult")) != NULL) //}else if ((p = strstr(str, "hiresmultfactor")) != NULL) { nret = sscanf (str, "%s %f", str1, &HiResDataMultFactor); CheckNoArgs (nret, 2, str); printf ("will multiply hi res data with factor of %d\n", HiResDataMultFactor); fflush (stdout); } else if ((p = strstr (str, "idoffset")) != NULL) { nret = sscanf (str, "%s %i", str1, &idOffset); CheckNoArgs (nret, 2, str); printf ("will subtrac %i from the calculated IDs\n", idOffset); fflush (stdout); } else if ((p = strstr (str, "input")) != NULL) { if (!InputSourceOnCommandLine) { nret = sscanf (str, "%s %s %s", str1, str2, str3); CheckNoArgs (nret, 3, str); if (strcmp ("tape", str2) == 0) { printf ("will take input from tape\n"); InputSrc = TAPE; fflush (stdout); } else if (strcmp ("disk", str2) == 0) { printf ("will take input from disk\n"); strcpy (GTSortInputFile, str3); InputSrc = DISK; fflush (stdout); } else if (strcmp ("net", str2) == 0) { printf ("will take input from UDP sender\n"); GSudpPort = atoi (str3); SetupUdpReceiver (GSudpPort); printf ("__using port %i\n", GSudpPort); InputSrc = NET; fflush (stdout); } else { printf ("unknown input option: %s\n", str2); printf ("aborting\n"); fflush (stdout); exit (0); }; } #include "UserChat.h" else { printf ("input source specified on commandline, ignoring chatfile specification\n"); } } else { /*-----------------------------*/ /* chatscript read error point */ /*-----------------------------*/ printf ("line %2.2i in chat script, option :%s \n__not understood\n", nn, str); printf ("%i\n", str[0]); printf ("aborting\n"); fflush (stdout); exit (0); }; /* read next line in chat script */ nn++; /* line counter */ nni++; /* instruction counter */ pc = fgets (str, STRLEN, fp); }; /* done */ fclose (fp); printf ("\n"); printf ("chat file: <%s> closed\n", name); printf ("processed %i sort instructions and %i lines\n", nni, nn); printf ("\n"); fflush (stdout); return (0); }; /*----------------------------------------------------------------------------*/ int GTEvdecompose (GTEVENT * Event) { /* declarations */ int i; /* extract IDs */ #ifdef ACQ_8CH Event->chan_id = Event->hdr[0] & 0x7; Event->board_id = Event->hdr[0] >> 3; // Event->id = Event->board_id * 10 + Event->chan_id; Event->id = Event->hdr[0] - 512; #endif // ACQ_8CH #ifdef ACQ_10CH Event->chan_id = (Event->hdr[0] & 0xf); // Event->board_id = (Event->hdr[0] >> 11); Event->board_id = (Event->hdr[0] >> 4); //Event->id = (Event->board_id - (37 - 1)*4) * 10 + Event->chan_id; Event->id = Event->board_id * 10 + Event->chan_id; Event->id -= idOffset; #endif // ACQ_10CH /* extract high energy, per documentation */ /* I do not understand what happens here!? */ #ifdef ACQ_8CH Event->rawE = (int) ((Event->hdr[6] & 0x3f) << 16) + Event->hdr[5]; if (Event->rawE > 2097151) Event->rawE -= 4194304; Event->rawE = (Event->rawE < 0) ? -Event->rawE : Event->rawE; Event->ehi = (Event->rawE >> 7); #endif // ACQ_8CH #ifdef ACQ_10CH bool sign = 0; sign = (Event->hdr[6] & 0x0100); Event->rawE = (int) ((Event->hdr[6] & 0x00ff) << 16) + (int) Event->hdr[5]; if (Event->rawE >= 0 && Event->rawE < INT_MAX) { if (sign) { if ((((Event->id == CC_ID1)) || ((Event->id == CC_ID2))) || ((((Event->id - CC_ID1) % 40) == 0) || (((Event->id - CC_ID2) % 40) == 0))) { Event->rawE = (int) ((Event->rawE) - (int) 0x01000000); // cout << Event->rawE << endl; } else { Event->rawE = (int) ((Event->rawE) - (int) 0x01000000); #if (GRETINA) Event->rawE = -(int) (Event->rawE); #endif // cout << "segment: " << Event->id << endl; } } else { if ((((Event->id == CC_ID1) || (Event->id == CC_ID2))) || ((((Event->id - CC_ID1) % 40) == 0) || (((Event->id - CC_ID2) % 40) == 0))) { Event->rawE = (int) (Event->rawE); // cout << Event->rawE << endl; } else { Event->rawE = -(int) (Event->rawE); // cout << Event->id << "\t" << Event->rawE << endl; } } } else Event->rawE = INT_MAX; Event->ehi = (Event->rawE / 32); #endif // ACQ_10CH /* extract LED external time, per documentation */ Event->LEDts = (unsigned long long int) Event->hdr[2] + ((unsigned long long int) Event->hdr[3] << 16) + ((unsigned long long int) Event->hdr[4] << 32); #if(0) printf ("\n"); printf ("hdr[ 2]=0x%4.4x, ", Event->hdr[2]); printf ("hdr[ 3]=0x%4.4x ", Event->hdr[3]); printf ("hdr[ 4]=0x%4.4x ", Event->hdr[4]); #endif /* extract CFD external time, per documentation */ Event->CFDts = (unsigned long long int) Event->hdr[7] + ((unsigned long long int) Event->hdr[8] << 16) + ((unsigned long long int) Event->hdr[9] << 32); Event->CFDp1 = (int) Event->hdr[10] + ((int) Event->hdr[11] << 16); Event->CFDp2 = (int) Event->hdr[12] + ((int) Event->hdr[13] << 16); #if(0) printf ("hdr[ 7]=0x%4.4x ", Event->hdr[7]); printf ("hdr[ 8]=0x%4.4x ", Event->hdr[8]); printf ("hdr[ 9]=0x%4.4x, ", Event->hdr[9]); #endif /* trace, per documentation */ for (i = 0; i < Event->traceLen / 2; i++) { Event->trace[i] = (Event->trace[i] & 0x8000) ? (Event->trace[i] - INT_MAX) : Event->trace[i]; } // if (Event->trace[i] & 2048) // Event->trace[i] += 2048; /* extract various bits */ if (Event->hdr[6] & 0x8000) Event->pu = 1; else Event->pu = 0; Event->CFDvalid = ((Event->hdr[6] & 0x4000) >> 14); Event->ExtTrig = ((Event->hdr[6] & 0x2000) >> 13); Event->LEDsign = ((Event->hdr[6] & 0x1000) >> 12); /* done */ fflush (stdout); return (0); } /*----------------------------------------------------------------------------*/ int GTPrintEvent (GTEVENT * Event) { /* declarations */ int i; /* print event */ printf ("-------------------------------------------------------------------------\n"); printf ("ev %8i> ", CurEvNo); printf ("board= %8i; ", Event->board_id); printf ("chan= %8i; ", Event->chan_id); printf ("id= %8i; ", Event->id); printf ("e= %6i; ", Event->ehi); /* print a little of the trace */ printf ("tr: "); for (i = 0; i < 3; i++) printf ("%6i ", Event->trace[i]); printf ("..."); for (i = Event->traceLen / 2 - 3; i < Event->traceLen / 2; i++) printf ("%6i ", Event->trace[i]); printf (";\n"); /* time stamps */ printf ("__LEDts=0x%8.8x|0x%8.8x; ", (unsigned int) (Event->LEDts >> 32), (unsigned int) (Event->LEDts & 0xffffffff)); printf ("CFDts=0x%8.8x|%8.8x\n", (unsigned int) (Event->CFDts >> 32), (unsigned int) (Event->CFDts & 0xffffffff)); /* bits */ printf ("__pu=%i, ", Event->pu); printf ("CFDvalid=%i, ", Event->CFDvalid); printf ("ExtTrig=%i, ", Event->ExtTrig); printf ("LEDsign=%i\n", Event->LEDsign); /* pos and such */ printf ("__pos=%12i, len= %5i, tracelen=%6i\n", Event->pos, Event->len, Event->traceLen); /* done */ fflush (stdout); return (0); } /*----------------------------------------------------------------------------*/ int GTPrintHeader (GTEVENT * Event) { /* declarations */ int i, j, k; char txt[12][80] = { "len", "ID", "extTS H", "extTS L", "Ehi L", "extTS H", "CFD TS L", "Ehi H", "CFD TS H", "CFD TS M", "CFD point 1", "CFD point 2" }; /* print */ for (i = 0; i < 12; i++) { printf ("hdr[%2i]=%5i,0x%4.4x ", i, Event->hdr[i], Event->hdr[i]); j = 32768; printf ("|"); for (k = 0; k < 16; k++) { if ((Event->hdr[i] & j) == j) printf ("1"); else printf ("0"); j = j / 2; if ((k + 1) % 4 == 0) printf ("|"); }; printf (" \"%11s\"", txt[i]); if (i % 2) printf ("\n"); else printf (" | "); }; /* print the event id */ printf ("__Event->id=%i\n", Event->id); /* done */ fflush (stdout); return (0); } /*----------------------------------------------------------------------------*/ int showStatus (GTEVENT * Event) { printf ("read %i events; ", (CurEvNo - firstEvent)); printf ("Beta=%6.4f; ", (float) Beta); printf ("time since last update %i minutes\n", (int) tdmp); printf ("Event->pos=%10i, read %6i Mbytes; ", Event->pos, (Event->pos * 4) / 1024000); printf ("seen %i bad EOEs\n", nBadTestPat); printf ("CommandFileName=\"%s\"\n", CommandFileName); #include "UserStat.h" /* done */ fflush (stdout); return (0); }; /*----------------------------------------------------------------------------*/ /* allow for user functions here */ #include "UserFunctions.h" /*----------------------------------------------------------------------------*/ int GTacq (char *ChatFileName) { /* declarations */ int st = 0, eov = 0, nWords = 0, i1, i, j, nret; int ii, jj, maxid = -2000000000, minid = 2000000000; GTEVENT Event; char str[256], str1[256], str2[246]; FILE *fp; time_t t1, t2; Int_t ComPressLevel = NOTDEF; char *p; double rn, dtmpehi; TH1 *hhtemp; TList *wlist; TList *zlist; TIterator *hiterator; TMapFile *mfile; TFile *f1; TH1D *tmpTH1D = NULL; TH2F *tmpTH2F = NULL; TH1D *ehi[NGE]; TH1D *dt; TH2F *arrayhit; /* root spectra pointers */ TH1D *hitpat; /* prototypes */ int GSSort_read_chat (char *); int wr_spe (char *, int *, float *); /* allow user to declare variables here */ printf ("\n"); printf ("executing UserDeclare.h code\n"); printf ("\n"); #include "UserDeclare.h" /*-------*/ /* Hello */ /*-------*/ printf ("\n"); printf ("GSacq running on: "); time_stamp (); printf ("$Id: GTSort.cxx,v 1.2 2011/03/08 17:00:02 tl Exp $\n"); printf ("\n"); /*------------*/ /* initialize */ /*------------*/ printf ("\n"); printf ("initializing\n"); printf ("\n"); fflush (stdout); /*------------------*/ /* read chat script */ /*------------------*/ GSSort_read_chat (ChatFileName); printf ("checking proper input of chat file...\n"); /* check that we have proper input */ if (InputSrc == NOTDEF) { printf ("you must specify an input source\n"); exit (1); } else if (InputSrc == DISK) { /* attempt to open input file */ GTDataFile = open (GTSortInputFile, O_RDONLY, 0); if (GTDataFile == -1) { printf ("could not open\"%s\"; quit\n", GTSortInputFile); exit (1); } else printf ("input file \"%s\" is open\n", GTSortInputFile); } else if (InputSrc == NET) { printf (" input source is NET, using port %i\n", GTudpPort); fflush (stdout); } else { printf ("input source not recognized, quit\n"); exit (1); }; /*---------------------*/ /* other sanety checks */ /*---------------------*/ if (InputSrc == NOTDEF) { printf ("you must specify an input source!\n"); printf ("quitting...\n"); exit (1); }; if (UseShareMemFile && UseRootFile) { printf ("you cannot use shared memory and a root disk\n"); printf ("at the same time!\n"); exit (1); }; /* force user to declare intension with root file */ /* so I can't be blamed for any overwrites!! */ if (!UseShareMemFile) if ((p = strstr (ROOTFileOption, "UNDEFINED")) != NULL) { printf ("for root files you must specify either:\n"); printf ("\n"); printf (" rootfileoption RECREATE\n"); printf ("or\n"); printf (" rootfileoption UPDATE\n"); printf ("\n"); printf ("please modify your chat file and try again\n"); exit (-1); }; /*------------------------*/ /* execute user init code */ /*------------------------*/ // printf("\n"); // printf("executing UserInit.h code\n"); // printf("\n"); // #include "UserInit.h" /*-----------------------------------*/ /* if we are going to use the */ /* shared map file, create it! */ /* be careful about the map address! */ /*-----------------------------------*/ if (UseShareMemFile) { printf ("\n"); if (StartMapAddress != 0) { TMapFile::SetMapAddress ((Long_t) StartMapAddress); printf ("shared mem start address set to 0x%8.8x\n", StartMapAddress); } else printf ("will use system default for shared mem start address\n"); mfile = TMapFile::Create (ShareMemFile, "RECREATE", SizeShareMemFile, "GS shared mem"); if (mfile == NULL) { printf ("failed to create TMapFile\n"); exit (-1); }; printf ("shared memory [%s] created, size: %i bytes\n", ShareMemFile, SizeShareMemFile); fflush (stdout); mfile->Print (); printf ("\n"); }; NprintEvNo = 0; CurEvNo = 0; if (!UseShareMemFile) { DumpEvery = 2000000000; printf ("\n"); printf ("_since rootfile: setting `DumpEvery` to infinity..!\n"); printf ("\n"); }; /* delete any command file */ sprintf (str, "\\rm -f %s", CommandFileName); system (str); printf ("deleted %s\n", str); /*------------------------------------------*/ /* if we are using root file, then either */ /* read in old rootfile or create a nev one */ /*------------------------------------------*/ if (!UseShareMemFile) if (UpdateRootFile) { /* check here whether the old root file exists */ fp = fopen (ROOTFile, "r"); if (fp == NULL) { printf ("could not open old rootfile: %s\n", ROOTFile); printf ("the old rootfile must exist if you \n"); printf ("want to use the UPDATE option\n"); printf ("aborting...\n"); exit (0); }; fclose (fp); /* read in old root file */ f1 = NULL; f1 = new TFile (ROOTFile, "UPDATE"); printf ("read old root file <%s>\n", ROOTFile); if (!f1->IsOpen ()) { printf ("could not open file....\n\n"); exit (-1); }; printf ("base=<%s>\n", f1->GetPath ()); f1->Print (); } else { /* create the rootfile */ f1 = NULL; f1 = new TFile (ROOTFile, "RECREATE"); printf ("root file <%s>\n", ROOTFile); if (!f1->IsOpen ()) { printf ("could not open file....\n\n"); exit (-1); }; printf ("base=<%s>\n", f1->GetPath ()); f1->Print (); }; /*---------------------------*/ /* create basic root spectra */ /*---------------------------*/ printf ("\n"); sprintf (str1, "hitpat"); sprintf (str2, "Gretina hitpattern"); hitpat = mkTH1D (str1, str2, NGE, 0, NGE); printf ("%s;", str1); /* energy spectra */ for (i = 0; i <= NGE; i++) { sprintf (str1, "ehi%3.3i", i); sprintf (str2, "det %3.3i hi res", i); ehi[i] = mkTH1D (str1, str2, 2 * L14BITS + 1, -L14BITS, L14BITS); ehi[i]->SetXTitle ("Channel"); ehi[i]->SetYTitle ("Counts"); }; /* module hit pattern */ sprintf (str1, "arrayhit"); sprintf (str2, "GRETINA hit pattern (digitizer vs chan)", i); arrayhit = mkTH2F (str1, str2, 100, 0, 100, 11, 0, 10); arrayhit->SetXTitle ("Digitizer"); arrayhit->SetYTitle ("Channel"); printf ("\n"); printf ("executing UserInit.h code\n"); printf ("\n"); #include "UserInit.h" /* update shared mem with minimal info */ /* so it is not empty before the first update */ /* shared memory wellness checkpoint */ if (UseShareMemFile) { printf ("\n"); printf ("Note: if the command below fails,\n"); printf ("increase the shared memory size!\n"); printf ("\n"); printf ("updating empty shared mem file... "); fflush (stdout); mfile->Update (); printf ("Done!\n"); printf ("\n"); fflush (stdout); }; /*----------------------*/ /* setup signal catcher */ /*----------------------*/ #ifdef LINUX signal (SIGHUP, signal_catcher); #endif #ifdef SOLARIS sigset (SIGHUP, signal_catcher); #endif /*---------------*/ /* start sorting */ /*---------------*/ printf ("started sorting... "); if (InputSrc == DISK) printf ("from disk...\n"); else if (InputSrc == NET) printf ("from net...\n"); fflush (stdout); tdmplast = time (NULL); while (st >= 0 && (CurEvNo - firstEvent) < nEvents && eov == 0) { // // Initialize: should have been a member function of the event class // Event.len = INT_MAX; // // memset(Event.hdr,0,sizeof(Event.hdr)); // Event.traceLen = INT_MAX; // // memset(Event.trace,0,sizeof(Event.trace));; // /* position of event in data stream (in words) */ // Event.pos = INT_MAX; // /* processed data */ Event.id = (unsigned short int) INT_MAX; Event.board_id = 0; //INT_MAX; Event.chan_id = 0; //INT_MAX; Event.rawE = INT_MAX; // Event.LEDts = INT_MAX; // Event.CFDts = INT_MAX; Event.ehi = INT_MAX; // Event.pu = INT_MAX; // Event.CFDvalid = INT_MAX; // Event.ExtTrig = INT_MAX; // Event.LEDsign = INT_MAX; /*----------------*/ /* get next event */ /*----------------*/ if (InputSrc == DISK) { st = 1; while (st) { st = GTGetDiskEv (&Event); if (st == 5) break; } } else if (InputSrc == NET) st = GTGetNetEv (&Event); #if(0) printf ("st=%i\n", st); fflush (stdout); #endif /*----------------------------------------*/ /* allow user to manipulate raw data here */ /*----------------------------------------*/ #include "UserRawEv.h" if (st != 0 && InputSrc == DISK) { printf (" GTGetDiskEv returned %i\n", st); /* terminate sort */ eov = 1; /* note: */ /* we might want to wait and try GTGetDiskEv */ /* later to give the impresssion of interactivity */ /* here in some future version... */ } if (st == 0) { /*----------------------------*/ /* good event, now process it */ /*----------------------------*/ /* statistics */ CurEvNo++; NprintEvNo++; nWords += (HDRLENWORDS + LENEOVWORDS + Event.len * 2); /* intrepret the data */ GTEvdecompose (&Event); /* check IDs */ if (Event.id > maxid) maxid = Event.id; if (Event.id < minid) minid = Event.id; if (Event.id > 0 && Event.id < NGE) { #include "UserGoodEv.h" /* apply overall data */ /* multiplication to energy */ rn = (double) rand () / RAND_MAX - 0.5; dtmpehi = (double) Event.ehi + rn; dtmpehi *= HiResDataMultFactor; if (ehiDoGainCor) dtmpehi = dtmpehi * ehiGeGain[Event.id] + ehiGeOffset[Event.id]; /* finally integer energy again */ if ( /*dtmpehi >= 0 && */ dtmpehi < L14BITS) Event.ehi = (int) (dtmpehi + 0.5); else Event.ehi = INT_MAX; /* debug print some events */ if (NprintEvNo <= NumToPrint) { GTPrintEvent (&Event); GTPrintHeader (&Event); }; #include "UserPreCond.h" /*------------------------------------------*/ /* bin basic things if the ID is in range.. */ /* Note: we should pileup reject here, but.. */ /* if we do we loose all good events.. */ /* which makes no sense!?............. */ /*------------------------------------------*/ //Event.pu = 0; /* force! */ if (Event.pu == 0 || Event.pu == 1) if (Event.id >= 0 && Event.id < NGE) { /* grand hitpattern */ hitpat->Fill ((double) Event.id, 1); /* total array hit pattern */ if (Event.board_id > 0 && Event.board_id <= 100) if ( /*Event.chan_id >= 0 && */ Event.chan_id < 10) arrayhit->Fill (Event.board_id, Event.chan_id, 1); /* energies */ // printf("Event.id: %i; ", Event.id); // printf("Event.ehi: %f\n", Event.ehi); fflush (stdout); if (Event.ehi < L14BITS && Event.ehi > 0) ehi[Event.id]->Fill ((double) Event.ehi, 1); }; /*-------------------------*/ /* execute user event code */ /*-------------------------*/ #include "UserEv.h" }; }; /*---------------------*/ /* house keeping...... */ /* done every so often */ /*---------------------*/ if (CurEvNo % 100 == 0) { /* calc time since last dump */ tdmp = time (NULL); tdmp -= tdmplast; tdmp /= 60; /* now minutes */ }; /*-----------------------------------------------------------*/ /* dump all spectra on signal or dump every DumpEvery events */ /* or respond to 'interactive' command...................... */ /*-----------------------------------------------------------*/ if (WeWereSignalled || (int) tdmp >= DumpEvery) { /* disarm signal */ WeWereSignalled = FALSE; /* check for command file */ fp = fopen (CommandFileName, "r"); if (fp != NULL) { printf ("found command file: %s\n", CommandFileName); fgets (str, STRLEN, fp); printf ("with command: %s\n", str); if ((p = strstr (str, "dumpevery")) != NULL) { sscanf (str, "%s %i", str1, &DumpEvery); printf ("will dump to output file every %i minutes\n", DumpEvery); fflush (stdout); } else if ((p = strstr (str, "printevents")) != NULL) { /* reset print event counter */ nret = sscanf (str, "%s %i", str1, &i1); if (nret == 2) NumToPrint = i1; printf ("will print %i events\n", NumToPrint); NprintEvNo = 0; } else if ((p = strstr (str, "status")) != NULL) { showStatus (&Event); } else if ((p = strstr (str, "stopsort")) != NULL) { /* simulate end of event to stop sort */ eov = 1; } else if ((p = strstr (str, "zapall")) != NULL) { /* zap spectra */ if (UseShareMemFile) { zlist = mfile->GetDirectory ()->GetList (); hiterator = zlist->MakeIterator (); while (hhtemp = (TH1 *) hiterator->Next ()) { hhtemp->Reset (); } printf ("all spectra were zapped @ "); time_stamp (); fflush (stdout); /* update */ printf ("updating shared mem... "); UPDSSHMEM; } else { /* do nothing */ } } else if ((p = strstr (str, "zap")) != NULL) { /* extract spectrum name */ sscanf (str, "%s %s", str1, spname); hhtemp = (TH1D *) gROOT->FindObject (spname); if (UseShareMemFile) { hhtemp = (TH1 *) mfile->Remove (spname); if (hhtemp != NULL) { hhtemp->Print (); hhtemp->Reset (); mfile->Add (hhtemp, spname); mfile->Update (hhtemp); } printf ("spectrum %s zapped @ ", spname); time_stamp (); fflush (stdout); /* update */ } else { /* do nothing */ }; } else printf ("command not understood\n"); /* delete command file */ fclose (fp); sprintf (str, "\\rm %s", CommandFileName); system (str); printf ("%s\n", str); } else { printf ("\"%s\" was not found\n", CommandFileName); /* update sh mem or writeout root file */ printf ("time since last dump: %i minute(s)\n", (int) tdmp); tdmp = 0; if (!UseShareMemFile) { printf ("*---------------------------------\n"); printf ("* you cannot update a disk file. \n"); printf (" you must wait for sort to finish\n"); printf (" or stop the sort! Ignoring you...\n"); printf ("*---------------------------------\n"); } else { printf ("updating shared mem... "); UPDSSHMEM showStatus (&Event); }; tdmplast = time (NULL); }; printf ("continuing the sort...\n"); fflush (stdout); }; }; /*-----------------------*/ /* we are done sorting!! */ /* save all ROOT spectra */ /*-----------------------*/ printf ("\n"); printf ("Sorting is done\n"); printf ("attempting to save root or map file\n"); printf ("\n"); fflush (stdout); /* if we were using shared memory */ if (UseShareMemFile) { UPDSSHMEM mfile->Print (); printf ("\n"); mfile->ls (); printf ("\n"); }; /* if we were using rootfile */ if (!UseShareMemFile) { printf ("attempting to close root file...\n"); fflush (stdout); printf ("f1->Write();\n"); fflush (stdout); f1->Write (); printf ("f1->Print();\n"); fflush (stdout); f1->Print (); printf ("f1->Close();\n"); fflush (stdout); f1->Close (); printf ("done saving rootfile \"%s\n\n", ROOTFile); fflush (stdout); } printf ("\n"); /*-------------------------*/ /* print simple statistics */ /*-------------------------*/ showStatus (&Event); #include "UserExit.h" /* done */ printf ("minid/maxid seen: %i/%i\n", minid, maxid); printf ("\n ** GTsort done!\n\n"); return (0); } /*-------------------------------------------------*/ /*----------------------------------------------------*/ TH1D * mkTH1D (char *str1, char *str2, int nn, double lo, double hi) { TH1D *tmppt; if (!UpdateRootFile) { tmppt = new TH1D (str1, str2, nn, lo, hi); printf ("Created Object \"%s\", %p\n", str1, tmppt); } else { tmppt = (TH1D *) gROOT->FindObject (str1); printf ("Found Object \"%s\", %p\n", str1, tmppt); } return (tmppt); } /*----------------------------------------------------*/ TH2F * mkTH2F (char *str1, char *str2, int nn1, double lo1, double hi1, int nn2, double lo2, double hi2) { TH2F *tmppt; if (!UpdateRootFile) { tmppt = new TH2F (str1, str2, nn1, lo1, hi1, nn2, lo2, hi2); printf ("Created Object \"%s\", %p\n", str1, tmppt); } else { tmppt = (TH2F *) gROOT->FindObject (str1); printf ("Found Object \"%s\", %p\n", str1, tmppt); }; return (tmppt); } /*--------------------------------------------------------*/ void CheckNoArgs (int required, int actual, char *str) { if (required < actual) { printf ("argument problem with chat option\n"); printf ("--> %s\n", str); printf ("required # arguments: %i\n", required - 1); printf ("actual # arguments: %i\n", actual - 1); printf ("Please fix and try again, quitting...\n"); exit (1); }; }