Someting wrong exporting the coordinaion number using a script (Forcite RDF analysis)

Hi all,
I am trying to make a study table listing the coordination number of each frame of a .xtd document using Forcite RDF analysis. If I manuly analyze this, it will take a lot of time. So I wrote a script to do the job. Yet there's someting wrong with this script, the coordination number can't be properly exported to the table, which I can't figure out why.
For example, if I set "
my \$CNcutoff = 3.15;", no result is exported; if "my \$CNcutoff = 2.85;", the I can get the coordination number in the study table. I've already checked the results in temperary documents, they are perfectly generated.
The following is the script, attached is the .xtd document. please help to check if there's any problem.
Thanks in advance.
Merlin

#!perl

use strict;
use Getopt::Long;
use warnings;
use MaterialsScript qw(:all);

# Coordination Number Evolution Analysis Tool

use constant PI => 3.14159265358979323846;

# User Input Section
my \$doc = \$Documents{"test.xtd"};
my \$CNStudyTable = Documents->New("Coordination_Number_Evolution.std");
my \$RDFSetA = "SetA";
my \$RDFSetB = "SetB";
my \$RDFBinWidth = 0.1;
my \$RDFCutoff = 10;
my \$CNcutoff = 3.15;

# Count atoms in the selected set
my \$Batoms = \$doc->UnitCell->Sets(\$RDFSetB)->Atoms;
my \$BAtomcounts = scalar(@\$Batoms);

my \$Aatoms = \$doc->UnitCell->Sets(\$RDFSetA)->Atoms;
my \$AAtomcounts = scalar(@\$Aatoms);

# Get volume 
my \$Volume = \$doc->SymmetrySystem->Volume;

# Create a new study table document to hold the results
my \$IntegratedCNsheet = \$CNStudyTable->ActiveSheet;
\$IntegratedCNsheet->ColumnHeading(0) = "Frame";
\$IntegratedCNsheet->ColumnHeading(1) = "Coordination Number";
\$IntegratedCNsheet->ColumnHeading(2) = "Structure";

# Main loop
my \$numFrames = \$doc->Trajectory->NumFrames;
for (my \$counter = 1; \$counter <= \$numFrames; ++\$counter) 
{
    \$doc->Trajectory->CurrentFrame = \$counter;
    my \$tmpdoc = Documents->New("tmp.xsd");
    \$tmpdoc->CopyFrom(\$doc);
    \$IntegratedCNsheet->Cell(\$counter-1, 0) = \$counter;
    my \$Framedoc = Documents->New("Frame.xsd");
    \$Framedoc->CopyFrom(\$doc);
    \$IntegratedCNsheet->Cell(\$counter-1, 2) = \$Framedoc;
    
    # Initialize Forcite RDF analysis
    my \$results = Modules->Forcite->Analysis->RadialDistributionFunction(\$tmpdoc, Settings(
        RDFBinWidth => \$RDFBinWidth, 
        RDFCutoff => \$RDFCutoff, 
        RDFSetA => \$RDFSetA, 
        RDFSetB => \$RDFSetB));
    my \$outRDFChart = \$results->RDFChart;
    my \$outRDFChartAsStudyTable = \$results->RDFChartAsStudyTable;    
    my \$columnCount = \$outRDFChartAsStudyTable->Sheets(2)->ColumnCount;
    my \$rowCount = \$outRDFChartAsStudyTable->Sheets(2)->RowCount;
    
    # COllect RDF data    
    # Generate a temp studytable    
    my \$tmpStudyTable = Documents->New("Coordination_Number_\$counter.std");
    my \$calcSheet = \$tmpStudyTable->ActiveSheet;
    \$calcSheet->ColumnHeading(0) = "r(Angstrom)";
    \$calcSheet->ColumnHeading(1) = "g(r)";
    \$calcSheet->ColumnHeading(2) = "IntegratedCN";

    # Get r data from RDF studytable    
    for (my \$rownum = 0; \$rownum < \$rowCount; ++\$rownum) 
    {    
        my \$cell = \$outRDFChartAsStudyTable->Sheets(2)->Cell(\$rownum, 0);
        \$calcSheet->Cell(\$rownum, 0) = \$cell;        
    }
    
    # Get coordination number and integrated coordination number data from RDF studytable    
    my \$integratedRDF;
    my \$CoordinateNumber;
    my \$integratedCN;        
    for (my \$rownum = 0; \$rownum < \$rowCount; ++\$rownum) 
    {
        my \$r = \$outRDFChartAsStudyTable->Sheets(2)->Cell(\$rownum, 0);
        \$calcSheet->Cell(\$rownum, 0) = \$r;        
        my \$gr = \$outRDFChartAsStudyTable->Sheets(2)->Cell(\$rownum, 1);
        \$calcSheet->Cell(\$rownum, 1) = \$gr;        
        \$integratedRDF = \$integratedRDF + \$gr;        
        \$CoordinateNumber = \$BAtomcounts * \$gr * 4 * PI * \$r * \$r * \$RDFBinWidth / \$Volume;        
        \$integratedCN = \$integratedCN + \$CoordinateNumber;        
        \$calcSheet->Cell(\$rownum, 2) = \$integratedCN;        
        if (\$r == \$CNcutoff)
        {
            my \$IntegratedCNsheet->Cell(\$counter-1, 1) = \$integratedCN;
        }
    
    }
    
    # Some clean work    
    \$outRDFChart->Discard;
    \$outRDFChartAsStudyTable->Discard;
    \$tmpdoc->Discard;
    \$Framedoc->Discard;
    \$tmpStudyTable->Discard;
}