EventMixer 1.0
An event mixer for mixing events generated by PIBIDS.
mixer.cc
Go to the documentation of this file.
00001 #include "mixer.hh"
00002 #include <iostream>
00003 
00004 bool compare_eventhits (EventHit* first, EventHit* second)
00005 {
00006   //A check for null would be appropriate here, but what would you do when you found null?
00007   return first->time<=second->time;
00008 }
00009 
00010 void verify_input(int argc, char **argv)
00011 {
00012    if (argc < 6 || argc % 2 == 1)
00013     {
00014       fprintf (stderr,"Usage: %s timeRandmax nTubes <outfile.txt> n*[<infile_n.txt> probability_to_selectfrom_this] \n",argv[0]);
00015       exit(1);
00016     }
00017 }
00018 
00019 void setup_eventsToMix(int argc, char **argv)
00020 {
00021   double cumSum = 0; //For norming the probabilities.
00022   for(int i = 2; i<(argc/2); i++)
00023     {
00024       eventsToMix.push_back(make_pair(new FileEventStream(argv[2*i], NumberOfTubes),atof(argv[2*i+1])));
00025       cumSum+=eventsToMix.back().second;
00026     }
00027   //now norm the probabilities.
00028   for(vector<pair<FileEventStream*, double> >::iterator it = eventsToMix.begin(); it!=eventsToMix.end(); it++)
00029     {
00030       it->second/=cumSum;
00031     }
00032 }
00033 
00034 FILE * setup_outputFile(char* filename)
00035 {
00036   FILE * outFile = fopen(filename, "w");
00037   if(outFile==NULL)
00038     {
00039       fprintf (stderr,"Could not open output file %s, exiting.", filename);
00040       exit(1);
00041     }
00042   return outFile;
00043 }
00044 
00045 void print_to_outfile(EventHit * it, FILE * outFile)
00046 {
00047   fprintf(outFile,"%d %d %d %.15e ", it->eventno, it->Z, it->A, it->time);
00048  
00049   if(it->UFSP < DOUBLE_EPS)
00050     fprintf(outFile,"0 ");
00051   else
00052     fprintf(outFile,"%.15e ",it->UFSP);
00053   
00054   if(it->LFSP<DOUBLE_EPS)
00055     fprintf(outFile,"0 ");
00056   else
00057     fprintf(outFile,"%.15e ",it->LFSP);
00058   
00059   if(it->BSP<DOUBLE_EPS)
00060     fprintf(outFile,"0 ");
00061   else
00062     fprintf(outFile,"%.15e ",it->BSP);
00063   
00064   if(it->Ge<DOUBLE_EPS)
00065     fprintf(outFile,"0");
00066   else
00067     fprintf(outFile,"%.15e",it->Ge);
00068   
00069   for(int j = 0; j<NumberOfTubes; j++)
00070     {
00071       if(it->DET[j]<DOUBLE_EPS)
00072         fprintf(outFile," 0");
00073       else
00074         fprintf(outFile," %.15e", it->DET[j]);
00075     }
00076   fprintf(outFile,"\n");
00077 }
00078 
00079 
00080 void do_mixing(double randMax, FILE * outFile)
00081 {
00082   double currentTime = 0;
00083   list<EventHit*> buffer;
00084   bool endLoop = false;
00085   long long nPrinted = 0;
00086   do
00087     {
00088       //Pick a new event with weighted probabilities for the different files.
00089       currentTime+=randMax*((double)rand()/(double)RAND_MAX);
00090       double u01 = ((double)rand()/(double)RAND_MAX);
00091       for(vector<pair<FileEventStream*, double> >::iterator it = eventsToMix.begin(); it!=eventsToMix.end(); it++)
00092         {
00093           u01-=it->second;
00094           if(u01<=0)
00095             {
00096               if(it->first->hasMoreEvents())
00097                 {
00098                   Event * tempEvent = it->first->getNextEvent();
00099                   for(vector<EventHit*>::iterator it = tempEvent->hit.begin(); it!=tempEvent->hit.end(); it++)
00100                     {
00101                       (*it)->time+=currentTime;
00102                       buffer.push_back(*it);
00103                     }
00104                 }
00105               else
00106                 {
00107                   endLoop=true;
00108                 }
00109               break;
00110             }
00111         }
00112 
00113       list<EventHit*> toPrint;
00114 
00115       for(list<EventHit*>::iterator it = buffer.begin(); it!=buffer.end(); it++)
00116         {
00117           if((*it)->time<currentTime || endLoop)
00118             {
00119               toPrint.push_back(*it);
00120               it = buffer.erase(it);
00121               --it;
00122             }
00123         }
00124       
00125       toPrint.sort(compare_eventhits);
00126 
00127       for(list<EventHit*>::iterator it = toPrint.begin(); it!=toPrint.end(); it++)
00128         {
00129           print_to_outfile(*it, outFile);
00130           ++nPrinted;
00131           delete *it;
00132         }
00133 
00134       toPrint.clear();
00135       
00136 
00137     }while(!endLoop);
00138 }
00139 
00140 
00141 
00142 int main(int argc, char **argv)
00143 {
00144   verify_input(argc, argv);
00145   srand ( time(NULL) );
00146   
00147   double randMax = atof(argv[1]);
00148   NumberOfTubes = atoi(argv[2]);
00149   setup_eventsToMix(argc, argv);
00150   FILE * outFile = setup_outputFile(argv[3]);
00151 
00152   //Everything should be set up for the actual mixing now.
00153   do_mixing(randMax, outFile);
00154   
00155   //Clean up.
00156   fclose(outFile);
00157   return 0;
00158 }
 All Classes Files Functions Variables Defines

Back to the main page of the Precalibrated Ion Beam Identification Detector project

Created by Rikard Lundmark