コード例 #1
0
void MainWindow::setupColorMapTest(QCustomPlot *customPlot)
{
	customPlot->legend->setVisible(true);
	presetInteractive(customPlot);
	QCPColorMap *colorMap = new QCPColorMap(customPlot->xAxis, customPlot->yAxis);
	customPlot->addPlottable(colorMap);
	colorMap->setName("Color Map");
	customPlot->addLayer("maplayer", customPlot->layer("grid"), QCustomPlot::limBelow);
	colorMap->setLayer("maplayer");

	int nx = 400;
	int ny = 400;
	colorMap->data()->setSize(nx, ny);
	colorMap->data()->setRange(QCPRange(0, 10), QCPRange(0, 10));
	colorMap->setInterpolate(true);
	colorMap->setTightBoundary(false);
	for (int x=0; x<nx; ++x)
	{
		for (int y=0; y<ny; ++y)
		{
			colorMap->data()->setCell(x, y, qExp(-qSqrt((x-310)*(x-310)+(y-260)*(y-260))/200.0)+
				qExp(-qSqrt((x-200)*(x-200)+(y-290)*(y-290))/80.0)-qExp(-qSqrt((x-180)*(x-180)+(y-140)*(y-140))/200.0));
		}
	}

	/* manual test of coordinate to cell transformations (and vice versa):
	connect(customPlot, SIGNAL(mouseMove(QMouseEvent*)), this, SLOT(colorMapMouseMove(QMouseEvent*)));
	colorMap->data()->setRange(QCPRange(0, 1), QCPRange(0, 1));
	colorMap->data()->setSize(2,2);
	colorMap->data()->setCell(0, 0, 0);
	colorMap->data()->setCell(0, 1, 0);
	colorMap->data()->setCell(1, 0, 2);
	colorMap->data()->setCell(1, 1, 4);
	*/

	//customPlot->xAxis->setRangeReversed(true);
	//customPlot->yAxis->setRangeReversed(true);

	colorMap->setInterpolate(false);

	QCPColorScale *colorScale = new QCPColorScale(customPlot);
	customPlot->plotLayout()->addElement(0, 1, colorScale);
	colorMap->setColorScale(colorScale);
	colorScale->setLabel("test");

	QCPMarginGroup *group = new QCPMarginGroup(customPlot);
	colorScale->setMarginGroup(QCP::msTop|QCP::msBottom, group);
	customPlot->axisRect()->setMarginGroup(QCP::msTop|QCP::msBottom, group);

	QCPColorGradient gradient = colorMap->gradient();
	gradient.loadPreset(QCPColorGradient::gpJet);
	gradient.setPeriodic(false);
	colorMap->setGradient(gradient);
	colorMap->rescaleDataRange(true);

	connect(customPlot, SIGNAL(beforeReplot()), colorMap, SLOT(updateLegendIcon()));
	customPlot->rescaleAxes();
	customPlot->replot();
}
コード例 #2
0
ファイル: yarrgui.cpp プロジェクト: Yarr/Yarr
void YarrGui::on_exportPlotCSVButton_clicked(){
    if(ui->scanPlots_tabWidget->count() == 0){return;}
    if(ui->plotTree->currentItem() == nullptr) {
        std::cerr << "Please select plot. Returning... " << std::endl;
        return;
    }
    if(ui->plotTree->currentItem()->childCount() > 0){
        std::cerr << "Please select plot. Returning... " << std::endl;
        return;
    }

    QWidget * toCast = ui->scanPlots_tabWidget->currentWidget();
    QCustomPlot * myPlot = dynamic_cast<QCustomPlot*>(toCast);
    if(myPlot == nullptr){
        std::cerr << "Severe cast error. Returning... " << std::endl;
        return;
    }

    QCPColorMap * myColorMap = dynamic_cast<QCPColorMap*>(myPlot->plottable());
    if(myColorMap == nullptr){
        std::cout << "Severe cast error. Aborting... " << std::endl;
        return;
    }

/*    QString myFileName = ui->plotTree->currentItem()->text(0);

    struct tm * timeinfo;
    time_t rawtime;
    time(&rawtime);
    timeinfo = localtime(&rawtime);

    myFileName = myFileName
               + QString::number(1900+(timeinfo->tm_year)) + '_'
               + (timeinfo->tm_mon > 8 ? QString::number(1+(timeinfo->tm_mon)) : ('0' + QString::number(1+(timeinfo->tm_mon)))) + '_'
               + QString::number((timeinfo->tm_mday)) + '_'
               + QString::number((timeinfo->tm_hour)) + '_'
               + QString::number((timeinfo->tm_min)) + '_'
               + QString::number((timeinfo->tm_sec)) + ".csv";
*/

    QString myFileName = QFileDialog::getSaveFileName(this,
                                                      "Save plot as CSV",
                                                      "./",
                                                      "Comma-Separated Values(*.csv)");
    if(myFileName==""){return;}

    std::ofstream myCSVOutput(myFileName.toStdString());

    for(int xCoord = 0; xCoord<80; xCoord++){
        for(int yCoord = 0; yCoord<336; yCoord++) {
            myCSVOutput << xCoord << ",\t" << yCoord << ",\t" << myColorMap->data()->cell(xCoord, yCoord) << std::endl;
        }
    }

    myCSVOutput.close();
    std::cout << "Saved current plot to \"" << myFileName.toStdString() << '"' << std::endl;

    return;
}
コード例 #3
0
//all the stuff we actually want
QCPColorMap* ConvertTH2(TH2* plot,QCPAxis *xAxis, QCPAxis *yAxis){
  //this is how we get the 2d histogram in Qt
  QCPColorMap* ret = new QCPColorMap(xAxis,yAxis);
  //get the external info we need from the plot
  //rebin the plot by 100 bins to see if we get shit correc then.
  //TH2* dum_rebin = plot->RebinY(100,"dum_rebin");
  int NBinsX = plot->GetNbinsX();
  int NBinsY = plot->GetNbinsY();
  double xmin = plot->GetXaxis()->GetXmin();
  double xmax = plot->GetXaxis()->GetXmax();
  double ymin = plot->GetYaxis()->GetXmin();//terrible method, but this should work
  double ymax = plot->GetYaxis()->GetXmax();//terrible

  /*
  int NBinsX = dum_rebin->GetNbinsX();
  int NBinsY = dum_rebin->GetNbinsY();
  double xmin = dum_rebin->GetXaxis()->GetXmin();
  double xmax = dum_rebin->GetXaxis()->GetXmax();
  double ymin = dum_rebin->GetYaxis()->GetXmin();//terrible method, but this should work
  double ymax = dum_rebin->GetYaxis()->GetXmax();//terrible
  */
  //std::cout<<"double checking th2, xmin ="<<xmin<<", xmax = "<<xmax<<", ymin="<<ymin<<", ymax="<<ymax<<std::endl;
  
  ret->data()->setSize(NBinsX,NBinsY);
  ret->data()->setRange(QCPRange(xmin,xmax),QCPRange(ymin,ymax));
  double z;
  //loop over the data;
  for (int xIndex=0; xIndex<NBinsX; ++xIndex){
      for (int yIndex=0; yIndex<NBinsY; ++yIndex){
    //ret->data()->cellToCoord(xIndex, yIndex, &x, &y);
    z = plot->GetBinContent(xIndex+1,yIndex+1);
    //z = dum_rebin->GetBinContent(xIndex+1,yIndex+1);
    //std::cout<<"for bin ("<<xIndex<<","<<yIndex<<"), z="<<z<<std::endl;
	ret->data()->setCell(xIndex, yIndex, z);
      }
  }
  //should be done then. One minor detail. Root draws the bins from [low edge,high edge)
  //qcp draws from the bin center, so it's offset.
  //now the rest is just formatting
  //delete dum_rebin;
  return ret;
  
}
コード例 #4
0
ファイル: yarrgui.cpp プロジェクト: Yarr/Yarr
void YarrGui::detachPlot(){
    if(ui->plotTree->currentItem() == nullptr){
        std::cerr << "Please select plot to detach...\n";
        return;
    }
    if(ui->plotTree->currentItem()->childCount() > 0){
        std::cerr << "Please select plot to detach...\n";
        return;
    }

    PlotDialog * myPDiag = new PlotDialog();
    QCustomPlot * plotWidget = dynamic_cast<QCustomPlot*>(ui->scanPlots_tabWidget->currentWidget());
    if(plotWidget == nullptr){
        std::cerr << "Severe cast error. Aborting...\n";
        return;
    }

    QCustomPlot * transferPlot = dynamic_cast<QCustomPlot*>(myPDiag->childAt(10, 10));
    if(transferPlot == nullptr){
        std::cerr << "Severe cast error. Aborting...\n";
        return;
    }

    QCPPlotTitle * widgetPT = dynamic_cast<QCPPlotTitle*>(plotWidget->plotLayout()->element(0, 0));
    if(widgetPT == nullptr){
        std::cerr << "Severe cast error. Aborting... \n";
        return;
    }

    if(dynamic_cast<QCPColorMap*>(plotWidget->plottable(0)) != nullptr){
        QCPColorMap * widgetCMap = dynamic_cast<QCPColorMap*>(plotWidget->plottable(0));

        QCPColorScale * widgetCScale = dynamic_cast<QCPColorScale*>(plotWidget->plotLayout()->element(1, 1));
        if(widgetCScale == nullptr) {
            std::cerr << "Severe cast error. Aborting... \n";
            return;
        }

        transferPlot->plotLayout()->insertRow(0);
        transferPlot->plotLayout()->addElement(0, 0, new QCPPlotTitle(transferPlot, widgetPT->text()));

        QCPColorMap * transferCMap = new QCPColorMap(transferPlot->xAxis, transferPlot->yAxis);
        transferPlot->addPlottable(transferCMap);
        transferCMap->data()->setSize(80, 336);
        transferCMap->setData(widgetCMap->data(), true);
        QCPColorScale * transferCScale = new QCPColorScale(transferPlot);
        transferPlot->plotLayout()->addElement(1, 1, transferCScale);
        transferCScale->setType(QCPAxis::atRight);
        transferCMap->setColorScale(transferCScale);
        transferCMap->keyAxis()->setLabel(widgetCMap->keyAxis()->label());
        transferCMap->valueAxis()->setLabel(widgetCMap->valueAxis()->label());

        transferCScale->axis()->setLabel(widgetCScale->axis()->label());
        transferCMap->setGradient(QCPColorGradient::gpPolar);
        transferCMap->rescaleDataRange();
    }else if(dynamic_cast<QCPBars*>(plotWidget->plottable(0)) != nullptr){
        QCPBars * widgetBars = dynamic_cast<QCPBars*>(plotWidget->plottable(0));

        QCPBars * transferBars = new QCPBars(transferPlot->xAxis, transferPlot->yAxis);
        transferBars->setData(widgetBars->data(), true);
        transferBars->rescaleAxes();

        transferPlot->plotLayout()->insertRow(0);
        transferPlot->plotLayout()->addElement(0, 0, new QCPPlotTitle(transferPlot, widgetPT->text()));
        transferBars->keyAxis()->setLabel(widgetBars->keyAxis()->label());
        transferBars->valueAxis()->setLabel(widgetBars->valueAxis()->label());
    }else{
        std::cerr << "Severe cast error. Aborting... \n";                       //DEBUG
        return;
    }

    transferPlot->rescaleAxes();
    transferPlot->replot();

    myPDiag->setModal(false);
    myPDiag->show();

    removePlot();

    return;
}
コード例 #5
0
ファイル: yarrgui.cpp プロジェクト: Yarr/Yarr
void YarrGui::doScan(QString qn){
    std::ofstream *tmpOfCout = new std::ofstream("deleteMeCout.txt");
    std::streambuf *coutBuf = std::cout.rdbuf(tmpOfCout->rdbuf());
    std::ofstream *tmpOfCerr = new std::ofstream("deleteMeCerr.txt");
    std::streambuf *cerrBuf = std::cerr.rdbuf(tmpOfCerr->rdbuf());

    int N = ui->feTree->topLevelItemCount();
    int M = scanVec.size();
    for(int j = 0; j < N; j++){
        scanDone = false;
        processorDone = false;

        if(!(ui->feTree->topLevelItem(j)->child(4)->checkState(1))){continue;} //Is the Scan checkbox checked?

        Fei4 * fe = dynamic_cast<Fei4*>(bk->feList.at(j));

        ScanBase * s = nullptr;

        if(qn == "NS")   {s = new Fei4NoiseScan(bk);}
        if(qn == "DS")   {s = new Fei4DigitalScan(bk);}
        if(qn == "AS")   {s = new Fei4AnalogScan(bk);}
        if(qn == "TS")   {s = new Fei4ThresholdScan(bk);}
        if(qn == "ToTS") {s = new Fei4TotScan(bk);}
        if(qn == "GTT")  {s = new Fei4GlobalThresholdTune(bk);}
        if(qn == "GPT")  {s = new Fei4GlobalPreampTune(bk);}
        if(qn == "PTT")  {s = new Fei4PixelThresholdTune(bk);}
        if(qn == "PPT")  {s = new Fei4PixelPreampTune(bk);}
        if(qn == "CS")   {s = &cs;}

        if(s == nullptr){
            std::cerr << "Invalid scan QString parameter passed or scan object construction failed. Returning...\n";
            return;
        }

        QTreeWidgetItem * plotTreeItem = nullptr; //Plot tree item for current FE
        for(int k = 0; k < ui->plotTree->topLevelItemCount(); k++){ //Is the current FE already in the tree? ...
           if(ui->plotTree->topLevelItem(k)->text(0) == ui->feTree->topLevelItem(j)->text(0)){
               plotTreeItem = ui->plotTree->topLevelItem(k);
               break;
           }
        }

        if(plotTreeItem == nullptr){ //... if not: create a branch for it
            plotTreeItem = new QTreeWidgetItem(ui->plotTree);
            plotTreeItem->setText(0, ui->feTree->topLevelItem(j)->text(0));
        }

        QTreeWidgetItem * plotTreeItemDS = new QTreeWidgetItem(); //plot tree item for current scan
        plotTreeItemDS->setText(0, qn);
        plotTreeItem->addChild(plotTreeItemDS);

        fe->histogrammer = new Fei4Histogrammer();
        fe->histogrammer->connect(fe->clipDataFei4, fe->clipHisto);

        fe->ana = new Fei4Analysis(bk, fe->getRxChannel());
        fe->ana->connect(s, fe->clipHisto, fe->clipResult);

        if (qn=="CS"){
            CustomScan * tmp;
            tmp = dynamic_cast<CustomScan*>(s);
            if(tmp->bA.at(OCC_MAP) == true)   {fe->histogrammer->addHistogrammer(new OccupancyMap());}
            if(tmp->bA.at(TOT_MAP) == true)   {fe->histogrammer->addHistogrammer(new TotMap());}
            if(tmp->bA.at(TOT_2_MAP) == true) {fe->histogrammer->addHistogrammer(new Tot2Map());}

            if(tmp->bA.at(OCC_ANA) == true)   {fe->ana->addAlgorithm(new OccupancyAnalysis());}
            if(tmp->bA.at(NOISE_ANA) == true) {fe->ana->addAlgorithm(new NoiseAnalysis());}
            if(tmp->bA.at(TOT_ANA) == true)   {fe->ana->addAlgorithm(new TotAnalysis());}
            if(tmp->bA.at(S_CU_FIT) == true)  {fe->ana->addAlgorithm(new ScurveFitter());}
            if(tmp->bA.at(PIX_THR) == true)   {fe->ana->addAlgorithm(new OccPixelThresholdTune);}
        }else{
            fe->histogrammer->addHistogrammer(new OccupancyMap());

            if (qn == "ToTS" || qn == "GPT" || qn == "PPT"){
                fe->histogrammer->addHistogrammer(new TotMap());
                fe->histogrammer->addHistogrammer(new Tot2Map());
            }

            if(qn == "NS")   {fe->ana->addAlgorithm(new NoiseAnalysis());}
            if(qn == "DS")   {fe->ana->addAlgorithm(new OccupancyAnalysis());}
            if(qn == "AS")   {fe->ana->addAlgorithm(new OccupancyAnalysis());}
            if(qn == "TS")   {fe->ana->addAlgorithm(new ScurveFitter());}
            if(qn == "ToTS") {fe->ana->addAlgorithm(new TotAnalysis());}
            if(qn == "GTT")  {fe->ana->addAlgorithm(new OccGlobalThresholdTune());}
            if(qn == "GPT")  {fe->ana->addAlgorithm(new TotAnalysis());}
            if(qn == "PTT")  {fe->ana->addAlgorithm(new OccPixelThresholdTune());}
            if(qn == "PPT")  {fe->ana->addAlgorithm(new TotAnalysis());}
        }

        s->init();
        ui->scanProgressBar->setValue(ui->scanProgressBar->value() + (int)(100.0/(7.0*N*M))); //1
        s->preScan();
        ui->scanProgressBar->setValue(ui->scanProgressBar->value() + (int)(100.0/(7.0*N*M))); //2

        unsigned int numThreads = std::thread::hardware_concurrency();
        //std::cout << "-> Starting " << numThreads << " processor Threads:" << std::endl;
        std::vector<std::thread> procThreads;
        for (unsigned i=0; i<numThreads; i++){
            procThreads.push_back(std::thread(process, bk, &scanDone));
            //std::cout << "  -> Processor thread #" << i << " started!" << std::endl;
        }

        std::vector<std::thread> anaThreads;
        //std::cout << "-> Starting histogrammer and analysis threads:" << std::endl;
        if (fe->isActive()){
            anaThreads.push_back(std::thread(analysis, fe->histogrammer, fe->ana, &processorDone));
            //std::cout << "  -> Analysis thread of Fe " << fe->getRxChannel() << std::endl;
        }

        s->run();
        ui->scanProgressBar->setValue(ui->scanProgressBar->value() + (int)(100.0/(7.0*N*M))); //3
        s->postScan();
        ui->scanProgressBar->setValue(ui->scanProgressBar->value() + (int)(100.0/(7.0*N*M))); //4
        scanDone = true;

        for (unsigned i=0; i<numThreads; i++){
            procThreads[i].join();
        }
        ui->scanProgressBar->setValue(ui->scanProgressBar->value() + (int)(100.0/(7.0*N*M))); //5

        processorDone = true;

        for(unsigned i=0; i<anaThreads.size(); i++){
            anaThreads[i].join();
        }
        ui->scanProgressBar->setValue(ui->scanProgressBar->value() + (int)(100.0/(7.0*N*M))); //6

        if(qn != "CS") {
            delete s;
        }

//        fe->toFileBinary();
//        fe->ana->plot("Scan_GUI");

        //DEBUG begin [plotting]

        //clear raw data
        while(fe->clipDataFei4->size() != 0){
            fe->clipDataFei4->popData();
        }

        //clear raw data
        while(fe->clipHisto->size() != 0){
            fe->clipHisto->popData();
        }

        while(fe->clipResult->size() != 0){
            HistogramBase * showMe = fe->clipResult->popData();

            //Add tab to plot tab widget
            QCustomPlot * tabScanPlot = new QCustomPlot(ui->scanPlots_tabWidget); // X
            QWidget * addToTabWidget = dynamic_cast<QWidget*>(tabScanPlot);
            QString newTabName = qn + ' ' + ui->feTree->topLevelItem(j)->text(0);
            ui->scanPlots_tabWidget->addTab(addToTabWidget, newTabName);

            //Add plot to scan tree
            QTreeWidgetItem * plotTreeItemP = new QTreeWidgetItem(plotTreeItemDS); // X
            plotTreeItemP->setText(0, "Plot " + QString::number(plotTreeItemDS->childCount())
                                              + " (" + QString::fromStdString(showMe->getName()) + ")");
            plotTreeItemDS->addChild(plotTreeItemP);

            if(dynamic_cast<Histo2d*>(showMe) != nullptr){
                Histo2d * myHist2d = dynamic_cast<Histo2d*>(showMe);
                //Create plot

                QCPColorMap * colorMap = new QCPColorMap(tabScanPlot->xAxis, tabScanPlot->yAxis);
                QCPColorScale * colorScale = new QCPColorScale(tabScanPlot);

                tabScanPlot->addPlottable(colorMap);
                tabScanPlot->plotLayout()->insertRow(0);
                tabScanPlot->plotLayout()->addElement(0, 0, new QCPPlotTitle(tabScanPlot, QString::fromStdString(myHist2d->getName())));

                colorMap->setName(QString::fromStdString(myHist2d->getName()));
                colorMap->data()->setSize(80, 336);
                colorMap->data()->setRange(QCPRange(0, 80), QCPRange(0, 336));
                colorMap->keyAxis()->setLabel(QString::fromStdString(myHist2d->getXaxisTitle()));
                colorMap->valueAxis()->setLabel(QString::fromStdString(myHist2d->getYaxisTitle()));
                for(int xCoord = 0; xCoord<80; xCoord++){
                    for(int yCoord = 0; yCoord<336; yCoord++){
                        double colVal = myHist2d->getBin(yCoord + 336*xCoord);              //TODO make better
                        colorMap->data()->setCell(xCoord, yCoord, colVal);
                    }
                }

                tabScanPlot->plotLayout()->addElement(1, 1, colorScale);
                colorScale->setType(QCPAxis::atRight);
                colorMap->setColorScale(colorScale);
                colorScale->axis()->setLabel(QString::fromStdString(myHist2d->getZaxisTitle()));
                colorMap->setGradient(QCPColorGradient::gpPolar);
                colorMap->rescaleDataRange();

            }else if(dynamic_cast<Histo1d*>(showMe) != nullptr){
                Histo1d * myHist1d = dynamic_cast<Histo1d*>(showMe);

                QCPBars * myBars = new QCPBars(tabScanPlot->xAxis, tabScanPlot->yAxis);
                tabScanPlot->addPlottable(myBars);
                myBars->setName(QString::fromStdString(myHist1d->getName()));

                for(unsigned int i = 0; i < myHist1d->size(); i++) {
                    myBars->addData((double)(i+1), myHist1d->getBin(i));
                }

                myBars->rescaleAxes();
                tabScanPlot->plotLayout()->insertRow(0);
                tabScanPlot->plotLayout()->addElement(0, 0, new QCPPlotTitle(tabScanPlot, QString::fromStdString(myHist1d->getName())));
                myBars->keyAxis()->setLabel(QString::fromStdString(myHist1d->getXaxisTitle()));
                myBars->valueAxis()->setLabel(QString::fromStdString(myHist1d->getYaxisTitle()));

            }else{
                std::cerr << "Correct plot type not found or severe cast error\n";                  //DEBUG
                return;
            }

            tabScanPlot->rescaleAxes();
            tabScanPlot->replot();
        }
        ui->scanProgressBar->setValue(ui->scanProgressBar->value() + (int)(100.0/(7.0*N*M))); //7
        delete fe->histogrammer;
        fe->histogrammer = nullptr;
        delete fe->ana;
        fe->ana = nullptr;
    }

    std::cout.rdbuf(coutBuf);
    std::cerr.rdbuf(cerrBuf);
    tmpOfCout->close();
    tmpOfCerr->close();

    std::ifstream tmpInCout("deleteMeCout.txt");
    std::ifstream tmpInCerr("deleteMeCerr.txt");
    std::cout << tmpInCout.rdbuf();
    std::cerr << tmpInCerr.rdbuf();
    tmpInCout.close();
    tmpInCerr.close();

    return;
}