Пример #1
0
//MAIN RENDER CODE ================================================
void DataGrid::render()
{
    //vector<T> xData(_xData); //Assign xData to non-const so we can pass by const reference.
    
    // Add the border if flagged
    if (flags & (int)o_t::BORDER) {
        addBorder();
        const int shrink = 4;
        plotArea.setSize(plotArea.getWidth()-shrink, plotArea.getHeight()-shrink);
        plotArea.translate(shrink/2, shrink/2);
    }
    
    // Add the title if flagged
    if (flags & (int)o_t::TITLE) {
        addTitle();
    }
    
    // Abort if series vec is empty
    if (series.getNumSeries()==0){
        cout << "No data series available, aborting. \n";
        return;
    }
    
    // get ranges
    const float xMin = series.getXmin();
    const float xMax = series.getXmax();
    const float yMin = series.getYmin();
    const float yMax = series.getYmax();
    // Another way of aliasing - pointless here bu I <3 lambdas
    // To use this alternative, replace xMin with xMin() below
    // auto xMin = [&](){return series.getXmin();};  
    
    // Add the y-axis if flagged
    if (flags & (int)o_t::YAXIS) {
        const int yaxWidth = 10;
        Rectangle rAx = Rectangle(plotArea.getTL(), yaxWidth, plotArea.getHeight());
        addYAxis(yMin, yMax, rAx);
        
        // Shift the plot area
        plotArea.setWidth(plotArea.getWidth()-yaxWidth);
        plotArea.translate(yaxWidth, 0);
    }
    
    // Add simple x-axis indicators if requested
    if (flags & (int)o_t::XAXIS) {
        addXAxis(xMin, xMax, plotArea);
    }
    
    
    for (size_t ii=0; ii<series.getNumSeries(); ++ii){
        // Warn if input is messed up
        vector<float> xData(series.getXdata(ii));
        vector<float> yData(series.getYdata(ii));
        
        if (xData.size()!=yData.size()) {
            cout << "WARNING: x and y data size mismatch, not plotting.\n";
            return;
        }
        
        
        for(size_t nn=0; nn<xData.size(); ++nn){
            int intX = mapVal(   xData[nn], xMin, xMax, plotArea.getLeft(), plotArea.getRight()   );
            // Notice the subtle reversal of yMax and yMin here so data flipped the correct way
            int intY = mapVal(   yData[nn], yMax, yMin, plotArea.getTop(), plotArea.getBtm()   );
            addPoint(Point(intX, intY), series.getMarker(ii) );
        }
    }
    
    // Add the legend if flagged
    if (flags & (int)o_t::LEGEND) {
        addLegend();
    }
    
} // End of rendering function
////////////////////////////////////////
// getHists - Get Histograms	   
SEXP getHistsR(SEXP fileForHists, SEXP histNames, SEXP directoryR)
{
  TFile* f = checkForFileForHistsWrapper(fileForHists);

  const char* oldDirectory = setFileDirectory(f, directoryR);

  // Make the return list
  SEXP ans, ansNames;
  PROTECT( ans = NEW_LIST( GET_LENGTH(histNames) ) );
  PROTECT( ansNames = NEW_STRING( GET_LENGTH(histNames) ) );

  // Loop over the names
  for ( unsigned int i = 0; i < GET_LENGTH(histNames); ++i ) {

    // Get the name of the object
		std::string name = CHAR( STRING_ELT(histNames, i) );
    
    // Set the list name
    SET_STRING_ELT( ansNames, i, mkChar(name.c_str()) );

    // What is this thing?
    TKey* key = gDirectory->FindKey(name.c_str());

    // If not found, then skip
    if ( ! key ) continue;

    // The object is real! - Get the class name
    const char* className = key->GetClassName();

    // Do certain things depending on the class name

    // For right now, we'll just do TH1F's and TH1D's. Maybe do more later.
    if ( strcmp(className, "TH1F") == 0 || strcmp(className, "TH1D") == 0 ) {

      // Get the histogram
      TH1* hist;
      gDirectory->GetObject(name.c_str(), hist);
      
      // For TH1F, there are 6 elements to the list (name, type, title,
      //    breaks, counts, 
      //    uncert, 
      //    mids, xname, underOverFlows, mean, rms, yname)
      SET_ELEMENT( ans, i, NEW_LIST(12) );
      SEXP data = VECTOR_ELT(ans, i);
      SEXP dataNames;
      PROTECT( dataNames = NEW_STRING(12) );

      // Add the type
      unsigned int j = 0;
      SEXP type = addCharVector(data, dataNames, j++, 1, "type");
      SET_STRING_ELT(type, 0, mkChar(className));
      
      // Add the name
      SEXP theName = addCharVector(data, dataNames, j++, 1, "name");
      SET_STRING_ELT(theName, 0, mkChar(name.c_str()));

      // Add basic histogram information
      j = addHistInfo(data, dataNames, j, hist);
      
      // Add the x-axis information
      j = addXAxis(data, dataNames, j, hist);

      // Add the x-contents information
      j = addXContents(data, dataNames, j, hist);

      // Add Y-axis name
      SEXP yname = addCharVector(data, dataNames, j++, 1, "yname");
      SET_STRING_ELT(yname, 0, mkChar( hist->GetYaxis()->GetTitle() ) );
      
      // Done
      SET_NAMES(data, dataNames);
      UNPROTECT(1);  // unprotect dataNames
    }

    else {
      REprintf("!! Do not know how to handle %s of class %s !!\n", 
	       name.c_str(), className);
      continue;
    }

  } // For over names
	
  // Restore the root old directory
  if ( ! f->cd(oldDirectory) ) {
    error("namesMatchingClass: cd to old directory failed");
  }

  // return the results
  SET_NAMES(ans, ansNames);
  UNPROTECT(2);
  
  return ans;
}