Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Origin.h>
- //#pragma labtalk(0) // to disable OC functions for LT calling.
- #include <XFBase.h>
- #include <GetNbox.h>
- /**
- * Get Parameters for Integration; is: Start- & Endpoint in X-Axis-Coordinates
- **/
- bool askForParameters_Integration(double &xStart, double &xEnd, bool &recalcWidth, double &baseline, string &newName)
- {
- GETN_TREE(myTree);
- GETN_NUM(get_xStart, "X-Range Start", xStart);
- GETN_NUM(get_xEnd, "X-Range End", xEnd);
- GETN_STR(info_Text1, "(Integration range in X-Values)", "hidden"); GETN_HINT_EX(FALSE, FALSE);
- GETN_CHECK(get_recalcWidth, "Integrate over xStart+RiseTime", recalcWidth)
- GETN_STR(info_Text2, "(Try to read user-parameter \"RiseTime\" and adjust integration-Width accordingly)", "hidden"); GETN_HINT_EX(FALSE, FALSE)
- GETN_NUM(get_Baseline, "j0 (\"Baseline\")", baseline);
- GETN_STR(info_Text3, "(4th Row will be filled with that value to calculate n)", "hidden"); GETN_HINT_EX(FALSE, FALSE);
- GETN_STR(get_Name, "LongName for new Worksheet", newName);
- if(GetNBox(myTree, "Integrate All", "Select parameters for integration"))
- {
- xStart = myTree.get_xStart.dVal;
- xEnd = myTree.get_xEnd.dVal;
- baseline = myTree.get_Baseline.dVal;
- recalcWidth = (myTree.get_recalcWidth.nVal != 0);
- newName = myTree.get_Name.strVal;
- return true;
- }
- else
- {
- return false;
- }
- }
- bool askForParameters_Averaging(double &xStart, double &xEnd, bool &recalcWidth, double &baseline, string &newName)
- {
- GETN_TREE(myTree);
- GETN_NUM(get_xStart, "X-Range Start", xStart);
- GETN_NUM(get_xEnd, "X-Range End", xEnd);
- GETN_STR(info_Text1, "(Averaging range in X-Values)", "hidden"); GETN_HINT_EX(FALSE, FALSE);
- GETN_CHECK(get_recalcWidth, "Integrate over xStart+RiseTime", recalcWidth)
- GETN_STR(info_Text2, "(Try to read user-parameter \"RiseTime\" and adjust integration-Width accordingly)", "hidden"); GETN_HINT_EX(FALSE, FALSE)
- GETN_NUM(get_Baseline, "j0 (\"Baseline\")", baseline);
- GETN_STR(info_Text3, "(4th Row will be filled with that value to calculate n)", "hidden"); GETN_HINT_EX(FALSE, FALSE);
- GETN_STR(get_Name, "LongName for new Worksheet", newName);
- if(GetNBox(myTree, "Average All", "Select parameters vor averaging"))
- {
- xStart = myTree.get_xStart.dVal;
- xEnd = myTree.get_xEnd.dVal;
- baseline = myTree.get_Baseline.dVal;
- recalcWidth = (myTree.get_recalcWidth.nVal != 0);
- newName = myTree.get_Name.strVal;
- return true;
- }
- else
- {
- return false;
- }
- }
- /**
- * Find nearest Index in a Dataset that starts with the lowest value in dX[0]
- **/
- int findNearestX(Dataset &dX, double value)
- {
- int startIndex = dX.GetLowerBound();
- double dif = abs(value - dX[startIndex]);
- double newDif = dif;
- for (int ii=startIndex+1; ii <= dX.GetUpperBound(); ii++)
- {
- newDif = abs(value - dX[ii]);
- if (newDif > dif)
- {
- return ii-1;
- }
- dif = newDif;
- }
- return dX.GetUpperBound();
- }
- /**
- * Find an entry in the Form "<Name> = <Value>" in the UserParameters of the given Worksheet
- **/
- double getUserParameter(Worksheet &ws, string name, double defValue = 0)
- {
- if (name.IsEmpty()) return defValue;
- string label;
- int i = 0;
- while (ws.Columns(0).GetExtendedLabel(label, RCLT_UDL+(i++)))
- {
- int pos = label.Find("=");
- if (pos >= 0)
- {
- string nameString = label.Left(pos);
- nameString.TrimLeft(); nameString.TrimRight();
- if (nameString.IsEmpty()) continue;
- if (nameString.CompareNoCase(name) == 0)
- {
- //Found the name, extract the value...
- string valueString = label.Right(label.GetLength()-pos-1);
- valueString.TrimLeft(); valueString.TrimRight();
- valueString.Replace(',','.');
- double val = atof(valueString, true);
- if (val == NANUM) return defValue;
- else return val;
- }
- }
- }
- return defValue;
- }
- /**
- * Create a Worksheet-Object and bind it to the worksheet used in the given curve
- **/
- Worksheet getWorksheetForCurve(Curve &curve)
- {
- string strTemp, strWks;
- curve.GetName(strTemp);
- int nn = strTemp.Find('_');
- if(nn > 0)
- strWks = strTemp.Left(nn);
- Worksheet wks(strWks);
- return wks;
- }
- /**
- * Create a Worksheet-Object and bind it to the worksheet displayed by the given DataPlot
- **/
- bool getWorksheetForPlot(DataPlot &plot, Worksheet &worksheet)
- {
- Curve theCurve(plot);
- return getWorksheetForCurve(theCurve);
- }
- bool doIntegration()
- {
- //Parameters:
- //-adjustWidth: if true, each worksheet will be searched for the UserParameter "RiseTime" to adjust the integration width
- //-based on (currentRisetime) - ((given width) - (originalRiseTime))
- bool adjustWidth = false;
- double originalRiseTime = 0;
- double riseTimeDifference = 0;
- double baseline = 0;
- //-xStart, xEnd: Start and End-Rows for integration, oroginal selected dataset
- int xStart = 0;
- int xEnd = 0;
- //-X-Value for Start and End of integration
- double valStart, valEnd;
- //-Name for the new Worksheet
- string newWorksheetName;
- GraphLayer graph = Project.ActiveLayer();
- if( !graph )
- {
- MessageBox(GetWindow(), "No Graph appears to be active, cannot continue!", "Integrate All" , 16);
- return false;
- }
- using cc = Project.ActiveCurve();
- if( !cc )
- {
- MessageBox(GetWindow(), "There is no active dataset, cannot determine integration range and the Range-Adjust function might not work!", "Integrate All" , 64);
- valStart = 0.0;
- valEnd = 40.0;
- }
- else
- {
- //There is an active dataset, so use this to determine the start- and end-values for integration
- if (graph.GetDataMarkers(xStart, xEnd))
- {
- //Query the currently active curve (cc) for data and extract the "real" values
- Dataset xd;
- printf("Active dataset is %s",cc.GetName());
- if(cc.AttachX(xd, FALSE))
- {
- printf(", its corresponding X dataset is %s\n",xd.GetName());
- valStart = xd[xStart];
- valEnd = xd[xEnd];
- }
- else
- {
- printf(", but I cannot access it's values!!\n");
- MessageBox(GetWindow(), "I failed getting the integration range from the current curve.", "Integrate All" , 64);
- //No marker set, default to 0..40
- valStart = 0.0;
- valEnd = 40.0;
- }
- printf("Range: %f, %f\n", valStart, valEnd);
- //Try to find the RiseTime of the currently selected curve
- Worksheet selWorksheet = getWorksheetForCurve(cc);
- newWorksheetName = selWorksheet.GetPage().GetLongName().Left(11)+"-nData";
- newWorksheetName.Replace("_", "-");
- originalRiseTime = getUserParameter(selWorksheet, "RiseTime", (valEnd-valStart)/1E6)*1E6;
- }
- else
- {
- //No marker set, default to 0..40
- valStart = 0.0;
- valEnd = 40.0;
- }
- }
- if ( !askForParameters_Integration(valStart, valEnd, adjustWidth, baseline, newWorksheetName) ) return false;
- //Calculate and Print RiseTime correction
- riseTimeDifference = originalRiseTime - (valEnd - valStart);
- printf("Original RiseTime was determined to be %f.\n", originalRiseTime);
- printf("Difference to integration range: %f.\n", riseTimeDifference);
- printf("Adjusting of integration range was switched %s\n", (adjustWidth ? "on" : "off"));
- //Prepare vectors to save results
- vector<double> vec_delayTime();
- vector<double> vec_integralData();
- vector<double> vec_integralWidth();
- //Traverse all Plots in the Graph and calculate...
- foreach (DataPlot dp in graph.DataPlots)
- {
- //Get the original Datarange of the Plot
- DataRange dr;
- dp.GetDataRange(dr);
- // Curve
- Curve cur(dp);
- string strTemp;
- cur.GetName(strTemp);
- if (strTemp == "_MARKER") continue;
- printf("Working on %s...\n", strTemp);
- //Get the Dataset associated with the Plot
- //Get Dataset and find Bounds
- Dataset dX;
- if (!cur.AttachX(dX, FALSE))
- {
- printf(" Cannot attach to curv's dataset.\n");
- continue;
- }
- //Get the Worksheet associated with the Curve
- Worksheet myWorksheet = getWorksheetForCurve(cur);
- //Get the DelayTime
- double delayTime = getUserParameter(myWorksheet, "DelayTime", -1);
- //Get the X and Y Column Index
- int index1, index2;
- string shortName, strX, strY;
- curve_get_wks_col_names(cur, strX, strY);
- for( int ii = 0; ii < myWorksheet.Columns.Count(); ii++ )
- {
- myWorksheet.Columns(ii).GetName(shortName);
- if (shortName == strX) index1 = ii;
- else if (shortName == strY) index2 = ii;
- }
- //Recalculate?
- double myStart, myEnd;
- myStart = valStart;
- myEnd = valEnd;
- if (adjustWidth)
- {
- double myRiseTime = getUserParameter(myWorksheet, "RiseTime", valEnd-valStart/1E6)*1E6;
- printf(" New RiseTime = %f\n", myRiseTime);
- myRiseTime = myRiseTime - riseTimeDifference;
- myEnd = myStart + myRiseTime;
- }
- printf(" xStart = %f, xEnd = %f\n", myStart, myEnd);
- printf(" Integration Range = %f\n", myEnd - myStart);
- xStart = findNearestX(dX, myStart);
- xEnd = findNearestX(dX, myEnd);
- printf(" Will Integrage from row %d to %d...\n", xStart, xEnd);
- printf(" Using Columns X: %d, Y: %d\n", index1, index2);
- // Create Datarange
- DataRange newRange();
- newRange.Add(myWorksheet, index1, "X", index1, xStart, xEnd);
- newRange.Add(myWorksheet, index2, "Y", index2, xStart, xEnd);
- //Copy Values from the Datarange
- vector<double> xData, yData;
- newRange.GetData(xData, 0);
- newRange.GetData(yData, 1);
- //Integrate!
- double area = 0;
- int res = ocmath_numeric_integral(myEnd, &area, xData.GetSize(), xData, yData);
- printf(" Area = %f, ",area);
- vec_delayTime.Add(delayTime);
- vec_integralData.Add(area);
- vec_integralWidth.Add(myEnd - myStart);
- switch (res)
- {
- case OE_NOERROR:
- printf("Success!\n");
- break;
- case OE_NULL_POINTER:
- printf("Error: Null Pointer!\n");
- break;
- case OE_SIZE_LT:
- printf("Warning: No data to integrage!\n");
- break;
- case OE_NOT_STRICTLY_INCREASING:
- printf("Error: pxData must be monotonically increasing.!\n");
- break;
- case OE_UNDERFLOW:
- printf("Error: xStart > xEnd!\n");
- break;
- case OE_OVERFLOW:
- printf("Error: xStart is out of range!\n");
- break;
- default:
- printf("Error: unknown, %d\n", res);
- }
- }
- //Display Data
- Worksheet wks;
- wks.Create("OtraceNBook");
- wks.GetPage().SetLongName(newWorksheetName);
- /*while(wks.Columns.Count()<3) wks.AddCol();
- wks.SetColDesignations("XY");
- wks.Columns(0).SetLongName("DelayTime");
- wks.Columns(0).SetUnits("µs");
- wks.Columns(1).SetLongName("Width");
- wks.Columns(1).SetUnits("µs");
- wks.Columns(2).SetLongName("Area");*/
- vectorbase &data1 = wks.Columns(0).GetDataObject();
- data1.Append(vec_delayTime);
- vectorbase &data2 = wks.Columns(1).GetDataObject();
- data2.Append(vec_integralWidth);
- vectorbase &data3 = wks.Columns(2).GetDataObject();
- data3.Append(vec_integralData);
- vectorbase &data4 = wks.Columns(3).GetDataObject();
- vector<double> baselineData;
- for (int i = 0; i < vec_integralData.GetSize(); i++) baselineData.Add(baseline);
- data4.Append(baselineData);
- return true;
- }
- bool doAveraging()
- {
- //Parameters:
- //-adjustWidth: if true, each worksheet will be searched for the UserParameter "RiseTime" to adjust the averaging width
- //-based on (currentRisetime) - ((given width) - (originalRiseTime))
- bool adjustWidth = false;
- double originalRiseTime = 0;
- double riseTimeDifference = 0;
- double baseline = 0;
- //-xStart, xEnd: Start and End-Rows for averaging, oroginal selected dataset
- int xStart = 0;
- int xEnd = 0;
- //-X-Value for Start and End of averaging
- double valStart, valEnd;
- //-Name for the new Worksheet
- string newWorksheetName;
- GraphLayer graph = Project.ActiveLayer();
- if( !graph )
- {
- MessageBox(GetWindow(), "No Graph appears to be active, cannot continue!", "Average All" , 16);
- return false;
- }
- using cc = Project.ActiveCurve();
- if( !cc )
- {
- MessageBox(GetWindow(), "There is no active dataset, cannot determine averaging range and the Range-Adjust function might not work!", "Average All" , 64);
- valStart = 0.0;
- valEnd = 40.0;
- }
- else
- {
- //There is an active dataset, so use this to determine the start- and end-values for averaging
- if (graph.GetDataMarkers(xStart, xEnd))
- {
- //Query the currently active curve (cc) for data and extract the "real" values
- Dataset xd;
- printf("Active dataset is %s",cc.GetName());
- if(cc.AttachX(xd, FALSE))
- {
- printf(", its corresponding X dataset is %s\n",xd.GetName());
- valStart = xd[xStart];
- valEnd = xd[xEnd];
- }
- else
- {
- printf(", but I cannot access it's values!!\n");
- MessageBox(GetWindow(), "I failed getting the integration range from the current curve.", "Average All" , 64);
- //No marker set, default to 0..40
- valStart = 0.0;
- valEnd = 40.0;
- }
- printf("Range: %f, %f\n", valStart, valEnd);
- //Try to find the RiseTime of the currently selected curve
- Worksheet selWorksheet = getWorksheetForCurve(cc);
- newWorksheetName = selWorksheet.GetPage().GetLongName().Left(11)+"-nData";
- newWorksheetName.Replace("_", "-");
- originalRiseTime = getUserParameter(selWorksheet, "RiseTime", (valEnd-valStart)/1E6)*1E6;
- }
- else
- {
- //No marker set, default to 0..40
- valStart = 0.0;
- valEnd = 40.0;
- }
- }
- if ( !askForParameters_Averaging(valStart, valEnd, adjustWidth, baseline, newWorksheetName) ) return false;
- //Calculate and Print RiseTime correction
- riseTimeDifference = originalRiseTime - (valEnd - valStart);
- printf("Original RiseTime was determined to be %f.\n", originalRiseTime);
- printf("Difference to integration range: %f.\n", riseTimeDifference);
- printf("Adjusting of integration range was switched %s\n", (adjustWidth ? "on" : "off"));
- //Prepare vectors to save results
- vector<double> vec_delayTime();
- vector<double> vec_averageData();
- vector<double> vec_averageWidth();
- //Traverse all Plots in the Graph and calculate...
- foreach (DataPlot dp in graph.DataPlots)
- {
- //Get the original Datarange of the Plot
- DataRange dr;
- dp.GetDataRange(dr);
- // Curve
- Curve cur(dp);
- string strTemp;
- cur.GetName(strTemp);
- if (strTemp == "_MARKER") continue;
- printf("Working on %s...\n", strTemp);
- //Get the Dataset associated with the Plot
- //Get Dataset and find Bounds
- Dataset dX;
- if (!cur.AttachX(dX, FALSE))
- {
- printf(" Cannot attach to curv's dataset.\n");
- continue;
- }
- //Get the Worksheet associated with the Curve
- Worksheet myWorksheet = getWorksheetForCurve(cur);
- //Get the DelayTime
- double delayTime = getUserParameter(myWorksheet, "DelayTime", -1);
- //Get the X and Y Column Index
- int index1, index2;
- string shortName, strX, strY;
- curve_get_wks_col_names(cur, strX, strY);
- for( int ii = 0; ii < myWorksheet.Columns.Count(); ii++ )
- {
- myWorksheet.Columns(ii).GetName(shortName);
- if (shortName == strX) index1 = ii;
- else if (shortName == strY) index2 = ii;
- }
- //Recalculate?
- double myStart, myEnd;
- myStart = valStart;
- myEnd = valEnd;
- if (adjustWidth)
- {
- double myRiseTime = getUserParameter(myWorksheet, "RiseTime", valEnd-valStart/1E6)*1E6;
- printf(" New RiseTime = %f\n", myRiseTime);
- myRiseTime = myRiseTime - riseTimeDifference;
- myEnd = myStart + myRiseTime;
- }
- printf(" xStart = %f, xEnd = %f\n", myStart, myEnd);
- printf(" Averaging Range = %f\n", myEnd - myStart);
- xStart = findNearestX(dX, myStart);
- xEnd = findNearestX(dX, myEnd);
- printf(" Will Average from row %d to %d...\n", xStart, xEnd);
- printf(" Using Columns X: %d, Y: %d\n", index1, index2);
- // Create Datarange
- DataRange newRange();
- //newRange.Add(myWorksheet, index1, "X", index1, xStart, xEnd);
- newRange.Add(myWorksheet, index2, "Y", index2, xStart, xEnd);
- //Copy Values from the Datarange
- vector<double> xData, yData;
- newRange.GetData(yData, 0);
- //newRange.GetData(yData, 1);
- //Average!
- double average = 0;
- int len = yData.GetSize();
- if (len > 0)
- {
- //Problem: sum first, or device first; I'd say: sum first, as values summed up
- //are likely to be much smaller than the integer count of samples...
- for (int i=0; i < len; i++) average += yData[i];
- average = average / len;
- }
- else
- printf("Warning: No data to average!\n");
- printf(" Average = %f, ",average);
- vec_delayTime.Add(delayTime);
- vec_averageData.Add(average);
- vec_averageWidth.Add(myEnd - myStart);
- }
- //Display Data
- Worksheet wks;
- wks.Create("OtraceBaselineBook");
- wks.GetPage().SetLongName(newWorksheetName);
- vectorbase &data1 = wks.Columns(0).GetDataObject();
- data1.Append(vec_delayTime);
- vectorbase &data2 = wks.Columns(1).GetDataObject();
- data2.Append(vec_averageWidth);
- vectorbase &data3 = wks.Columns(2).GetDataObject();
- data3.Append(vec_averageData);
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment