Example #1
0
int LandSimulation(int landfg,char* strInputFilePath,CProgressWnd* pwndProgress)
{
	CString ReportFilePath(strInputFilePath);
	ReportFilePath.TrimRight("inp");
	CString OutputFilePath = ReportFilePath;
	ReportFilePath += "rpt";
	OutputFilePath += "out";
	char* strReportFilePath = ReportFilePath.GetBuffer(ReportFilePath.GetLength());
	char* strOutputFilePath = OutputFilePath.GetBuffer(OutputFilePath.GetLength());
	
	// initialize progress bar
	pwndProgress->SetRange(0, 100);			 
	pwndProgress->SetText("");
	CString strMsg, strForDdg, strE;

	COleDateTime time_i;		// time at the beginning of the simulation
	COleDateTime time_f;		// time at the end of the simulation
	COleDateTimeSpan time_dif;	// simulation run time
	SYSTEMTIME tm;				// system time
	GetLocalTime(&tm);
	time_i = COleDateTime(tm);

	long newHour, oldHour = 0;
	DateTime elapsedTime = 0.0;

	// --- open the files & read input data
	ErrorCode = 0;
	swmm_open(strInputFilePath,strReportFilePath,strOutputFilePath);

	// --- run the simulation if input data OK
	if ( !ErrorCode )
	{
		// --- initialize values
		swmm_start(TRUE);

		// --- execute each time step until elapsed time is re-set to 0
		if ( !ErrorCode )
		{
			int y, m, d;
			datetime_decodeDate(StartDateTime, &y, &m, &d);
			COleDateTime tStart(y,m,d,0,0,0);
			datetime_decodeDate(EndDateTime, &y, &m, &d);
			COleDateTime tEnd(y,m,d,0,0,0);
			COleDateTimeSpan span0 = tEnd - tStart;

			do
			{
				swmm_step(&elapsedTime);
				newHour = elapsedTime * 24.0;

				COleDateTimeSpan span = COleDateTimeSpan(0,newHour,0,0);
				COleDateTime tCurrent = tStart + span;

				int nSYear = tCurrent.GetYear();
				int nSMonth = tCurrent.GetMonth();
				int nSDay = tCurrent.GetDay();
				int nSHour = tCurrent.GetHour();

				GetLocalTime(&tm);
				time_f = COleDateTime(tm);
				time_dif = time_f - time_i;

				int dd_elap = int(time_dif.GetDays());
				int hh_elap = int(time_dif.GetHours());
				int mm_elap = int(time_dif.GetMinutes());
				int ss_elap = int(time_dif.GetSeconds());

				if ( newHour > oldHour )
				{
					oldHour = newHour;

					if (landfg == 0)
						strMsg.Format("Land Simulation:\t Pre-Development Scenario\n");
					else
						strMsg.Format("Land Simulation:\t Post-Development Scenario\n");
					strForDdg = strMsg;

					strMsg.Format("Calculating:\t %02d-%02d-%04d\n", nSMonth, nSDay, nSYear);
					strForDdg += strMsg;
					
					strE.Format("\nTime Elapsed:\t %02d:%02d:%02d:%02d\n", dd_elap, hh_elap, mm_elap, ss_elap);
					strForDdg += strE;

					double lfPart = span.GetTotalSeconds();
					double lfAll  = span0.GetTotalSeconds();
					double lfPerc = lfPart/lfAll;

					if(pwndProgress->GetSafeHwnd() != NULL && nSHour == 0)
					{
						pwndProgress->SetText(strForDdg);
						pwndProgress->SetPos((int)(lfPerc*100));
						pwndProgress->PeekAndPump();
					}

					if (pwndProgress->Cancelled())
					{
						pwndProgress->DestroyWindow();
						AfxMessageBox("BMP simulation is cancelled");
						break;
					}
				}
			} while ( elapsedTime > 0.0 && !ErrorCode );
		}

		// --- clean up
		swmm_end();
	}

	// --- report results
	swmm_report();

	// --- close the system
	swmm_close();

	//return ErrorCode;
	return ErrorCode;
}
Example #2
0
// Main Program
int main( int nArgs, char **args ) {

  // Read command line parameters
  if ( nArgs < 3 ) {
    std::cout << "Please specify output filename and run time (seconds)." << std::endl;
    return 1;
  }
  std::string outFileName(args[1]);
  int runTime(30);
  { std::stringstream tmp; tmp << std::string(args[2]); tmp >> runTime; }
  std::cout << "LED triggered run for " << runTime << " seconds: " << outFileName << std::endl;

  // Create the ROOT Application (to draw canvases)
  TApplication *theApp = new TApplication("LEDRun",&nArgs,args);
  //gStyle->SetPalette(1);

  // Configure the Argonne Board
  int err(0);
  uint nSamples = 600; //1024; // max = 2046
  uint preTrigSamples = 50;
  err = ConfigArgoBoard_ExtTrig( nSamples, preTrigSamples );
  if ( err != 0 ) {
    std::cout << "Failed to configure ANL Digitizer." << std::endl;
    return err;
  }

  // Get ANL Digitizer configuration info
  uint ANL_nSamples(0); DeviceRead(lbneReg.readout_window[0],&ANL_nSamples);
  uint ANL_preTrigSize(0); DeviceRead(lbneReg.readout_pretrigger[0],&ANL_preTrigSize);
  uint ANL_eventSize = sizeof(Event_Header) + 2*nSamples; // in bytes
  uint m1Size(0); DeviceRead(lbneReg.m1_window[0],&m1Size);
  uint m2Size(0); DeviceRead(lbneReg.m2_window[0],&m2Size);
  uint pWindow(0); DeviceRead(lbneReg.p_window[0],&pWindow);
  uint kWindow(0); DeviceRead(lbneReg.k_window[0],&kWindow);
  uint iWindow(0); DeviceRead(lbneReg.i_window[0],&iWindow);
  uint dWindow(0); DeviceRead(lbneReg.d_window[0],&dWindow);

  // Output File
  TFile *outFile = new TFile(outFileName.c_str(),"RECREATE");
  TDirectory *waveDir = outFile->mkdir("waveforms");
  std::vector<TH1D*> waveformExamples;

  // Set up output ROOT tree for ANL Digitizer
  TTree *ANLTree = new TTree("ANLTree","ANLTree");
  Event_Packet ArPacket; Event ArEvent;
  ANLTree->Branch("channelID",&(ArEvent.channelID),"channelID/s");
  ANLTree->Branch("syncDelay",&(ArEvent.syncDelay),"syncDelay/i");
  ANLTree->Branch("syncCount",&(ArEvent.syncCount),"syncCount/i");
  ANLTree->Branch("timestamp",&(ArEvent.intTimestamp),"timestamp/l");
  ANLTree->Branch("peakSum",&(ArEvent.peakSum),"peakSum/I");
  ANLTree->Branch("peakTime",&(ArEvent.peakTime),"peakTime/C");
  ANLTree->Branch("prerise",&(ArEvent.prerise),"prerise/i");
  ANLTree->Branch("integratedSum",&(ArEvent.integratedSum),"integratedSum/i");
  ANLTree->Branch("baseline",&(ArEvent.baseline),"baseline/s");
  ANLTree->Branch("cfdPoint",&(ArEvent.cfdPoint),"cfdPoint[4]/S");
  ANLTree->Branch("nSamples",&(ArEvent.waveformWords),"nSamples/s");
  std::stringstream waveformDescr; waveformDescr << "waveform[" << nSamples << "]/s";
  ANLTree->Branch("waveform",&(ArEvent.waveform),waveformDescr.str().c_str());

  // Set up configuration ROOT tree
  TTree *configTree = new TTree("configTree","configTree");
  configTree->Branch("runTime",&runTime,"runTime/I");
  configTree->Branch("ANL_nSamples",&ANL_nSamples,"ANL_nSamples/i");
  configTree->Branch("ANL_preTrigSize",&ANL_preTrigSize,"ANL_preTrigSize/i");
  configTree->Branch("m1Size",&m1Size,"m1Size/i");
  configTree->Branch("m2Size",&m2Size,"m2Size/i");
  configTree->Branch("pWindow",&pWindow,"pWindow/i");
  configTree->Branch("kWindow",&kWindow,"kWindow/i");
  configTree->Branch("iWindow",&iWindow,"iWindow/i");
  configTree->Branch("dWindow",&dWindow,"dWindow/i");
  configTree->Fill();
  configTree->Write();

  // Monitoring histograms
  TH1D *ANL_pulseAmp[32];
  for ( int chan = 0; chan < 12; ++chan ) {
    std::stringstream histName; histName << "ANL_pulseAmp_CH" << chan;
    std::stringstream histTitle; histTitle << "Pulse Amplitudes, ANL Channel " << chan << ";ADC Counts";
    ANL_pulseAmp[chan] = new TH1D(histName.str().c_str(),histTitle.str().c_str(),3000,0,3000);
  }    

  std::cout << "Starting acquisition." << std::endl;
  // Enable boards
  err = DeviceTimeout(500);
  err = DeviceStart();

  // Loop for specified time
  int totArEvts(0);
  time_t tStart(0), tEnd(0);
  time(&tStart); time(&tEnd);
  while ( tEnd - tStart < runTime ) {

    /*** ANL Digitizer Readout ***/
    uint dataSize; err = DeviceQueueStatus(&dataSize); // How many full events have been collected?
    if ( err != 0 ) { std::cout << "Failed device queue status check." << std::endl; break; }
    uint ANL_nEvts = uint(dataSize) / ANL_eventSize;
    //if ( ANL_nEvts > 0 ) std::cout << "Received " << ANL_nEvts << " events on the Argonne Digitizer (" << dataSize << " bytes)." << std::endl;
    for ( uint evt = 0; evt < ANL_nEvts; ++evt ) {
      totArEvts++;
      uint dataReceived = 0;
      err = DeviceReceive(&ArPacket,&dataReceived);
      if ( err != 0 ) { std::cout << "Failed device receive." << std::endl; break; }
      err = LBNE_EventUnpack(&ArPacket,&ArEvent);
      if ( err != 0 ) {	std::cout << "Failed to unpack data." << std::endl; break; }
      ANLTree->Fill();
      ANL_pulseAmp[ArEvent.channelID]->Fill(ArEvent.prerise/double(kWindow)-ArEvent.peakSum/double(m1Size));
      /*
      std::stringstream waveHistName; waveHistName << "waveform_" << totArEvts;
      TH1D *hist = new TH1D(waveHistName.str().c_str(),waveHistName.str().c_str(),nSamples,0,nSamples);
      for ( uint samp = 0; samp < nSamples; ++samp ) {
	hist->SetBinContent(samp+1,ArEvent.waveform[samp] & 0x3FFF); // 0x3FFF drops time marks on waveform, not necessary in next firmware upgrade
      }
      waveformExamples.push_back(hist);
      */
    }

    //std::cout << tEnd-tStart << std::endl;
    time(&tEnd);
  }

  //// Finalize

  // Finalize Argonne Digitizer
  err = DeviceStopReset();
  if ( err != 0 ) {
    std::cout << "Failed to stop and reset board 0. ErrorCode " << err << std::endl;
    return 1;
  }
  err = DeviceDisconnect(commUSB);

  // Write and close output file
  ANLTree->Write();

  for ( int chan = 0; chan < 12; ++chan ) ANL_pulseAmp[chan]->Write();

  waveDir->cd();
  for ( uint i = 0; i < waveformExamples.size(); ++i ) {
    waveformExamples[i]->Write();
  }
  outFile->cd();

  outFile->Close();

  // Done!
  std::cout << "Done!  Collected " << totArEvts/12 << " events" << std::endl;

  return 0;
}
Example #3
0
void AlexSim::writeArrivalTimes(const QString filename) const
{
    QFile file(filename);
    if(!file.open(QIODevice::WriteOnly | QIODevice::Text)) QMessageBox::information(0, "error", file.errorString());
    QTextStream out(&file); out.setRealNumberPrecision(11);
    out <<"# Simulation data. All times in s. Parameters are:\n";
    out <<"# rateDonor="<<rateDemDex<<"\trateAcceptor="<<rateAemAex<<"\trateBackground="<<rateBackground<<"\tburstDuration="<<burstDuration<<"\tsigma="<<sigma<<"\ttStart="<<tStart<<"\ttEnd="<<tEnd()<<"\n";
    out <<"#  time\tchannel\n";

    long n=photons.size();
    for(long ii=0;ii<n;ii++) {
        out <<photons.value(ii).time<<"\t"<<QString::number(photons.value(ii).flags,2)<<"\n";
    }
    file.close();

    qDebug()<<n<<"photon times written to file "<<filename;

}
Example #4
0
void AlexSim::writeHist(const QString filename) const
{
    long ii;
    long nbins=(long)((t-tStart)/burstDuration*10);
    if(nbins==0||photons.size()==0) qWarning()<<"writeHist: no photon records ";
    gsl_histogram * histDonor = gsl_histogram_alloc (nbins);
    gsl_histogram_set_ranges_uniform (histDonor, tStart, t); // change t to tEnd if the latter is implemented
    gsl_histogram * histAcceptor = gsl_histogram_alloc (nbins);
    gsl_histogram_set_ranges_uniform (histAcceptor, tStart, t); // change t to tEnd if the latter is implemented
    for (ii=0;ii<photons.size();ii++) {
#ifdef PHOTONMACRO
        if(isChannel(photons.at(ii),DonorEm)) gsl_histogram_increment (histDonor, photons.at(ii).time);
#else
        if(photons.at(ii).isChannel(DonorEm)) gsl_histogram_increment (histDonor, photons.at(ii).time);
#endif
        else gsl_histogram_increment (histAcceptor, photons.at(ii).time);
    }

    QFile file(filename);
    if(!file.open(QIODevice::WriteOnly | QIODevice::Text)) QMessageBox::warning(0, "error", file.errorString());
    QTextStream out(&file);
    out <<"# Simulation data. photon count over time. parameters are:\n";
    out <<"# rateDonor="<<rateDemDex<<"\trateAcceptor="<<rateAemAex<<"\trateBackground="<<rateBackground<<"\tburstDuration="<<burstDuration<<"\tsigma="<<sigma<<"\ttStart="<<tStart<<"\ttEnd="<<tEnd()<<"\n";
    out.setRealNumberPrecision(11);
    out <<"#  time in ms \tdonor channel \tacceptor channel\n";

    for(ii=0;ii<histDonor->n;ii++) {
        out << histDonor->range[ii]*1e3 << "\t" << gsl_histogram_get(histDonor,ii) << "\t" << gsl_histogram_get(histAcceptor,ii) << "\n";
    }
    file.close();
    gsl_histogram_free (histDonor);
    gsl_histogram_free (histAcceptor);

    qDebug()<<nbins<<"histogram entries written to file "<<filename;
}
Example #5
0
void AlexSim::simBurstDuration(int nBursts,bool writeToFile)
{
    // simulate many photon bursts and write their sampled duration and burst size to file --> linear relationship?

    simInit();
//    photonArrivalTimeDonor.reserve((int)(nBursts*burstDuration*rateDemDex));
//    photonArrivalTimeAcceptor.reserve((int)(nBursts*burstDuration*rateAemAex));

    if(writeToFile) {
        QFile file("./AlexSimBurstDuration.txt");
        file.open(QIODevice::WriteOnly | QIODevice::Text);
        QTextStream out(&file);
        out <<"# Simulation data. All times in ms.Parameters are:\n";
        out <<"# rateDonor="<<rateDemDex<<"\trateAcceptor="<<rateAemAex<<"/trateBackground="<<rateBackground<<"\tburstDuration="<<burstDuration<<"\tvariance="<<burstDurationVar<<"\ttStart="<<tStart<<"\ttEnd="<<tEnd()<<"\n";
        out.setRealNumberPrecision(11);
        out <<"#  time \tburst duration\t#photons\n";

        for(int i=0;i<nBursts;i++) {
            burstCount(out);
            background(burstDuration);
        }

        file.close();
        writeArrivalTimes("./AlexSimPhotonTimes.txt");
    } else {
        for(int i=0;i<nBursts;i++) {
            burst(false);
            background(burstDuration);
        }

    }

}
Example #6
0
void TTool::updateToolsPropertiesTranslation() {
  ToolTable::iterator tt, tEnd(toolTable->end());
  for (tt = toolTable->begin(); tt != tEnd; ++tt)
    tt->second->updateTranslation();
}