コード例 #1
0
ファイル: 3dFDR.c プロジェクト: CesarCaballeroGaudes/afni
void process_subbrick (THD_3dim_dataset * dset, int ibrick)

{
  const float EPSILON = 1.0e-10;
  float factor;            /* factor is new scale factor for this sub-brick */
  void * vfim = NULL;      /* sub-brick data pointer */
  float * ffim = NULL;     /* sub-brick data in floating point format */
  char brick_label[THD_MAX_NAME];       /* sub-brick label */


ENTRY("process_subbrick") ;
  if (!FDR_quiet)  printf ("Processing sub-brick #%d \n", ibrick);

  
  /*----- Allocate memory for float data -----*/
  ffim = (float *) malloc (sizeof(float) * FDR_nxyz);   MTEST (ffim);


  /*----- Convert sub-brick to float stats -----*/
  SUB_POINTER (dset, ibrick, 0, vfim);
  EDIT_coerce_scale_type (FDR_nxyz, DSET_BRICK_FACTOR(dset,ibrick),
			  DSET_BRICK_TYPE(dset,ibrick), vfim,   /* input  */
			  MRI_float                   , ffim);  /* output */


  /*----- Calculate FDR z-scores for all voxels within this volume -----*/
  process_volume (ffim, DSET_BRICK_STATCODE(dset,ibrick),
		        DSET_BRICK_STATAUX (dset,ibrick));


  /*----- Replace old sub-brick with new z-scores -----*/
  if( !FDR_float || DSET_BRICK_TYPE(dset,ibrick)==MRI_float ){
    SUB_POINTER (dset, ibrick, 0, vfim);
    factor = EDIT_coerce_autoscale_new (FDR_nxyz, MRI_float, ffim,
				      DSET_BRICK_TYPE(dset,ibrick), vfim);  
    if (factor < EPSILON)  factor = 0.0;
    else factor = 1.0 / factor;
    if( DSET_BRICK_TYPE(dset,ibrick) == MRI_short )
      EDIT_misfit_report( DSET_FILECODE(dset) , ibrick ,
                          FDR_nxyz , factor , vfim , ffim ) ;
  } else {                          /*** if -float was given ***/
    EDIT_substitute_brick( dset , ibrick , MRI_float , ffim ) ;
    ffim = NULL ; factor = 0.0f ;
  }
  

  /*----- edit the sub-brick -----*/
  if( FDR_qval ) strcpy (brick_label, "FDRq:");
  else           strcpy (brick_label, "FDRz:");
  strcat (brick_label, DSET_BRICK_LABEL(dset, ibrick));
  EDIT_BRICK_LABEL (dset, ibrick, brick_label);
  EDIT_BRICK_FACTOR (dset, ibrick, factor);
  if( !FDR_qval ) EDIT_BRICK_TO_FIZT  (dset,ibrick);
  else            EDIT_BRICK_TO_NOSTAT(dset,ibrick);

  /*----- Deallocate memory -----*/
  if (ffim != NULL) { free (ffim);   ffim = NULL; }

  EXRETURN ;
}
コード例 #2
0
ファイル: 3destpdf.c プロジェクト: CesarCaballeroGaudes/afni
void initialize_program 
(
  int argc,                        /* number of input arguments */
  char ** argv                     /* array of input arguments */ 
)

{
  float parameters [DIMENSION];    /* parameters for PDF estimation */
  Boolean ok = TRUE;               /* flag for successful PDF estimation */

  int nx, ny, nz, nxy, nxyz, ixyz;       /* voxel counters */
  int n;                                 /* histogram bin index */
  short * sfim = NULL;                   /* pointer to anat data */
  short * rfim = NULL;                   /* truncated data */ 
  int icount;
  int lower_cutoff = 25;

  /*----- Get operator inputs -----*/
  get_options (argc, argv);


  /*----- Initialize local variables -----*/
  if (anat == NULL)  estPDF_error ("Unable to read anat dataset");
  nx = DSET_NX(anat);   ny = DSET_NY(anat);   nz = DSET_NZ(anat);
  nxy = nx*ny;   nxyz = nxy*nz;
  sfim  = (short *) DSET_BRICK_ARRAY(anat,0) ;
  if (sfim == NULL)  estPDF_error ("Unable to read anat dataset");
  rfim = (short *) malloc (sizeof(short) * nxyz);   MTEST (rfim);


  /*----- Just use voxels whose intensity is above the lower cutoff -----*/
  icount = 0;
  for (ixyz = 0;  ixyz < nxyz;  ixyz++)
    if (sfim[ixyz] > lower_cutoff)
      {
	rfim[icount] = sfim[ixyz];
	icount++;
      }
  printf ("%d voxels above lower cutoff = %d \n", icount, lower_cutoff);


  /*----- Get PDF estimate and set voxel intensity limits -----*/
  estpdf_short (icount, rfim, parameters);
  min_val_float = parameters[4] - 2.0*parameters[5];
  max_val_float = parameters[7] + 2.0*parameters[8];
  
   
  if (! quiet)
    {
      printf ("\n");
      printf ("Control inputs: \n");
      printf ("anat filename = %s \n", anat_filename);
      printf ("min value = %f \n", min_val_float);
      printf ("max value = %f \n", max_val_float);
    }


}
コード例 #3
0
ファイル: 3dFDR.c プロジェクト: CesarCaballeroGaudes/afni
float * read_time_series 
(
  char * ts_filename,          /* time series file name (plus column index) */
  int * ts_length              /* output value for time series length */
)

{
  char message[THD_MAX_NAME];    /* error message */
  char * cpt;                    /* pointer to column suffix */
  char filename[THD_MAX_NAME];   /* time series file name w/o column index */
  char subv[THD_MAX_NAME];       /* string containing column index */
  MRI_IMAGE * im, * flim;  /* pointers to image structures 
			      -- used to read 1D ASCII */
  float * far;             /* pointer to MRI_IMAGE floating point data */
  int nx;                  /* number of time points in time series */
  int ny;                  /* number of columns in time series file */
  int iy;                  /* time series file column index */
  int ipt;                 /* time point index */
  float * ts_data = NULL;  /* input time series data */


  /*----- First, check for empty filename -----*/
  if (ts_filename == NULL)
    FDR_error ("Missing input time series file name");


  /*----- Read the time series file -----*/
  flim = mri_read_1D(ts_filename) ;
  if (flim == NULL)
    {
      sprintf (message,  "Unable to read time series file: %s",  ts_filename);
      FDR_error (message);
    }

  far = MRI_FLOAT_PTR(flim);
  nx = flim->nx;
  ny = flim->ny; iy = 0 ;
  if( ny > 1 ){
    fprintf(stderr,"WARNING: time series %s has more than 1 column\n",ts_filename);
  }
  

  /*----- Save the time series data -----*/
  *ts_length = nx;
  ts_data = (float *) malloc (sizeof(float) * nx);
  MTEST (ts_data);
  for (ipt = 0;  ipt < nx;  ipt++)
    ts_data[ipt] = far[ipt + iy*nx];   
  
  
  mri_free (flim);  flim = NULL;

  return (ts_data);
}
コード例 #4
0
ファイル: 3destpdf.c プロジェクト: CesarCaballeroGaudes/afni
void get_options
(
  int argc,                        /* number of input arguments */
  char ** argv                     /* array of input arguments */ 
)

{
  int nopt = 1;                     /* input option argument counter */
  int ival, index;                  /* integer input */
  float fval;                       /* float input */
  char message[MAX_STRING_LENGTH];  /* error message */


  /*----- does user request help menu? -----*/
  if (argc < 2 || strncmp(argv[1], "-help", 5) == 0)  display_help_menu();  
   

  /*----- main loop over input options -----*/
  while (nopt < argc )
    {

      /*-----   -anat filename   -----*/
      if (strncmp(argv[nopt], "-anat", 5) == 0)
	{
	  nopt++;
	  if (nopt >= argc)  estPDF_error ("need argument after -anat ");
	  anat_filename = malloc (sizeof(char) * MAX_STRING_LENGTH);
	  MTEST (anat_filename);
	  strcpy (anat_filename, argv[nopt]);

	  anat = THD_open_one_dataset (anat_filename);
	  if (!ISVALID_3DIM_DATASET (anat))
	    {
	      sprintf (message, "Can't open dataset: %s\n", anat_filename); 
	      estPDF_error (message); 
	    } 
	  DSET_load(anat); CHECK_LOAD_ERROR(anat);

	  nopt++;
	  continue;
	}
      

      /*----- unknown command -----*/
      sprintf(message,"Unrecognized command line option: %s\n", argv[nopt]);
      estPDF_error (message);
      
    }

  
}
コード例 #5
0
ファイル: 3dFDR.c プロジェクト: CesarCaballeroGaudes/afni
voxel * create_voxel ()
{
  voxel * voxel_ptr = NULL;

  voxel_ptr = (voxel *) malloc (sizeof(voxel));
  MTEST (voxel_ptr);
  
  voxel_ptr->ixyz = 0;
  voxel_ptr->pvalue = 0.0;
  voxel_ptr->next_voxel = NULL;

  return (voxel_ptr);
  
}
コード例 #6
0
ファイル: 3dttest.c プロジェクト: Gilles86/afni
int main( int argc , char *argv[] )
{
   int nx,ny,nz , nxyz , ii,kk , num1,num2 , num_tt=0 , iv ,
       piece , fim_offset;
   float dx,dy,dz , dxyz ,
         num1_inv=0.0 , num2_inv , num1m1_inv=0.0 , num2m1_inv , dof ,
         dd,tt,q1,q2 , f1,f2 , tt_max=0.0 ;
   THD_3dim_dataset *dset=NULL , *new_dset=NULL ;
   THD_3dim_dataset * base_dset;
   float *av1 , *av2 , *sd1 , *sd2 , *ffim , *gfim ;
   float *base_ary=NULL;

   void  *vsp ;
   void  *vdif ;           /* output mean difference */
   char  cbuf[THD_MAX_NAME] ;
   float fbuf[MAX_STAT_AUX] , fimfac ;
   int   output_datum ;
   float npiece , memuse ;

   float *dofbrik=NULL , *dofar=NULL ;
   THD_3dim_dataset *dof_dset=NULL ;

   /*-- read command line arguments --*/

   if( argc < 2 || strncmp(argv[1],"-help",5) == 0 ) TT_syntax(NULL) ;

   /*-- 20 Apr 2001: addto the arglist, if user wants to [RWCox] --*/

   mainENTRY("3dttest main"); machdep() ; PRINT_VERSION("3dttest") ;
   INFO_message("For most purposes, 3dttest++ should be used instead of 3dttest!") ;

   { int new_argc ; char ** new_argv ;
     addto_args( argc , argv , &new_argc , &new_argv ) ;
     if( new_argv != NULL ){ argc = new_argc ; argv = new_argv ; }
   }

   AFNI_logger("3dttest",argc,argv) ;

   TT_read_opts( argc , argv ) ;

   if( ! TT_be_quiet )
      printf("3dttest: t-tests of 3D datasets, by RW Cox\n") ;

   /*-- read first dataset in set2 to get dimensions, etc. --*/

   dset = THD_open_dataset( TT_set2->ar[0] ) ;  /* 20 Dec 1999  BDW */
   if( ! ISVALID_3DIM_DATASET(dset) )
     ERROR_exit("Unable to open dataset file %s",TT_set2->ar[0]);

   nx = dset->daxes->nxx ;
   ny = dset->daxes->nyy ;
   nz = dset->daxes->nzz ;         nxyz = nx * ny * nz ;
   dx = fabs(dset->daxes->xxdel) ;
   dy = fabs(dset->daxes->yydel) ;
   dz = fabs(dset->daxes->zzdel) ; dxyz = dx * dy * dz ;

#ifdef TTDEBUG
printf("*** nx=%d ny=%d nz=%d\n",nx,ny,nz) ;
#endif

   /*-- make an empty copy of this dataset, for eventual output --*/

#ifdef TTDEBUG
printf("*** making empty dataset\n") ;
#endif

   new_dset = EDIT_empty_copy( dset ) ;

   tross_Make_History( "3dttest" , argc,argv , new_dset ) ;

   strcpy( cbuf , dset->self_name ) ; strcat( cbuf , "+TT" ) ;

   iv = DSET_PRINCIPAL_VALUE(dset) ;

   if( TT_datum >= 0 ){
      output_datum = TT_datum ;
   } else {
      output_datum = DSET_BRICK_TYPE(dset,iv) ;
      if( output_datum == MRI_byte ) output_datum = MRI_short ;
   }

#ifdef TTDEBUG
printf(" ** datum = %s\n",MRI_TYPE_name[output_datum]) ;
#endif

   iv = EDIT_dset_items( new_dset ,
                           ADN_prefix , TT_prefix ,
                           ADN_label1 , TT_prefix ,
                           ADN_directory_name , TT_session ,
                           ADN_self_name , cbuf ,
                           ADN_type , ISHEAD(dset) ? HEAD_FUNC_TYPE : GEN_FUNC_TYPE ,
                           ADN_func_type , FUNC_TT_TYPE ,
                           ADN_nvals , FUNC_nvals[FUNC_TT_TYPE] ,
                           ADN_ntt , 0 ,                           /* 07 Jun 2007 */
                           ADN_datum_all , output_datum ,
                         ADN_none ) ;

   if( iv > 0 )
     ERROR_exit("%d errors in attempting to create output dataset!",iv ) ;

   if( THD_deathcon() && THD_is_file(new_dset->dblk->diskptr->header_name) )
      ERROR_exit(
              "Output dataset file %s already exists--cannot continue!\a",
              new_dset->dblk->diskptr->header_name ) ;

#ifdef TTDEBUG
printf("*** deleting exemplar dataset\n") ;
#endif

   THD_delete_3dim_dataset( dset , False ) ; dset = NULL ;

/** macro to test a malloc-ed pointer for validity **/

#define MTEST(ptr) \
   if((ptr)==NULL) \
      ( fprintf(stderr,"*** Cannot allocate memory for statistics!\n"), exit(0) )

   /*-- make space for the t-test computations --*/

   /* (allocate entire volumes) 13 Dec 2005 [rickr] */
                              npiece  = 3.0 ;  /* need at least this many */
   if( TT_paired )            npiece += 1.0 ;
   else if( TT_set1 != NULL ) npiece += 2.0 ;

   npiece += mri_datum_size(output_datum) / (float) sizeof(float) ;
   npiece += mri_datum_size(output_datum) / (float) sizeof(float) ;

#if 0
   piece_size = TT_workmem * MEGA / ( npiece * sizeof(float) ) ;
   if( piece_size > nxyz ) piece_size = nxyz ;

#ifdef TTDEBUG
printf("*** malloc-ing space for statistics: %g float arrays of length %d\n",
       npiece,piece_size) ;
#endif
#endif

   av2  = (float *) malloc( sizeof(float) * nxyz ) ; MTEST(av2) ;
   sd2  = (float *) malloc( sizeof(float) * nxyz ) ; MTEST(sd2) ;
   ffim = (float *) malloc( sizeof(float) * nxyz ) ; MTEST(ffim) ;
   num2 = TT_set2->num ;

   if( TT_paired ){
      av1  = sd1 = NULL ;
      gfim = (float *) malloc( sizeof(float) * nxyz ) ; MTEST(gfim) ;
      num1 = num2 ;
   } else if( TT_set1 != NULL ){
      av1  = (float *) malloc( sizeof(float) * nxyz ) ; MTEST(av1) ;
      sd1  = (float *) malloc( sizeof(float) * nxyz ) ; MTEST(sd1) ;
      gfim = NULL ;
      num1 = TT_set1->num ;
   } else {
      av1  = sd1 = NULL ;
      gfim = NULL ;
      num1 = 0 ;
   }

   vdif = (void *) malloc( mri_datum_size(output_datum) * nxyz ) ; MTEST(vdif) ;
   vsp  = (void *) malloc( mri_datum_size(output_datum) * nxyz ) ; MTEST(vsp)  ;

   /* 27 Dec 2002: make DOF dataset (if prefix is given, and unpooled is on) */

   if( TT_pooled == 0 && TT_dof_prefix[0] != '\0' ){
     dofbrik = (float *) malloc( sizeof(float) * nxyz ) ; MTEST(dofbrik) ;

     dof_dset = EDIT_empty_copy( new_dset ) ;

     tross_Make_History( "3dttest" , argc,argv , dof_dset ) ;

     EDIT_dset_items( dof_dset ,
                       ADN_prefix , TT_dof_prefix ,
                       ADN_directory_name , TT_session ,
                       ADN_type , ISHEAD(dset) ? HEAD_FUNC_TYPE : GEN_FUNC_TYPE,
                       ADN_func_type , FUNC_BUCK_TYPE ,
                       ADN_nvals , 1 ,
                       ADN_datum_all , MRI_float ,
                      ADN_none ) ;

     if( THD_is_file(dof_dset->dblk->diskptr->header_name) )
        ERROR_exit(
                "-dof_prefix dataset file %s already exists--cannot continue!\a",
                dof_dset->dblk->diskptr->header_name ) ;

     EDIT_substitute_brick( dof_dset , 0 , MRI_float , dofbrik ) ;
   }

   /* print out memory usage to edify the user */

   if( ! TT_be_quiet ){
      memuse =    sizeof(float) * nxyz * npiece
              + ( mri_datum_size(output_datum) + sizeof(short) ) * nxyz ;

      if( dofbrik != NULL ) memuse += sizeof(float) * nxyz ;  /* 27 Dec 2002 */

      printf("--- allocated %d Megabytes memory for internal use (%d volumes)\n",
             (int)(memuse/MEGA), (int)npiece) ;
   }

   mri_fix_data_pointer( vdif , DSET_BRICK(new_dset,0) ) ;  /* attach bricks */
   mri_fix_data_pointer( vsp  , DSET_BRICK(new_dset,1) ) ;  /* to new dataset */

   /** only short and float are allowed for output **/
   if( output_datum != MRI_short && output_datum != MRI_float )
      ERROR_exit("Illegal output data type %d = %s",
                 output_datum , MRI_TYPE_name[output_datum] ) ;

   num2_inv = 1.0 / num2 ;  num2m1_inv = 1.0 / (num2-1) ;
   if( num1 > 0 ){
      num1_inv = 1.0 / num1 ;  num1m1_inv = 1.0 / (num1-1) ;
   }

   /*----- loop over pieces to process the input datasets with -----*/

/** macro to open a dataset and make it ready for processing **/

#define DOPEN(ds,name)                                                            \
   do{ int pv ; (ds) = THD_open_dataset((name)) ;  /* 16 Sep 1999 */              \
       if( !ISVALID_3DIM_DATASET((ds)) )                                          \
          ERROR_exit("Can't open dataset: %s",(name)) ;                           \
       if( (ds)->daxes->nxx!=nx || (ds)->daxes->nyy!=ny || (ds)->daxes->nzz!=nz ) \
          ERROR_exit("Axes size mismatch: %s",(name)) ;                           \
       if( !EQUIV_GRIDS((ds),new_dset) )                                          \
          WARNING_message("Grid mismatch: %s",(name)) ;                           \
       if( DSET_NUM_TIMES((ds)) > 1 )                                             \
         ERROR_exit("Can't use time-dependent data: %s",(name)) ;                 \
       if( TT_use_editor ) EDIT_one_dataset( (ds), &TT_edopt ) ;                  \
       else                DSET_load((ds)) ;                                      \
       pv = DSET_PRINCIPAL_VALUE((ds)) ;                                          \
       if( DSET_ARRAY((ds),pv) == NULL )                                          \
          ERROR_exit("Can't access data: %s",(name)) ;                            \
       if( DSET_BRICK_TYPE((ds),pv) == MRI_complex )                              \
          ERROR_exit("Can't use complex data: %s",(name)) ;                       \
       break ; } while (0)

#if 0   /* can do it directly now (without offsets)  13 Dec 2005 [rickr] */
/** macro to return pointer to correct location in brick for current processing **/

#define SUB_POINTER(ds,vv,ind,ptr)                                            \
   do{ switch( DSET_BRICK_TYPE((ds),(vv)) ){                                  \
         default: ERROR_exit("Illegal datum! ***");                           \
            case MRI_short:{ short * fim = (short *) DSET_ARRAY((ds),(vv)) ;  \
                            (ptr) = (void *)( fim + (ind) ) ;                 \
            } break ;                                                         \
            case MRI_byte:{ byte * fim = (byte *) DSET_ARRAY((ds),(vv)) ;     \
                            (ptr) = (void *)( fim + (ind) ) ;                 \
            } break ;                                                         \
            case MRI_float:{ float * fim = (float *) DSET_ARRAY((ds),(vv)) ;  \
                             (ptr) = (void *)( fim + (ind) ) ;                \
            } break ; } break ; } while(0)
#endif

   /** number of pieces to process **/
   /* num_piece = (nxyz + piece_size - 1) / nxyz ; */

#if 0
   nice(2) ;  /** lower priority a little **/
#endif


   /* possibly open TT_base_dset now, and convert to floats */
   if( TT_base_dname ) {
      DOPEN(base_dset, TT_base_dname) ;
      base_ary = (float *) malloc( sizeof(float) * nxyz ) ; MTEST(base_ary) ;
      EDIT_coerce_scale_type(nxyz , DSET_BRICK_FACTOR(base_dset,0) ,
              DSET_BRICK_TYPE(base_dset,0),DSET_ARRAY(base_dset,0), /* input */
              MRI_float ,base_ary  ) ;                              /* output */
      THD_delete_3dim_dataset( base_dset , False ) ; base_dset = NULL ;
   }

   /* only 1 'piece' now   13 Dec 2005 [rickr] */
   for( piece=0 ; piece < 1 ; piece++ ){

      fim_offset = 0 ;

#ifdef TTDEBUG
printf("*** start of piece %d: length=%d offset=%d\n",piece,nxyz,fim_offset) ;
#else
      if( ! TT_be_quiet ){
         printf("--- starting piece %d/%d (%d voxels) ",piece+1,1,nxyz) ;
         fflush(stdout) ;
      }
#endif

      /** process set2 (and set1, if paired) **/

      for( ii=0 ; ii < nxyz ; ii++ ) av2[ii] = 0.0 ;
      for( ii=0 ; ii < nxyz ; ii++ ) sd2[ii] = 0.0 ;

      for( kk=0 ; kk < num2 ; kk++ ){

         /** read in the data **/

         DOPEN(dset,TT_set2->ar[kk]) ;
         iv = DSET_PRINCIPAL_VALUE(dset) ;

#ifndef TTDEBUG
         if( ! TT_be_quiet ){ printf(".") ; fflush(stdout) ; }  /* progress */
#else
         printf(" ** opened dataset file %s\n",TT_set2->ar[kk]);
#endif

#if 0 /* fimfac will be compute when the results are ready */
         if( piece == 0 && kk == 0 ){
            fimfac = DSET_BRICK_FACTOR(dset,iv) ;
            if( fimfac == 0.0 ) fimfac = 1.0 ;
            fimfacinv = 1.0 / fimfac ;
#ifdef TTDEBUG
printf(" ** set fimfac = %g\n",fimfac) ;
#endif
         }
#endif

         /** convert it to floats (in ffim) **/
         EDIT_coerce_scale_type(nxyz , DSET_BRICK_FACTOR(dset,iv) ,
                                DSET_BRICK_TYPE(dset,iv),DSET_ARRAY(dset,iv), /* input */
                                MRI_float ,ffim  ) ;                         /* output */
         THD_delete_3dim_dataset( dset , False ) ; dset = NULL ;

         /** get the paired dataset, if present **/

         if( TT_paired ){
            DOPEN(dset,TT_set1->ar[kk]) ;
            iv = DSET_PRINCIPAL_VALUE(dset) ;

#ifndef TTDEBUG
         if( ! TT_be_quiet ){ printf(".") ; fflush(stdout) ; }  /* progress */
#else
        printf(" ** opened dataset file %s\n",TT_set1->ar[kk]);
#endif

            EDIT_coerce_scale_type(
                        nxyz , DSET_BRICK_FACTOR(dset,iv) ,
                        DSET_BRICK_TYPE(dset,iv),DSET_ARRAY(dset,iv), /* input */
                        MRI_float ,gfim  ) ;                         /* output */
            THD_delete_3dim_dataset( dset , False ) ; dset = NULL ;

            if( TT_voxel >= 0 )
              fprintf(stderr,"-- paired values #%02d: %f, %f\n",
                      kk,ffim[TT_voxel],gfim[TT_voxel]) ;

            for( ii=0 ; ii < nxyz ; ii++ ) ffim[ii] -= gfim[ii] ;
         } else if( TT_voxel >= 0 )
            fprintf(stderr,"-- set2 value #%02d: %f\n",kk,ffim[TT_voxel]);

#ifdef TTDEBUG
printf("  * adding into av2 and sd2\n") ;
#endif

         /* accumulate into av2 and sd2 */

         for( ii=0 ; ii < nxyz ; ii++ ){
            dd = ffim[ii] ; av2[ii] += dd ; sd2[ii] += dd * dd ;
         }

      }  /* end of loop over set2 datasets */

      /** form the mean and stdev of set2 **/

#ifdef TTDEBUG
printf(" ** forming mean and sigma of set2\n") ;
#endif

      for( ii=0 ; ii < nxyz ; ii++ ){
         av2[ii] *= num2_inv ;
         dd       = (sd2[ii] - num2*av2[ii]*av2[ii]) ;
         sd2[ii]  = (dd > 0.0) ? sqrt( num2m1_inv * dd ) : 0.0 ;
      }
      if( TT_voxel >= 0 )
         fprintf(stderr,"-- s2 mean = %g, sd = %g\n",
                 av2[TT_voxel],sd2[TT_voxel]) ;

      /** if set1 exists but is not paired with set2, process it now **/

      if( ! TT_paired && TT_set1 != NULL ){

         for( ii=0 ; ii < nxyz ; ii++ ) av1[ii] = 0.0 ;
         for( ii=0 ; ii < nxyz ; ii++ ) sd1[ii] = 0.0 ;

         for( kk=0 ; kk < num1 ; kk++ ){
            DOPEN(dset,TT_set1->ar[kk]) ;
            iv = DSET_PRINCIPAL_VALUE(dset) ;

#ifndef TTDEBUG
         if( ! TT_be_quiet ){ printf(".") ; fflush(stdout) ; }  /* progress */
#else
         printf(" ** opened dataset file %s\n",TT_set1->ar[kk]);
#endif

            EDIT_coerce_scale_type(
                                nxyz , DSET_BRICK_FACTOR(dset,iv) ,
                                DSET_BRICK_TYPE(dset,iv),DSET_ARRAY(dset,iv), /* input */
                                MRI_float ,ffim  ) ;                         /* output */
            THD_delete_3dim_dataset( dset , False ) ; dset = NULL ;

#ifdef TTDEBUG
printf("  * adding into av1 and sd1\n") ;
#endif

            for( ii=0 ; ii < nxyz ; ii++ ){
               dd = ffim[ii] ; av1[ii] += dd ; sd1[ii] += dd * dd ;
            }
            if( TT_voxel >= 0 )
               fprintf(stderr,"-- set1 value #%02d: %g\n",kk,ffim[TT_voxel]) ;
         }  /* end of loop over set1 datasets */

         /** form the mean and stdev of set1 **/

#ifdef TTDEBUG
printf(" ** forming mean and sigma of set1\n") ;
#endif

         for( ii=0 ; ii < nxyz ; ii++ ){
            av1[ii] *= num1_inv ;
            dd       = (sd1[ii] - num1*av1[ii]*av1[ii]) ;
            sd1[ii]  = (dd > 0.0) ? sqrt( num1m1_inv * dd ) : 0.0 ;
         }
         if( TT_voxel >= 0 )
            fprintf(stderr,"-- s1 mean = %g, sd = %g\n",
                    av1[TT_voxel], sd1[TT_voxel]) ;
      }  /* end of processing set1 by itself */

      /***** now form difference and t-statistic *****/

#ifndef TTDEBUG
         if( ! TT_be_quiet ){ printf("+") ; fflush(stdout) ; }  /* progress */
#else
         printf(" ** computing t-tests next\n") ;
#endif

#if 0 /* will do at end using EDIT_convert_dtype  13 Dec 2005 [rickr] */

      /** macro to assign difference value to correct type of array **/
#define DIFASS switch( output_datum ){                                        \
                 case MRI_short: sdar[ii] = (short) (fimfacinv*dd) ; break ;  \
                 case MRI_float: fdar[ii] = (float) dd             ; break ; }
#define TOP_SS  32700
#define TOP_TT (32700.0/FUNC_TT_SCALE_SHORT)

#endif

      if( TT_paired || TT_use_bval == 1 ){ /** case 1: paired estimate or 1-sample **/

        if( TT_paired || TT_n1 == 0 ){       /* the olde waye: 1 sample test */
          f2 = 1.0 / sqrt( (double) num2 ) ;
          for( ii=0 ; ii < nxyz ; ii++ ){
            av2[ii] -= (base_ary ? base_ary[ii] : TT_bval) ;  /* final mean */
            if( sd2[ii] > 0.0 ){
               num_tt++ ;
               tt      = av2[ii] / (f2 * sd2[ii]) ;
               sd2[ii] = tt;      /* final t-stat */

               tt = fabs(tt) ; if( tt > tt_max ) tt_max = tt ;
            } else {
               sd2[ii] = 0.0;
            }
          }
          if( TT_voxel >= 0 )
             fprintf(stderr,"-- paired/bval mean = %g, t = %g\n",
                     av2[TT_voxel], sd2[TT_voxel]) ;

        } else {  /* 10 Oct 2007: -sdn1 was used with -base1: 'two' sample test */
          f1 = (TT_n1-1.0) * (1.0/TT_n1 + 1.0/num2) / (TT_n1+num2-2.0) ;
          f2 = (num2 -1.0) * (1.0/TT_n1 + 1.0/num2) / (TT_n1+num2-2.0) ;
          for( ii=0 ; ii < nxyz ; ii++ ){
            av2[ii] -= (base_ary ? base_ary[ii] : TT_bval) ;  /* final mean */
            q1 = f1 * TT_sd1*TT_sd1 + f2 * sd2[ii]*sd2[ii] ;
            if( q1 > 0.0 ){
              num_tt++ ;
              tt = av2[ii] / sqrt(q1) ;
              sd2[ii] = tt ;      /* final t-stat */
              tt = fabs(tt) ; if( tt > tt_max ) tt_max = tt ;
            } else {
              sd2[ii] = 0.0 ;
            }
          }
        } /* end of -sdn1 special case */
#ifdef TTDEBUG
printf(" ** paired or bval test: num_tt = %d\n",num_tt) ;
#endif

      } else if( TT_pooled ){ /** case 2: unpaired 2-sample, pooled variance **/

         f1 = (num1-1.0) * (1.0/num1 + 1.0/num2) / (num1+num2-2.0) ;
         f2 = (num2-1.0) * (1.0/num1 + 1.0/num2) / (num1+num2-2.0) ;
         for( ii=0 ; ii < nxyz ; ii++ ){
            av2[ii] -= av1[ii] ;        /* final mean */
            q1 = f1 * sd1[ii]*sd1[ii] + f2 * sd2[ii]*sd2[ii] ;
            if( q1 > 0.0 ){
               num_tt++ ;
               tt = av2[ii] / sqrt(q1) ;
               sd2[ii] = tt ;      /* final t-stat */

               tt = fabs(tt) ; if( tt > tt_max ) tt_max = tt ;
            } else {
               sd2[ii] = 0.0 ;
            }
         }

         if( TT_voxel >= 0 )
            fprintf(stderr,"-- unpaired, pooled mean = %g, t = %g\n",
                    av2[TT_voxel], sd2[TT_voxel]) ;
#ifdef TTDEBUG
printf(" ** pooled test: num_tt = %d\n",num_tt) ;
#endif

      } else { /** case 3: unpaired 2-sample, unpooled variance **/
               /** 27 Dec 2002: modified to save DOF into dofar **/

         if( dofbrik != NULL ) dofar = dofbrik + fim_offset ;  /* 27 Dec 2002 */

         for( ii=0 ; ii < nxyz ; ii++ ){
            av2[ii] -= av1[ii] ;
            q1 = num1_inv * sd1[ii]*sd1[ii] ;
            q2 = num2_inv * sd2[ii]*sd2[ii] ;
            if( q1>0.0 && q2>0.0 ){               /* have positive variances? */
               num_tt++ ;
               tt = av2[ii] / sqrt(q1+q2) ;
               sd2[ii] = tt ;      /* final t-stat */

               tt = fabs(tt) ; if( tt > tt_max ) tt_max = tt ;

               if( dofar != NULL )                             /* 27 Dec 2002 */
                 dofar[ii] =  (q1+q2)*(q1+q2)
                            / (num1m1_inv*q1*q1 + num2m1_inv*q2*q2) ;
            } else {
               sd2[ii] = 0.0 ;
               if( dofar != NULL ) dofar[ii] = 1.0 ;           /* 27 Dec 2002 */
            }
         }

         if( TT_voxel >= 0 )
            fprintf(stderr,"-- unpaired, unpooled mean = %g, t = %g\n",
                    av2[TT_voxel], sd2[TT_voxel]) ;
#ifdef TTDEBUG
printf(" ** unpooled test: num_tt = %d\n",num_tt) ;
#endif
      }

#ifndef TTDEBUG
         if( ! TT_be_quiet ){ printf("\n") ; fflush(stdout) ; }
#endif

   }  /* end of loop over pieces of the input */

   if( TT_paired ){
      printf("--- Number of degrees of freedom = %d (paired test)\n",num2-1) ;
      dof = num2 - 1 ;
   } else if( TT_use_bval == 1 ){
      if( TT_n1 == 0 ){
        printf("--- Number of degrees of freedom = %d (1-sample test)\n",num2-1) ;
        dof = num2 - 1 ;
      } else {
        dof = TT_n1+num2-2 ;
        printf("--- Number of degrees of freedom = %d (-sdn1 2-sample test)\n",(int)dof) ;
      }
   } else {
      printf("--- Number of degrees of freedom = %d (2-sample test)\n",num1+num2-2) ;
      dof = num1+num2-2 ;
      if( ! TT_pooled )
         printf("    (For unpooled variance estimate, this is only approximate!)\n") ;
   }

   printf("--- Number of t-tests performed  = %d out of %d voxels\n",num_tt,nxyz) ;
   printf("--- Largest |t| value found      = %g\n",tt_max) ;

   kk = sizeof(ptable) / sizeof(float) ;
   for( ii=0 ; ii < kk ; ii++ ){
      tt = student_p2t( ptable[ii] , dof ) ;
      printf("--- Double sided tail p = %8f at t = %8f\n" , ptable[ii] , tt ) ;
   }

   /**----------------------------------------------------------------------**/
   /** now convert data to output format                13 Dec 2005 [rickr] **/

   /* first set mean */
   fimfac = EDIT_convert_dtype(nxyz , MRI_float,av2 , output_datum,vdif , 0.0) ;
   DSET_BRICK_FACTOR(new_dset, 0) = (fimfac != 0.0) ? 1.0/fimfac : 0.0 ;
   dd = fimfac; /* save for debug output */

   /* if output is of type short, limit t-stat magnitude to 32.7 */
   if( output_datum == MRI_short ){
     for( ii=0 ; ii < nxyz ; ii++ ){
       if     ( sd2[ii] >  32.7 ) sd2[ii] =  32.7 ;
       else if( sd2[ii] < -32.7 ) sd2[ii] = -32.7 ;
     }
   }

   fimfac = EDIT_convert_dtype(nxyz , MRI_float,sd2 , output_datum,vsp , 0.0) ;
   DSET_BRICK_FACTOR(new_dset, 1) = (fimfac != 0.0) ? 1.0/fimfac : 0.0 ;

#ifdef TTDEBUG
printf(" ** fimfac for mean, t-stat = %g, %g\n",dd, fimfac) ;
#endif
   /**----------------------------------------------------------------------**/

   INFO_message("Writing combined dataset into %s\n", DSET_BRIKNAME(new_dset) ) ;

   fbuf[0] = dof ;
   for( ii=1 ; ii < MAX_STAT_AUX ; ii++ ) fbuf[ii] = 0.0 ;
   (void) EDIT_dset_items( new_dset , ADN_stat_aux , fbuf , ADN_none ) ;

#if 0 /* factors already set */
   fbuf[0] = (output_datum == MRI_short && fimfac != 1.0 ) ? fimfac                    : 0.0 ;
   fbuf[1] = (output_datum == MRI_short                  ) ? 1.0 / FUNC_TT_SCALE_SHORT : 0.0 ;
   (void) EDIT_dset_items( new_dset , ADN_brick_fac , fbuf , ADN_none ) ;
#endif

   if( !AFNI_noenv("AFNI_AUTOMATIC_FDR") ) ii = THD_create_all_fdrcurves(new_dset) ;
   else                                    ii = 0 ;
   THD_load_statistics( new_dset ) ;
   THD_write_3dim_dataset( NULL,NULL , new_dset , True ) ;
   if( ii > 0 ) ININFO_message("created %d FDR curves in header",ii) ;

   if( dof_dset != NULL ){                                  /* 27 Dec 2002 */
     DSET_write( dof_dset ) ;
     WROTE_DSET( dof_dset ) ;
   }

   exit(0) ;
}
コード例 #7
0
ファイル: 3dFDR.c プロジェクト: CesarCaballeroGaudes/afni
void process_volume (float * ffim, int statcode, float * stataux)

{
  int ixyz;                      /* voxel index */
  int icount;                    /* count of sorted p-values */
  float fval;                    /* voxel input statistical value */
  float pval;                    /* voxel input stat. p-value */
  float qval;                    /* voxel FDR q-value */
  float zval;                    /* voxel FDR z-score */
  float qval_min;                /* smallest previous q-value */
  voxel * head_voxel = NULL;     /* linked list of voxels */
  voxel * voxel_ptr  = NULL;     /* pointer to current voxel */
  int ibin;                      /* p-value bin */
  int   * iarray = NULL;         /* output array of voxel indices */
  float * parray = NULL;         /* output array of voxel p-values */
  float * qarray = NULL;         /* output array of voxel FDR q-values */
  float * zarray = NULL;         /* output array of voxel FDR z-scores */

  float numer ;


  /*------------ 18 Jan 2008: use the 'new' method? ------------*/

  if( FDR_old < 1 ){
    MRI_IMAGE *qim ; int flags=0 ;
    qim = mri_new_vol_empty( FDR_nxyz,1,1 , MRI_float ) ;
    mri_fix_data_pointer( ffim , qim ) ;
    if( FDR_mask != NULL ){
      float zz = (FUNC_IS_STAT(statcode)) ? 0.0f : 1.0f ;
      for( ixyz=0 ; ixyz < FDR_nxyz ; ixyz++ )
        if( !FDR_mask[ixyz] ) ffim[ixyz] = zz ;
    }
    if( FDR_curve ){ /* hidden option: produce t-z curve */
      floatvec *fv = mri_fdr_curve( qim , statcode , stataux ) ;
      if( fv == NULL ) ERROR_message("mri_fdr_curve fails!") ;
      else {
        printf("# FDR thresh-z curve\n") ;
        for( ixyz=0 ; ixyz < fv->nar ; ixyz++ )
          printf("%g %g\n", fv->x0+ixyz*fv->dx , fv->ar[ixyz] ) ;
      }
      exit(0) ;
    } else {         /* normal operation: convert to z(q) or q */
      if( FDR_pmask == 0    ) flags |= 1 ;  /* compatibility mode */
      if( FDR_cn    >  1.0f ) flags |= 2 ;  /* dependency flag */
      if( FDR_qval          ) flags |= 4 ;  /* qval flag */
      (void)mri_fdrize( qim , statcode,stataux , flags ) ;
    }
    mri_clear_data_pointer(qim); mri_free(qim);
    return ;
  }

  /*---------------- back to the 'old' method ------------------*/
  
  /*----- Allocate memory for screen output arrays -----*/
  if (FDR_list)
    {
      iarray = (int   *) malloc (sizeof(int)   * FDR_nthr);   MTEST(iarray);
      parray = (float *) malloc (sizeof(float) * FDR_nthr);   MTEST(parray);
      qarray = (float *) malloc (sizeof(float) * FDR_nthr);   MTEST(qarray);
      zarray = (float *) malloc (sizeof(float) * FDR_nthr);   MTEST(zarray);
    }
  
  
  /*----- Loop over all voxels; sort p-values -----*/
  icount = FDR_nthr;

  for (ixyz = 0;  ixyz < FDR_nxyz;  ixyz++)
    {

      /*----- First, check if voxel is inside the mask -----*/
      if( FDR_mask != NULL && !FDR_mask[ixyz] ) continue;


      /*----- Convert stats to p-values -----*/
      fval = fabs(ffim[ixyz]);
      if (statcode <= 0)
	pval = fval;
      else
	pval = THD_stat_to_pval (fval, statcode, stataux);

      if (pval >= 1.0)  
	{
	  /*----- Count but don't sort voxels with p-value = 1 -----*/
	  icount--;
	  if (FDR_list)
	    {
	      iarray[icount] = ixyz;
	      parray[icount] = 1.0;
	      qarray[icount] = 1.0;
	      zarray[icount] = 0.0;
	    }
	}
      else
	{ 
	  /*----- Place voxel in p-value bin -----*/
	  ibin = (int)  (pval * (FDR_MAX_LL));
	  if (ibin < 0)  ibin = 0;
	  if (ibin > FDR_MAX_LL-1)  ibin = FDR_MAX_LL-1;
	  head_voxel = new_voxel (ixyz, pval, FDR_head_voxel[ibin]);
	  FDR_head_voxel[ibin] = head_voxel;
	}
    }

  /*----- Calculate FDR q-values -----*/
  qval_min = 1.0;
  ibin = FDR_MAX_LL-1;
  numer = (FDR_pmask) ? icount : FDR_nthr ;  /* 18 Jan 2008 */
  while (ibin >= 0) 
    {
      voxel_ptr = FDR_head_voxel[ibin];
  
      while (voxel_ptr != NULL)
	{
          /*----- Convert sorted p-values to FDR q-values -----*/
	  pval = voxel_ptr->pvalue;
	  qval = FDR_cn * (pval*numer) / icount;
	  if (qval > qval_min)
	    qval = qval_min;
	  else
	    qval_min = qval;

	  /*----- Convert FDR q-value to FDR z-score -----*/
          if( !FDR_qval ){
            if (qval < 1.0e-20) zval = 10.0;
            else                zval = normal_p2t(qval);
          } else {              zval = qval ; }

	  icount--;

	  /*----- Save calculated values -----*/
	  if (FDR_list)
	    {
	      iarray[icount] = voxel_ptr->ixyz;
	      parray[icount] = pval;
	      qarray[icount] = qval;
	      zarray[icount] = zval;
	    }

	  voxel_ptr->pvalue = zval;
	  voxel_ptr = voxel_ptr->next_voxel;
	}

      ibin--;
    }


  /*----- Write out the calculated values -----*/
  if (FDR_list)
    {
      printf ("%12s %12s %12s %12s \n", 
	      "Index", "p-value", "q-value", "z-score");
      for (icount = 0;  icount < FDR_nthr;  icount++)
	{
	  if (FDR_input1D_filename != NULL)
	    ixyz = iarray[icount] + 1;
	  else
	    ixyz = iarray[icount];
	  printf ("%12d %12.6f %12.6f %12.6f \n",  
		  ixyz, parray[icount], qarray[icount], zarray[icount]);
	}

      /*----- Deallocate memory for output arrays -----*/
      free (iarray);   free (parray);   free (qarray);   free (zarray);
    }


  /*----- Place FDR z-scores into float array -----*/
  save_all_voxels (ffim);


  /*----- Deallocate linked-list memory -----*/
  delete_all_voxels();

}
コード例 #8
0
ファイル: 3dFDR.c プロジェクト: CesarCaballeroGaudes/afni
void initialize_program (int argc, char * argv[])
{
  int iv;                  /* index number of sub-brick */
  void * vfim = NULL;      /* sub-brick data pointer */
  float * ffim = NULL;     /* sub-brick data in floating point format */
  int ixyz;                /* voxel index */
  int nx, ny, nz, nxyz;    /* numbers of voxels in input dataset */
  int mx=0, my=0, mz=0, mxyz;    /* numbers of voxels in mask dataset */
  int nthr=0;                /* number of voxels above mask threshold */
  char message[80];        /* error message */
  int ibin;                /* p-value bin index */


  /*-- 20 Apr 2001: addto the arglist, if user wants to [RWCox] --*/
  machdep() ; 
  { int new_argc ; char ** new_argv ;
  addto_args( argc , argv , &new_argc , &new_argv ) ;
  if( new_argv != NULL ){ argc = new_argc ; argv = new_argv ; }
  }
  

  /*----- Save command line for history notes -----*/
  commandline = tross_commandline( PROGRAM_NAME , argc,argv ) ;


  /*----- Does user request help menu? -----*/
  if( argc < 2 || strcmp(argv[1],"-help") == 0 ) FDR_Syntax() ;

  
  /*----- Add to program log -----*/
  AFNI_logger (PROGRAM_NAME,argc,argv); 


  /*----- Read input options -----*/
  read_options( argc , argv ) ;


  /*----- Open the mask dataset -----*/
  if (FDR_mask_filename != NULL)
    {
      if (!FDR_quiet) 
        printf ("Reading mask dataset: %s \n", FDR_mask_filename);
      DOPEN (FDR_dset, FDR_mask_filename);

      if (FDR_dset == NULL)
      {
        sprintf (message, "Cannot open mask dataset %s", FDR_mask_filename); 
        FDR_error (message);
      }

      if (DSET_NVALS(FDR_dset) != 1)
        WARNING_message("Mask dataset: using sub-brick #0") ;


      /*----- Get dimensions of mask dataset -----*/
      mx   = DSET_NX(FDR_dset);   
      my   = DSET_NY(FDR_dset);   
      mz   = DSET_NZ(FDR_dset);
      mxyz = mx*my*mz;


      /*----- Allocate memory for float data -----*/
      ffim = (float *) malloc (sizeof(float) * mxyz);   MTEST (ffim);


      /*----- Convert mask dataset sub-brick to floats (in ffim) -----*/
      iv = 0 ;
      SUB_POINTER (FDR_dset, iv, 0, vfim);
      EDIT_coerce_scale_type (mxyz, DSET_BRICK_FACTOR(FDR_dset,iv),
			      DSET_BRICK_TYPE(FDR_dset,iv), vfim,  /* input  */
			      MRI_float                   , ffim); /* output */
  
      
      /*----- Allocate memory for mask volume -----*/
      FDR_mask = (byte *) malloc (sizeof(byte) * mxyz);
      MTEST (FDR_mask);
      
      
      /*----- Create mask of voxels above mask threshold -----*/
      nthr = 0;
      for (ixyz = 0;  ixyz < mxyz;  ixyz++){
        if (fabs(ffim[ixyz]) >= FDR_mask_thr){ FDR_mask[ixyz] = 1; nthr++; }
        else                                   FDR_mask[ixyz] = 0;
      }

      if (!FDR_quiet)  
        printf ("Number of voxels above mask threshold = %d \n", nthr);
      if (nthr < 1)  
        FDR_error ("No voxels above mask threshold.  Cannot continue.");


      /*----- Delete floating point sub-brick -----*/
      if (ffim != NULL) { free (ffim);   ffim = NULL; }

      /*----- Delete mask dataset -----*/
      THD_delete_3dim_dataset (FDR_dset, False);  FDR_dset = NULL ;

    }


  /*----- Get the input data -----*/

  if (FDR_input1D_filename != NULL)
    {
      /*----- Read the input .1D file -----*/
      if (!FDR_quiet)  printf ("Reading input data: %s \n", 
			       FDR_input1D_filename);
      FDR_input1D_data = read_time_series (FDR_input1D_filename, &nxyz);

      if (FDR_input1D_data == NULL)  
	{ 
	  sprintf (message,  "Unable to read input .1D data file: %s", 
		   FDR_input1D_filename);
	  FDR_error (message);
	}
      
      if (nxyz < 1)  
	{ 
	  sprintf (message,  "No p-values in input .1D data file: %s", 
		   FDR_input1D_filename);
	  FDR_error (message);
	}

      FDR_nxyz = nxyz;
      FDR_nthr = nxyz;
    }
  
  else
    {
      /*----- Open the input 3D dataset -----*/
      if (!FDR_quiet)  printf ("Reading input dataset: %s \n", 
			       FDR_input_filename);
      FDR_dset = THD_open_dataset(FDR_input_filename);
      CHECK_OPEN_ERROR(FDR_dset,FDR_input_filename);
      
      /*----- Get dimensions of input dataset -----*/
      nx   = DSET_NX(FDR_dset);   
      ny   = DSET_NY(FDR_dset);   
      nz   = DSET_NZ(FDR_dset);
      nxyz = nx*ny*nz;
      
      
      /*----- Check for compatible dimensions -----*/
      if (FDR_mask != NULL)
	{
	  if ((nx != mx) || (ny != my) || (nz != mz))
	    FDR_error ("Mask and input dataset have incompatible dimensions");
	  FDR_nxyz = nxyz;
	  FDR_nthr = nthr;
	}
      else
	{
	  FDR_nxyz = nxyz;
	  FDR_nthr = nxyz;
	}


      /*----- Check whether output dataset already exists -----*/
      if( THD_deathcon() ) check_one_output_file (FDR_dset, FDR_output_prefix);
    }


  /*----- Initialize constant c(N) -----*/
  if (FDR_cn < 0.0)
    {
      double cn;
      cn = 0.0;
      for (ixyz = 1;  ixyz <= FDR_nthr;  ixyz++)
	cn += 1.0 / ixyz;
      FDR_cn = cn;
      if (!FDR_quiet)
	printf ("c(N) = %f \n", FDR_cn);
    }
  
  /*----- Initialize voxel pointers -----*/
  for (ibin = 0;  ibin < FDR_MAX_LL;  ibin++)
    FDR_head_voxel[ibin] = NULL;

  return ;
}
コード例 #9
0
ファイル: 3dFDR.c プロジェクト: CesarCaballeroGaudes/afni
void read_options ( int argc , char * argv[] )
{
  int nopt = 1 ;           /* count of input arguments */
  char message[80];        /* error message */


  if( AFNI_yesenv("AFNI_FLOATIZE") ) FDR_float = 1 ;
  
  /*----- main loop over input options -----*/
  while( nopt < argc )
    {

      /*-----   -input fname   -----*/
      if (strcmp(argv[nopt], "-input") == 0)
	{
	  nopt++;
	  if (nopt >= argc)  FDR_error ("need argument after -input ");
	  FDR_input_filename = (char *) malloc (sizeof(char)*THD_MAX_NAME);
	  MTEST (FDR_input_filename);
	  strcpy (FDR_input_filename, argv[nopt]);
	  nopt++;
	  continue;
	}
      
      
      /*-----   -input1D dname   -----*/
      if (strcmp(argv[nopt], "-input1D") == 0)
	{
	  nopt++;
	  if (nopt >= argc)  FDR_error ("need argument after -input1D ");
	  FDR_input1D_filename = (char *) malloc (sizeof(char)*THD_MAX_NAME);
	  MTEST (FDR_input1D_filename);
	  strcpy (FDR_input1D_filename, argv[nopt]);
	  nopt++;
	  continue;
	}
      
      
      /*-----   -mask_file mname   -----*/
      if (strcmp(argv[nopt], "-mask_file") == 0 || strcmp(argv[nopt],"-mask") == 0)
	{
	  nopt++;
	  if (nopt >= argc)  FDR_error ("need argument after -mask_file ");
	  FDR_mask_filename = (char *) malloc (sizeof(char)*THD_MAX_NAME);
	  MTEST (FDR_mask_filename);
	  strcpy (FDR_mask_filename, argv[nopt]);
	  nopt++;
	  continue;
	}
      

      /*----- -mask_thr m -----*/
      if( strcmp(argv[nopt],"-mask_thr") == 0 ){
	 float fval;
         nopt++ ;
         if( nopt >= argc ){
            FDR_error (" need 1 argument after -mask_thr"); 
         }
	 sscanf (argv[nopt], "%f", &fval); 
	 if (fval < 0.0){
            FDR_error (" Require mask_thr >= 0.0 ");
         }
	 FDR_mask_thr = fval;
	 nopt++;  continue;
      }

      /*---- -force & -old & -pmask & -float etc. [18 Jan 2008] -----*/

      if( strcmp(argv[nopt],"-force") == 0 ){
        FDR_force = 1 ; nopt++ ; continue ;
      }
      if( strcmp(argv[nopt],"-qval") == 0 ){
        FDR_qval = 1 ; nopt++ ; continue ;
      }
      if( strcmp(argv[nopt],"-old") == 0 ){
        FDR_old = 1 ; FDR_pmask = 0 ; nopt++ ; continue ;
      }
      if( strcmp(argv[nopt],"-new") == 0 ){
        FDR_old = -1 ; FDR_pmask = 1 ; nopt++ ; continue ;
      }
      if( strcmp(argv[nopt],"-pmask") == 0 ){
        FDR_pmask = 1 ; nopt++ ; continue ;
      }
      if( strcmp(argv[nopt],"-nopmask") == 0 ){
        FDR_pmask = 0 ; nopt++ ; continue ;
      }
      if( strcmp(argv[nopt],"-float") == 0 ){
        FDR_float = 1 ; nopt++ ; continue ;
      }
#if 0
      if( strcmp(argv[nopt],"-curve") == 0 ){  /* hidden option */
        FDR_curve = 1 ; nopt++ ; continue ;
      }
#endif

      /*----- -cind -----*/
      if( strcmp(argv[nopt],"-cind") == 0 ){
         FDR_cn = 1.0;
         nopt++ ; continue ;
      }

      
      /*----- -cdep -----*/
      if( strcmp(argv[nopt],"-cdep") == 0 ){
         FDR_cn = -1.0;
         nopt++ ; continue ;
      }

      
      /*----- -quiet -----*/
      if( strcmp(argv[nopt],"-quiet") == 0 ){
         FDR_quiet = 1;
         nopt++ ; continue ;
      }
      if( strcmp(argv[nopt],"-verb") == 0 ){
         FDR_quiet = 0 ; nopt++ ; continue ;
      }

      
      /*----- -list -----*/
      if( strcmp(argv[nopt],"-list") == 0 ){
         FDR_list = 1;
         nopt++ ; continue ;
      }

      
      /*----- -prefix prefix -----*/
      if( strcmp(argv[nopt],"-prefix") == 0 ||
          strcmp(argv[nopt],"-output") == 0   ){
         nopt++ ;
         if( nopt >= argc ){
            FDR_error (" need argument after -prefix!");
         }
	 FDR_output_prefix = (char *) malloc (sizeof(char) * THD_MAX_PREFIX); 
         MCW_strncpy( FDR_output_prefix , argv[nopt++] , THD_MAX_PREFIX ) ;
         continue ;
      }


      /*----- unknown command -----*/
      sprintf(message,"Unrecognized command line option: %s\n", argv[nopt]);
      FDR_error (message);
      

   }  /*----- end of loop over command line arguments -----*/


   if( FDR_old == 0 && FDR_pmask ){  /* the new way is on by default */
     fprintf(stderr,"\n"
       "++ The 'new' method of FDR calculation is on by default; in particular:\n"
       " + * Voxel p-values of exactly 1 (e.g., from t=0 or F=0 or correlation=0)\n"
       " +   are ignored by default; in the old mode of operation, they were\n"
       " +   included in the count which goes into the FDR algorithm.  The old\n"
       " +   process tends to increase the q-values and so decrease the z-scores.\n"
       " + * If you wish to do the FDR conversion using the old mode, use '-old'\n"
       " +   on the command line.  For more information, use '3dFDR -help'.\n"
       " + * If you don't want to see this message again, use the '-new' option.\n"
       "++ RWCox - 18 Jan 2008\n\n"
     ) ;
   }

   if( FDR_old < 1 && FDR_pmask == 0 && FDR_mask != NULL ){  /* 29 Jan 2008 */
     fprintf(stderr,"\n"
       "++ In the 'new' method of FDR calculation, options '-nopmask' and\n"
       " +  -mask_file are incompatible.  Am now turning '-pmask' back on\n"
       " +  so that the mask can be used.\n"
       "++ RWCox - 29 Jan 2008\n\n"
     ) ;
     FDR_pmask = 1 ;
   }

   return ;
}
コード例 #10
0
void print2D_current(TString caseAna, TString channel, TString fsrFlag, TString accFlag, TString norm_type){

  double sigma_Z = 524.699999999999932;
  if (fsrFlag == "post") sigma_Z = 516.5;

  cout << "Reading " << "covariance_finalResults_2D_mu_"+fsrFlag+"FSR_"+accFlag+"_normalized.root" << endl;
  TFile fin("covariance_finalResults_2D_mu_"+fsrFlag+"FSR_"+accFlag+"_normalized.root","read");
  TMatrixD *Morig=(TMatrixD*)fin.Get("totalCov_TM");
  TMatrixD *Corig=(TMatrixD*)fin.Get("totalCorr_TM");
  fin.Close();

  // copy
  TMatrixD M(*Morig);
  TMatrixD MAlexey(*Morig);
  TMatrixD MTEST(*Morig);
  delete Morig; // no longer need original

  // get errors to apply the correlation cut-off
  int dim=M.GetNcols();
  TVectorD S(dim);

  for (int i=0; i<dim; i++) {
    S[i]=sqrt(M(i,i));
  }

  //
  // Now ready to print the matrix
  //

  // define ranges
  const int nMassBins=6;
  const int nYBins[nMassBins]={ 24, 24, 24, 24, 24, 12 };
  const int massLimits[nMassBins+1] = { 20, 30, 45, 60, 120, 200, 1500 };

  double diagonals_preFSR[132] = {0.00220474, 0.00211757, 0.00208329, 0.00196359, 0.00188362, 0.00178958, 0.00170293, 0.00147753, 0.00132556, 0.00123584, 0.00114805, 0.00112789, 0.00114458, 0.00130523, 0.00135017, 0.00140954, 0.0014109, 0.00147887, 0.00140234, 0.00126359, 0.00112224, 0.000942219, 0.000836654, 0.000407358, 0.00178671, 0.001573, 0.00152372, 0.00145345, 0.00142417, 0.00143499, 0.00144411, 0.00147708, 0.00148128, 0.00142131, 0.00140812, 0.00131649, 0.0012678, 0.0012579, 0.00119449, 0.00116062, 0.0011051, 0.00101312, 0.00100221, 0.000896522, 0.000852432, 0.000762897, 0.000637827, 0.000318889,0.000619175, 0.00063628, 0.000683457, 0.000663991, 0.000833326, 0.000576589, 0.000604494, 0.000665053, 0.000562349, 0.000571806, 0.000574754, 0.000641049, 0.000559305, 0.000510976, 0.000465562, 0.000552576, 0.000426906, 0.000394093, 0.000473651, 0.000490072, 0.000487637, 0.000310961, 0.000382431, 0.000199051, 0.00820416, 0.00817875, 0.00823994, 0.00826059, 0.0083192, 0.00826397, 0.00818159, 0.00799829, 0.0079163, 0.00798274, 0.00788228, 0.00776324, 0.00757453, 0.00723493, 0.00688138, 0.00638258, 0.00615382, 0.00560927, 0.00511678, 0.00477886, 0.00480081, 0.00446441, 0.0035405, 0.00140224, 0.000313394, 0.000313743, 0.000317145, 0.000302152, 0.000274664, 0.000282591, 0.000290322, 0.000263838, 0.000275426, 0.000243859, 0.000232308, 0.000223118, 0.000221724, 0.000214459, 0.000212437, 0.000205239, 0.000199126, 0.000188036, 0.000177693, 0.000186241, 0.000147574, 0.000143643, 0.00010251, 5.09799e-05, 0.000139172, 0.000115512, 0.000103777, 9.132e-05, 7.77451e-05, 7.36202e-05, 6.34563e-05, 5.60115e-05, 5.03875e-05, 4.09827e-05, 3.82219e-05, 1.55491e-05};
  double rdiagonals_preFSR[132] = {0.0339971, 0.0337208, 0.032773, 0.0335877, 0.0340049, 0.0344678, 0.0351017, 0.0345766, 0.0344008, 0.0339871, 0.0334087, 0.0332816, 0.0322232, 0.0322838, 0.0310592, 0.0313493, 0.0294624, 0.0289783, 0.0268042, 0.0240434, 0.0209985, 0.0160163, 0.0104074, 0.00394112, 0.0512196, 0.0488462, 0.049328, 0.0489294, 0.0497668, 0.0516309, 0.0498744, 0.0488812, 0.0501116, 0.0487699, 0.0490278, 0.0486872, 0.0467183, 0.049095, 0.0459175, 0.0444989, 0.0413405, 0.0367446, 0.0315492, 0.0262498, 0.0209891, 0.0150392, 0.00890852, 0.00310112, 0.0213773, 0.0213444, 0.021062, 0.0216232, 0.020339, 0.0222888, 0.0218933, 0.0214533, 0.0215288, 0.0212879, 0.0205312, 0.0196386, 0.0185561, 0.0186806, 0.0170245, 0.0159697, 0.0140271, 0.0122443, 0.0112471, 0.00938589, 0.0077171, 0.00489739, 0.00342707, 0.00116623,0.604588, 0.600796, 0.601957, 0.596068, 0.595779, 0.590047, 0.584797, 0.57086, 0.557382, 0.54488, 0.526063, 0.503772, 0.475897, 0.445807, 0.417606, 0.374982, 0.339285, 0.292885, 0.250475, 0.20262, 0.156481, 0.108768, 0.0637553, 0.0202485,
0.00662107, 0.0068695, 0.00674016, 0.00671329, 0.006447, 0.00671166, 0.00630126, 0.00632929, 0.00613147, 0.00581744, 0.00577516, 0.00531591, 0.00516504, 0.00479951, 0.00464618, 0.00416784, 0.00371484, 0.0030139, 0.00238956, 0.00220813, 0.00138641, 0.000996914, 0.000525727, 0.000204149, 0.00100991, 0.00115943, 0.00124304, 0.000994088, 0.00101695, 0.00100447, 0.000906887, 0.000616144, 0.000469744, 0.000288772, 0.000184214, 4.10414e-05};

  double diagonals_postFSR[132] = {0.00221175, 0.00212597, 0.00208369, 0.00198295, 0.00190802, 0.00182345, 0.00173207, 0.00152478, 0.00137675, 0.00128194, 0.00120738, 0.00120436, 0.00121263, 0.00136807, 0.00139429, 0.00146451, 0.00146562, 0.00152889, 0.00145013, 0.00130909, 0.00114496, 0.000954991, 0.000795812, 0.000393729, 0.00180454, 0.00159939, 0.00153431, 0.0014354, 0.00142042, 0.00147654, 0.00148879, 0.00150211, 0.00153454, 0.00146574, 0.00145038, 0.00138594, 0.00132894, 0.00133732, 0.00126548, 0.00123125, 0.00114978, 0.00107037, 0.00102759, 0.000934409, 0.000851672, 0.000725795, 0.00065131, 0.000296067, 0.000737911, 0.000735332, 0.000724041, 0.000716565, 0.000688831, 0.000705611, 0.000704845, 0.000703817, 0.00069591, 0.000703824, 0.000680863, 0.000665061, 0.000653354, 0.000634177, 0.00058112, 0.000541761, 0.00050425, 0.000485744, 0.000442929, 0.00040406, 0.000364285, 0.000309282, 0.000245493, 0.000136572, 0.0080247, 0.00800476, 0.00806034, 0.00808799, 0.00815336, 0.00810854, 0.00802642, 0.00785401, 0.00778262, 0.00780448, 0.00776131, 0.00766702, 0.00747821, 0.00715004, 0.00681454, 0.00633735, 0.00606506, 0.00554794, 0.00507419, 0.0048055, 0.0047721, 0.00445233, 0.00353591, 0.00137453, 0.00034878, 0.000349607, 0.00034785, 0.00033325, 0.000310629, 0.000321449, 0.000324078, 0.000303162, 0.000315696, 0.000284189, 0.000275219, 0.000264221, 0.000260037, 0.000252548, 0.00025174, 0.000243753, 0.000234663, 0.000214962, 0.000204784, 0.00020642, 0.000165171, 0.000152023, 0.000113424, 6.24019e-05, 0.000150168, 0.000129036, 0.000117808, 0.000102805, 9.06346e-05, 8.79511e-05, 7.67355e-05, 6.53114e-05, 5.9421e-05, 4.99151e-05, 4.55147e-05, 1.97611e-05};

  double rdiagonals_postFSR[132] = {0.0336475, 0.0332778, 0.0326289, 0.0332891, 0.0337836, 0.0342364, 0.034789, 0.0342068, 0.0340713, 0.0336801, 0.0329994, 0.0330048, 0.031868, 0.0319083, 0.0308544, 0.0309242, 0.0290639, 0.0285542, 0.02643, 0.0235605, 0.020521, 0.0155586, 0.0100116, 0.00371247, 0.0515544, 0.0491292, 0.0497977, 0.0493683, 0.0501341, 0.0520321, 0.0501604, 0.0493229, 0.0503617, 0.049072, 0.0493856, 0.0488609, 0.0470033, 0.0491916, 0.0459866, 0.0445097, 0.0411828, 0.0365744, 0.0313967, 0.0260833, 0.0207742, 0.0148265, 0.00878173, 0.00297568,
0.0256592, 0.0257041, 0.0253218, 0.0260294, 0.0248893, 0.0266365, 0.0262303, 0.0256931, 0.0256673, 0.0252969, 0.0246081, 0.0236905, 0.0221783, 0.02201, 0.02022, 0.0187476, 0.0164779, 0.0144995, 0.0130875, 0.0109181, 0.00889321, 0.00575018, 0.00387918, 0.00127005,
0.602668, 0.59906, 0.600088, 0.594422, 0.594111, 0.588837, 0.583597, 0.569714, 0.556393, 0.543969, 0.525012, 0.502787, 0.475247, 0.445218, 0.416989, 0.374532, 0.338999, 0.29251, 0.250117, 0.202428, 0.156312, 0.108611, 0.0636249, 0.0201009,
0.00631477, 0.00651359, 0.00642427, 0.00636077, 0.00614697, 0.0063431, 0.00597265, 0.00601271, 0.00586154, 0.00557037, 0.00550196, 0.00508115, 0.0049044, 0.00452822, 0.00440214, 0.00397127, 0.00353436, 0.0028604, 0.00227513, 0.00209037, 0.00131489, 0.000937039, 0.000501518, 0.000191148,
0.000957856, 0.00111243, 0.00119348, 0.00095393, 0.000971326, 0.000964124, 0.000861841, 0.000586147, 0.000449087, 0.000277411, 0.000177376, 3.9162e-05};


  double sigmaZerrNoLumi = 0.;
  TString accFlag1 = accFlag;
  if (accFlag == "fullAcc") accFlag1= "fullSpace";

  if (fsrFlag == "pre" && accFlag == "inAcc") sigmaZerrNoLumi = pow(0.4,2) +pow(5.1,2) +pow(1.2,2);
  if (fsrFlag == "post" && accFlag == "inAcc")   sigmaZerrNoLumi =pow(0.4,2) +pow(4.9,2) +pow(1.1,2);
  int fi_min=0;
  for (int iM=0; iM<nMassBins; iM++) {
    int fj_min=fi_min;
    for (int jM=iM; jM<nMassBins; ++jM) {
      TString fname=Form("cov_%d_%d_vs_%d_%d_%s_%s_%s.out",
			 massLimits[iM],massLimits[iM+1],
			 massLimits[jM],massLimits[jM+1],string(fsrFlag).c_str(),string(accFlag).c_str(),string(norm_type).c_str());
      std::ofstream fout(fname.Data());
      fout << "Covariances of the Drell-Yan differential cross section in the muon channel as a function of the dilepton mass for mass bins "
	   << Form("(%d,%d) and (%d,%d)\n",
		   massLimits[iM],massLimits[iM+1],
		   massLimits[jM],massLimits[jM+1]);
           if (norm_type == "rshape") {
           string thiss =  "        # "+string(fsrFlag)+"FSR "+string(accFlag1)+" r-shape suppressed\n";
              fout << thiss.c_str();
           } else if (norm_type == "absolute") {
           string thiss =  "        # "+string(fsrFlag)+"FSR "+string(accFlag1)+" absolute suppressed\n";
              fout << thiss.c_str();
           } else {
           string thiss =  "        # "+string(fsrFlag)+"FSR "+string(accFlag1)+" (sigma/sigma_Z) suppressed\n";
              fout << thiss.c_str();
           }
           fout << "M : 20 - 1500 GeV\n";
           if (accFlag != "fullAcc") {
             fout << "ABS(ETA(MU)) <= 2.4\n";
           }
           fout <<  "PT(MU1) : >=9 GEV\n"
           <<  "PT(MU2) : >=14 GEV\n"
           <<  "RE : P P --> DYmumu X\n"
           <<  "SQRT(S) : 7000.0 GEV\n"
	   << "mass-rapidity-bin-index= rapBin + sum_{im=1}^{massBin-1} nYBins(im) \n"
	   << "x: mass-rapidity-bin-index\n"
	   << "y: mass-rapidity-bin-index\n";
           if (norm_type == "rshape") {
             fout << "z: cov(r_x,r_y)\n";
           } else if (norm_type == "absolute") {
             fout << "z: cov(sigma_x,sigma_y) [pb^2]\n";
           } else {
             fout << "z: cov(sigma_x/sigmaZ,sigma_y/sigmaZ)\n";
           }

      // print top row
      fout << "    ";
      int allZeroes=1;
      for (int fj=fj_min; fj<fj_min+nYBins[jM]; fj++) fout << Form("    %6d",fj+1);
      fout << "\n";

     // print the matrix
      for (int fi=fi_min+nYBins[iM]-1; fi>=fi_min; fi--) {
	fout << Form("%4d",fi+1) << "    ";
	for (int fj=fj_min; fj<fj_min+nYBins[jM]; fj++) {
          double corr_ij = MAlexey(fi,fj)/sqrt(MAlexey(fi,fi)*MAlexey(fj,fj));
          double prod = 0;
          if (fsrFlag == "pre" && accFlag == "inAcc") prod = diagonals_preFSR[fi]*diagonals_preFSR[fj];
          if (fsrFlag == "post" && accFlag == "inAcc") prod = diagonals_postFSR[fi]*diagonals_postFSR[fj];
          if (fi!=fj) M(fi,fj) = prod*corr_ij;
          else M(fi,fj) = prod;
          double val = M(fi,fj);
          if (norm_type == "absolute") { 
            if (fsrFlag == "pre" && accFlag == "inAcc")   val = sigma_Z*sigma_Z*val + rdiagonals_preFSR[fi]*rdiagonals_preFSR[fj]*sigmaZerrNoLumi;
            if (fsrFlag == "post" && accFlag == "inAcc")  val = sigma_Z*sigma_Z*val + rdiagonals_postFSR[fi]*rdiagonals_postFSR[fj]*sigmaZerrNoLumi;
          } 
          if (norm_type == "normalized" || norm_type == "absolute") {
              if (jM != nMassBins-1 && iM != nMassBins-1) {val *= 0.1*0.1;}
              else if (((jM == nMassBins-1) && !(iM == nMassBins-1)) || (!(jM == nMassBins-1) && (iM == nMassBins-1))) {
                 val *= 0.1*0.2;
              } else {
                 val *= 0.2*0.2;
              }
          }
          //FIXME correlation cut-off
          //if (fabs(MAlexey(fi,fj)/sqrt(MAlexey(fi,fi)*MAlexey(fj,fj)))<0.01) val=0.;
	  fout << "  " << Form("% 6.1e",val);
	  if (fabs(M(fi,fj))!=0.) allZeroes=0;
	}
	fout << "\n";
      }
      fj_min+=nYBins[jM];
      fout.close();
      if (allZeroes) {
	std::cout << "all entries are 0 in file <" << fname << ">\n";
      }
    }
    fi_min+=nYBins[iM];
  }

  for (int fj=0; fj<132; fj++) {
    for (int fi=0; fi < 132; fi++) {

          double corr_ij = MAlexey(fi,fj)/sqrt(MAlexey(fi,fi)*MAlexey(fj,fj));
          double prod = 0;
          if (fsrFlag == "pre") prod = diagonals_preFSR[fi]*diagonals_preFSR[fj];
          if (fsrFlag == "post") prod = diagonals_postFSR[fi]*diagonals_postFSR[fj];
          if (fi!=fj) M(fi,fj) = prod*corr_ij;
          else M(fi,fj) = prod;
          double val = M(fi,fj);
          if (norm_type == "absolute") {
            if (fsrFlag == "pre") val = sigma_Z*sigma_Z*val + rdiagonals_preFSR[fi]*rdiagonals_preFSR[fj]*sigmaZerrNoLumi;
            if (fsrFlag == "post")  val = sigma_Z*sigma_Z*val + rdiagonals_postFSR[fi]*rdiagonals_postFSR[fj]*sigmaZerrNoLumi;
          }
          if (norm_type == "normalized" || norm_type == "absolute") {
              if (fi < 120 && fj < 120) {val *= 0.1*0.1;}
              else if (((fi >=120) && !(fj >= 120)) || (!(fi >= 120) && (fj >= 120))) {
                 val *= 0.1*0.2;
              } else {
                 val *= 0.2*0.2;
              }
          }
          //FIXME correlation cut-off
          if (fabs(MAlexey(fi,fj)/sqrt(MAlexey(fi,fi)*MAlexey(fj,fj)))<0.01) val=0.;
          MTEST(fi,fj) = val;
      } 
   }

  TFile * fFile = new TFile("OUTcovariance_2D_mu_"+fsrFlag+"FSR_"+accFlag+"_"+norm_type+".root","RECREATE");
  MTEST.Write("totalCov_TM");
  Corig->Write("totalCorr_TM");
  fFile->Close();

  //validation
  cout << "Validation " << fsrFlag << " " << accFlag << " " << norm_type << endl;  

  //check positive definiteness by checning the signs of all eigenvalues
  cout << "Test matrix determinant " << MTEST.Determinant() << endl;  


 for (int i = 0; i < 132; i++) {
   for (int j = 0; j < 132; j++) {
     if (fabs(MTEST(i,j)-MTEST(j,i)) > fabs(MTEST(i,j))/10000.) cout << "Warning " << i << " " << j << " " << MTEST(i,j) << " " <<  MTEST(j,i) << endl;
   }
  }

 for (int i = 0; i < 132; i++) {
   for (int j = 0; j < 132; j++) {
     if (i!=j) continue;
     double val = 0;
     if (fsrFlag == "pre") val =diagonals_preFSR[j];
     if (fsrFlag == "post") val = diagonals_postFSR[j];
     if (norm_type == "absolute") {
          if (fsrFlag == "pre") val = sqrt(sigma_Z*sigma_Z*val*val + rdiagonals_preFSR[fi]*rdiagonals_preFSR[fj]*sigmaZerrNoLumi);
          if (fsrFlag == "post")  val = sqrt(sigma_Z*sigma_Z*val*val + rdiagonals_postFSR[fi]*rdiagonals_postFSR[fj]*sigmaZerrNoLumi);
     } 
     if (norm_type == "normalized" || norm_type == "absolute") {
              if (i < 120 && j < 120) {val *= 0.1;}
              else if (((i >=120) && !(j >= 120)) || (!(i >= 120) && (j >= 120))) {
                 val *= sqrt(0.1*0.2);
              } else {
                 val *= 0.2;
              }
     }
     if (fabs(sqrt(MTEST(j,j)) - val) > fabs(sqrt(MTEST(j,j)))/10000.) cout << "Warning " << sqrt(MTEST(j,j)) << " " << val << " " << fabs(sqrt(MTEST(j,j)) - val) << endl;
    }
  } 
}