Esempio n. 1
0
void read_matches(char    *filename_in_dir,
                  int      i_read_in,
                  int      j_read_in,
                  int      n_halos_max,
                  int      mode,
                  int     *n_groups_i,
                  int     *n_groups_j,
                  int     *n_particles_i_in,
                  int     *n_particles_j_in,
                  int     *n_sub_group_i_in,
                  int     *n_sub_group_j_in,
                  int     *match_ids,
                  float   *match_score,
                  size_t  *match_index,
                  char    *match_flag_two_way,
                  int      flag_reject_bad_matches){
   char   group_text_prefix[5];
   char   filename_in[MAX_FILENAME_LENGTH];
   SID_fp fp_in;
   int    k_read;
   int    l_read;
   int    n_search;
   int    n_matches;
   size_t offset;
   int    flag_continue;
   int    i_read_file;
   int    j_read_file;
   int    n_groups_file;
   int    n_groups_file_1;
   int    n_groups_file_2;
   int    n_groups;
   int    n_groups_i_file;
   int    n_groups_j_file;
   int   *n_sub_group_i;
   int   *n_sub_group_j;
   int    flag_alloc_n_sub_i=FALSE;
   int    flag_alloc_n_sub_j=FALSE;
   
   if(i_read_in==j_read_in)
     SID_trap_error("i_read=j_read in read_matches",ERROR_LOGIC);

   switch(mode){
      case MATCH_SUBGROUPS:
      sprintf(group_text_prefix,"sub");
      break;
      case MATCH_GROUPS:
      sprintf(group_text_prefix,"");
      // We need n_subgroups arrays for removal of bad groups
      //    if they have not been passed to us.
      if(n_sub_group_i_in==NULL){
         flag_alloc_n_sub_i=TRUE;
         n_sub_group_i     =(int *)SID_malloc(sizeof(int)*n_halos_max);
      }
      else
         n_sub_group_i=n_sub_group_i_in;
      if(n_sub_group_j_in==NULL){
         flag_alloc_n_sub_j=TRUE;
         n_sub_group_j     =(int *)SID_malloc(sizeof(int)*n_halos_max);
      }
      else
         n_sub_group_j=n_sub_group_j_in;
      break;
   }

   // Since we need the particle counts for the goodness of match criterion,
   //   create temporary arrays in case we weren't passed an array for them.
   int *n_particles_i=n_particles_i_in;
   int *n_particles_j=n_particles_j_in;
   // 1) We always need n_particles_i
   int  flag_alloc_n_particles_i=FALSE;
   int  flag_alloc_n_particles_j=FALSE;
   if(n_particles_i==NULL)
      flag_alloc_n_particles_i=TRUE;
   if(n_particles_j==NULL)
      flag_alloc_n_particles_j=TRUE;
   // 2) We need n_particles_j if doing 2-way checks
   if(match_flag_two_way!=NULL && n_particles_j==NULL)
      flag_alloc_n_particles_j=TRUE;

   // Read the needed info from the header file
   int  i_read;
   int  i_read_start;
   int  i_read_stop;
   int  n_search_total;
   int  n_files;
   int  n_groups_in;
   int  counter=0;
   char filename_in_name[256];
   strcpy(filename_in_name,filename_in_dir);
   strip_path(filename_in_name);
   sprintf(filename_in,"%s/%sgroup_matches_header.dat",filename_in_dir,group_text_prefix);
   SID_fopen(filename_in,"r",&fp_in);
   SID_fread(&i_read_start,  sizeof(int),1,&fp_in);
   SID_fread(&i_read_stop,   sizeof(int),1,&fp_in);
   SID_fread(&n_search_total,sizeof(int),1,&fp_in);
   SID_fread(&n_files,       sizeof(int),1,&fp_in);
   for(i_read=i_read_stop;i_read>=i_read_start && counter<2;i_read--){
      SID_fread(&i_read_file, sizeof(int),1,&fp_in);
      if(i_read_file==i_read_in){
         SID_fread(n_groups_i,sizeof(int),1,&fp_in);
         if((*n_groups_i)>0){
            // Create a temporary array for n_particles_i if we were not passed one
            if(flag_alloc_n_particles_i)
               n_particles_i=(int *)SID_malloc(sizeof(int)*(*n_groups_i));
            if(n_particles_i!=NULL)
               SID_fread_ordered(n_particles_i,sizeof(int),(size_t)(*n_groups_i),&fp_in);
            else
               SID_fskip(sizeof(int),(*n_groups_i),&fp_in);
            if(mode==MATCH_GROUPS){
               if(n_sub_group_i!=NULL)
                  SID_fread_ordered(n_sub_group_i,sizeof(int),(size_t)(*n_groups_i),&fp_in);
               else
                  SID_fskip(sizeof(int),(*n_groups_i),&fp_in);
            }
         }
         counter++;
      }
      else if(i_read_file==j_read_in){
         SID_fread(n_groups_j,sizeof(int),1,&fp_in);
         if((*n_groups_j)>0){
            if(flag_alloc_n_particles_j)
               n_particles_j=(int *)SID_malloc(sizeof(int)*(*n_groups_j));
            if(n_particles_j!=NULL)
               SID_fread_ordered(n_particles_j,sizeof(int),(size_t)(*n_groups_j),&fp_in);
            else
               SID_fskip(sizeof(int),(*n_groups_j),&fp_in);
            if(mode==MATCH_GROUPS){
               if(n_sub_group_j!=NULL)
                  SID_fread_ordered(n_sub_group_j,sizeof(int),(size_t)(*n_groups_j),&fp_in);
               else
                  SID_fskip(sizeof(int),(*n_groups_j),&fp_in);
            }
         }
         counter++;
      }
      else{
         SID_fread(&n_groups_in,sizeof(int),1,&fp_in);
         if(n_groups_in>0){
            SID_fskip(sizeof(int),n_groups_in,&fp_in);
            if(mode==MATCH_GROUPS)
               SID_fskip(sizeof(int),n_groups_in,&fp_in);
         }
      }
   }
   SID_fclose(&fp_in);

   // Read the matching file
   char filename_cat1[256];
   char filename_cat2[256];
   char filename_in_dir_snap[256];
   sprintf(filename_cat1,"%03d",i_read_in);
   sprintf(filename_cat2,"%03d",j_read_in);
   sprintf(filename_in_dir_snap,"%s/%s",filename_in_dir,filename_cat1);
   if(filename_in_dir!=NULL)
      sprintf(filename_in,"%s/%sgroup_matches_%s_%s.dat",filename_in_dir_snap,group_text_prefix,filename_cat1,filename_cat2);
   else
      sprintf(filename_in,"%s_%sgroup_matches_%s_%s.dat",filename_in_name,    group_text_prefix,filename_cat1,filename_cat2);

   SID_fopen(filename_in,"r",&fp_in);
   SID_fread(&i_read_file,sizeof(int),1,&fp_in);
   SID_fread(&j_read_file,sizeof(int),1,&fp_in);
   SID_fread(n_groups_i,  sizeof(int),1,&fp_in);
   SID_fread(n_groups_j,  sizeof(int),1,&fp_in);

   // Read matching data
   SID_fread(match_ids,  sizeof(int),   (*n_groups_i),&fp_in);
   SID_fread(match_index,sizeof(size_t),(*n_groups_i),&fp_in);
   SID_fread(match_score,sizeof(float), (*n_groups_i),&fp_in);
   SID_fclose(&fp_in);

   // If one of the catalogs is empty, set to no-match defaults
   if((*n_groups_i)<=0 || (*n_groups_j)<=0){
      for(int i_halo=0;i_halo<(*n_groups_i);i_halo++){
         match_ids[i_halo]  =-1;
         match_score[i_halo]= 0.;
      }
      if(match_flag_two_way!=NULL){
         for(int i_halo=0;i_halo<(*n_groups_i);i_halo++)
            match_flag_two_way[i_halo]=FALSE;
      }
   }
   else{
      // If we are reading groups, nullify all matches
      //    between halos with no substructures.
      int i_halo;
      if(mode==MATCH_GROUPS){
         size_t *match_index_temp;
         for(i_halo=0;i_halo<(*n_groups_i);i_halo++){
            if(n_sub_group_i[i_halo]<=0){
               match_ids[i_halo]  =-1;
               match_score[i_halo]= 0.;
            }
            else if(match_ids[i_halo]>=0 && (*n_groups_j)>0){
               if(n_sub_group_j[match_ids[i_halo]]<=0){
                  match_ids[i_halo]  =-1;
                  match_score[i_halo]= 0.;
               }
            }
         }
         merge_sort(match_ids,(size_t)(*n_groups_i),&match_index_temp,SID_INT,SORT_COMPUTE_INDEX,SORT_COMPUTE_NOT_INPLACE);
         memcpy(match_index,match_index_temp,(*n_groups_i)*sizeof(size_t));
         SID_free(SID_FARG match_index_temp);
      }

      // Apply a goodness-of-fit criterion and check that the maximum allowed score has not been exceeded
      for(i_halo=0;i_halo<(*n_groups_i);i_halo++){
         if(match_ids[i_halo]>=0){
            if(flag_reject_bad_matches && !check_validity_of_match(n_particles_i[i_halo],match_score[i_halo]))
               match_ids[i_halo]=-1;
         }
      }

      // Since we may have changed some matches with the goodness 
      //    of fit criterion, we need to re-perform the sort
      size_t *match_index_temp=NULL;
      merge_sort(match_ids,(size_t)(*n_groups_i),&match_index_temp,SID_INT,SORT_COMPUTE_INDEX,SORT_COMPUTE_NOT_INPLACE);
      memcpy(match_index,match_index_temp,(*n_groups_i)*sizeof(size_t));
      SID_free(SID_FARG match_index_temp);

      // Determine if the matches are two-way if we have been asked to check this
      if(match_flag_two_way!=NULL && (*n_groups_i)>0){

         // We're going to need the particle counts in the target catalog for checking the goodness of return matches.  Make sure we have them.
         if(n_particles_j==NULL)
            SID_trap_error("Target catalog halo sizes are not defined in read_matches() but are needed for checking two-way match flags.",ERROR_LOGIC);

         // Flip the file names for reading the return matches
         sprintf(filename_in_dir_snap,"%s/%s",filename_in_dir,filename_cat2);
         if(filename_in_dir!=NULL)
            sprintf(filename_in,"%s/%sgroup_matches_%s_%s.dat",filename_in_dir_snap,group_text_prefix,filename_cat2,filename_cat1);
         else
            sprintf(filename_in,"%s_%sgroup_matches_%s_%s.dat",filename_in_name,    group_text_prefix,filename_cat2,filename_cat1);

         // Open two files, one for reading the IDs and one for matching the scores of the matching file
         int i_read_file_check;
         int j_read_file_check;
         int n_groups_i_check;
         int n_groups_j_check;
         SID_fp fp_check_ids;
         SID_fp fp_check_score;
         SID_fopen(filename_in,"r",&fp_check_ids);
         SID_fopen(filename_in,"r",&fp_check_score);
         SID_fread(&j_read_file_check,sizeof(int),1,&fp_check_ids);
         SID_fread(&i_read_file_check,sizeof(int),1,&fp_check_ids);
         SID_fread(&n_groups_j_check, sizeof(int),1,&fp_check_ids);
         SID_fread(&n_groups_i_check, sizeof(int),1,&fp_check_ids);

         // Check that we have the right files
         if(n_groups_i_check!=(*n_groups_i))
            SID_trap_error("Source halo counts don't match (ie. %d!=%d) in two-way check in read_matches().",ERROR_LOGIC,n_groups_i_check,(*n_groups_i));
         if(n_groups_j_check!=(*n_groups_j))
            SID_trap_error("Target halo counts don't match (ie. %d!=%d) in two-way check in read_matches().",ERROR_LOGIC,n_groups_j_check,(*n_groups_j));
         if(i_read_file_check!=i_read_file)
            SID_trap_error("Source file numbers don't match (ie. %d!=%d) in two-way check in read_matches().",ERROR_LOGIC,i_read_file_check,i_read_file);
         if(j_read_file_check!=j_read_file)
            SID_trap_error("Target file numbers don't match (ie. %d!=%d) in two-way check in read_matches().",ERROR_LOGIC,j_read_file_check,j_read_file);

         // Skip to the beginning of the relevant block for the score-reading file pointer
         SID_fskip(sizeof(int),   4,            &fp_check_score); // header
         SID_fskip(sizeof(int),   (*n_groups_j),&fp_check_score); // ids
         SID_fskip(sizeof(size_t),(*n_groups_j),&fp_check_score); // indices

         // Set everything to being a one-way match unless subsequently changed
         for(i_halo=0;i_halo<(*n_groups_i);i_halo++) 
            match_flag_two_way[i_halo]=FALSE;

         // Read matching data in buffered chunks 
         int     n_good      =0;
         int     n_2way      =0;
         int     n_buffer    =1024*1024;
         int     n_chunk     =0;
         int     n_remaining =(*n_groups_j);
         int    *buffer_ids  =(int   *)SID_malloc(sizeof(int)  *n_buffer);
         float  *buffer_score=(float *)SID_malloc(sizeof(float)*n_buffer);
         for(int j_halo=0;n_remaining>0;n_remaining-=n_chunk){
            n_chunk=MIN(n_remaining,n_buffer);
            SID_fread(buffer_ids,  sizeof(int),  n_chunk,&fp_check_ids);
            SID_fread(buffer_score,sizeof(float),n_chunk,&fp_check_score);
            for(int k_halo=0;k_halo<n_chunk;k_halo++,j_halo++){
               int id_i=buffer_ids[k_halo];
               if(id_i>=0){
                  if(id_i>=(*n_groups_i))
                     SID_trap_error("Allowed matching index has been exceeded i(ie %d>=%d) while determining two-way match flags.",
                                    ERROR_LOGIC,id_i,(*n_groups_i));
                  // Do this check first to avoid having to check if both needed n_particles_* references are defined
                  int id_j=match_ids[id_i];
                  if(id_j==j_halo){
                     if(!flag_reject_bad_matches || check_validity_of_match(n_particles_j[j_halo],buffer_score[k_halo])){
                        match_flag_two_way[id_i]=TRUE; 
                        n_2way++;
                     }
                     n_good++;
                  }
               }
            }
         }
         //SID_log("n_good=%d n_2way=%d",SID_LOG_COMMENT,n_good,n_2way);
         SID_fclose(&fp_check_ids);
         SID_fclose(&fp_check_score);
         SID_free(SID_FARG buffer_ids);
         SID_free(SID_FARG buffer_score);
      }
   }

   // If any of these arrays are temporary, free them.
   if(flag_alloc_n_sub_i)
      SID_free(SID_FARG n_sub_group_i);
   if(flag_alloc_n_sub_j)
      SID_free(SID_FARG n_sub_group_j);
   if(flag_alloc_n_particles_i)
      SID_free(SID_FARG n_particles_i);
   if(flag_alloc_n_particles_j)
      SID_free(SID_FARG n_particles_j);
}
Esempio n. 2
0
int main(int argc, char *argv[]) {
    int    n_search;
    int    i_halo;
    char   filename_in[SID_MAX_FILENAME_LENGTH];
    char   group_text_prefix[4];
    int    n_files;
    int    k_read;
    int    l_read;
    int *  n_particles_i;
    int *  n_particles_j;
    int    j_read;
    int    mode;
    int    j_halo;
    int    i_read;
    int    i_read_start;
    int    i_read_stop;
    SID_fp fp_in;

    SID_Init(&argc, &argv, NULL);

    // Fetch user inputs
    char filename_SSimPL_root[SID_MAX_FILENAME_LENGTH];
    strcpy(filename_SSimPL_root, argv[1]);
    SID_log("Checking the integrity of the match files for {%s}...", SID_LOG_OPEN | SID_LOG_TIMER, filename_SSimPL_root);
    int *n_groups    = NULL;
    int *n_subgroups = NULL;
    for(int i_type = 0; i_type < 2; i_type++) {
        // Convert filename_root to filename
        switch(i_type) {
            case 0:
                mode = MATCH_SUBGROUPS;
                sprintf(group_text_prefix, "sub");
                break;
            case 1:
                mode = MATCH_GROUPS;
                sprintf(group_text_prefix, "");
                break;
        }
        SID_log("Processing %sgroups...", SID_LOG_OPEN | SID_LOG_TIMER, group_text_prefix);

        // Set the standard SSiMPL match file path
        char filename_root_in[SID_MAX_FILENAME_LENGTH];
        sprintf(filename_root_in, "%s/trees/matches/", filename_SSimPL_root);

        // Read halo sizes from header file
        SID_log("Processing header file...", SID_LOG_OPEN | SID_LOG_TIMER);
        sprintf(filename_in, "%s/%sgroup_matches_header.dat", filename_root_in, group_text_prefix);
        SID_fopen(filename_in, "r", &fp_in);
        SID_fread(&i_read_start, sizeof(int), 1, &fp_in);
        SID_fread(&i_read_stop, sizeof(int), 1, &fp_in);
        SID_fread(&n_search, sizeof(int), 1, &fp_in);
        SID_fread(&n_files, sizeof(int), 1, &fp_in);
        int *n_halos = NULL;
        switch(mode) {
            case MATCH_SUBGROUPS:
                n_subgroups = (int *)SID_malloc(sizeof(int) * n_files);
                n_halos     = n_subgroups;
                break;
            case MATCH_GROUPS:
                n_groups = (int *)SID_malloc(sizeof(int) * n_files);
                n_halos  = n_groups;
                break;
        }
        if(mode == MATCH_GROUPS)
            SID_log("Halo counts (snap/No. groups/No. subgroups):", SID_LOG_OPEN);
        for(k_read = 0; k_read < n_files; k_read++) {
            SID_fread(&l_read, sizeof(int), 1, &fp_in);
            SID_fread(&(n_halos[k_read]), sizeof(int), 1, &fp_in);
            SID_fskip(sizeof(int), n_halos[k_read], &fp_in);
            if(mode == MATCH_GROUPS) {
                int *n_subgroups_group = (int *)SID_malloc(sizeof(int) * n_halos[k_read]);
                SID_fread(n_subgroups_group, sizeof(int), n_halos[k_read], &fp_in);
                int n_subgroups_test = 0;
                for(int i_test = 0; i_test < n_halos[k_read]; i_test++)
                    n_subgroups_test += n_subgroups_group[i_test];
                if(n_subgroups[k_read] != n_subgroups_test)
                    SID_log("Error in %s header: l_read=%3d k_read=%3d n_subgroups: %d!=%d\n",
                            SID_LOG_COMMENT,
                            l_read,
                            k_read,
                            n_subgroups[k_read],
                            n_subgroups_test);
                SID_free(SID_FARG n_subgroups_group);
            }
            if(mode == MATCH_GROUPS)
                SID_log("%03d %d %d", SID_LOG_COMMENT, k_read, n_groups[k_read], n_subgroups[k_read]);
        }
        if(mode == MATCH_GROUPS)
            SID_log("", SID_LOG_CLOSE | SID_LOG_NOPRINT);
        SID_fclose(&fp_in);
        SID_log("Done.", SID_LOG_CLOSE);

        SID_log("Processing match files...", SID_LOG_OPEN | SID_LOG_TIMER);
        for(int i_read = i_read_start; i_read < i_read_stop; i_read++) {
            for(int j_read = GBP_MAX(0, i_read - n_search); j_read < GBP_MIN(i_read_stop, i_read + n_search); j_read++) {
                if(i_read != j_read) {
                    sprintf(filename_in, "%s/%03d/%sgroup_matches_%03d_%03d.dat", filename_root_in, i_read, group_text_prefix, i_read, j_read);
                    SID_log("Processing {%s}...", SID_LOG_OPEN, filename_in);

                    // Read header information
                    int i_read_in;
                    int j_read_in;
                    int n_groups_i;
                    int n_groups_j;
                    SID_fopen(filename_in, "r", &fp_in);
                    SID_fread(&i_read_in, sizeof(int), 1, &fp_in);
                    SID_fread(&j_read_in, sizeof(int), 1, &fp_in);
                    SID_fread(&n_groups_i, sizeof(int), 1, &fp_in);
                    SID_fread(&n_groups_j, sizeof(int), 1, &fp_in);

                    if(i_read_in != i_read || j_read_in != j_read || n_groups_i != n_halos[n_files - i_read_in - 1] ||
                       n_groups_j != n_halos[n_files - j_read_in - 1])
                        SID_log("Error in matching file: i_read=%3d j_read=%3d n_i_in=%d n_i=%d n_j_in=%d n_j=%d\n",
                                SID_LOG_COMMENT,
                                i_read,
                                j_read,
                                n_groups_i,
                                n_halos[n_files - i_read_in - 1],
                                n_groups_j,
                                n_halos[n_files - j_read_in - 1]);

                    // Read matches
                    int match;
                    for(k_read = 0; k_read < n_groups_i; k_read++)
                        SID_fread(&match, sizeof(int), 1, &fp_in);

                    // Read indices
                    size_t indices;
                    for(k_read = 0; k_read < n_groups_i; k_read++)
                        SID_fread(&indices, sizeof(size_t), 1, &fp_in);

                    // Read scores
                    float score;
                    for(k_read = 0; k_read < n_groups_i; k_read++)
                        SID_fread(&score, sizeof(float), 1, &fp_in);

                    // Close file
                    SID_fclose(&fp_in);

                    SID_log("Done.", SID_LOG_CLOSE);
                }
            }
        }
        SID_log("Done.", SID_LOG_CLOSE);

        SID_log("Done.", SID_LOG_CLOSE);
    }
    SID_free(SID_FARG n_groups);
    SID_free(SID_FARG n_subgroups);

    SID_log("Done.", SID_LOG_CLOSE);
    SID_Finalize();
}
Esempio n. 3
0
int main(int argc, char *argv[]) {
    int     n_search;
    int     n_files;
    int     k_read;
    int     max_n_groups;
    int     l_read;
    int *   n_particles_i;
    int *   n_particles_j;
    int     n_groups_i;
    int     n_groups_j;
    int *   match_ids;
    float * match_score;
    size_t *match_index;
    int     j_halo;
    int     match;
    int     i_read;
    int     i_read_start;
    int     i_read_stop;
    SID_fp  fp_in;

    SID_Init(&argc, &argv, NULL);

    // Fetch user inputs
    if(argc != 12)
        SID_exit_error("Invalid Syntax.", SID_ERROR_SYNTAX);
    char filename_SSimPL_root[SID_MAX_FILENAME_LENGTH];
    char filename_halos_root[SID_MAX_FILENAME_LENGTH];
    char filename_trees_root[SID_MAX_FILENAME_LENGTH];
    char filename_out_root[SID_MAX_FILENAME_LENGTH];
    strcpy(filename_SSimPL_root, argv[1]);
    strcpy(filename_halos_root, argv[2]);
    strcpy(filename_trees_root, argv[3]);
    double x_cen    = (double)atof(argv[4]);
    double y_cen    = (double)atof(argv[5]);
    double z_cen    = (double)atof(argv[6]);
    double radius   = (double)atof(argv[7]);
    double z_min_in = (double)atof(argv[8]);
    double z_max_in = (double)atof(argv[9]);
    double M_min    = (double)atof(argv[10]);
    strcpy(filename_out_root, argv[11]);
    double radius2 = radius * radius;

    SID_log("Query trees for sphere (x,y,z,r)=(%.2lf,%.2lf,%.2lf,%.2lf) between z=%.2lf and z=%.2lf...",
            SID_LOG_OPEN,
            x_cen,
            y_cen,
            z_cen,
            radius,
            z_min_in,
            z_max_in);

    char filename_catalog_root[SID_MAX_FILENAME_LENGTH];
    sprintf(filename_catalog_root, "%s/catalogs/%s", filename_SSimPL_root, filename_halos_root);

    // Read tree header information
    tree_info *trees;
    char       filename_file_root[SID_MAX_FILENAME_LENGTH];
    sprintf(filename_file_root, "%s/trees/%s", filename_SSimPL_root, filename_trees_root);
    SID_set_verbosity(SID_SET_VERBOSITY_RELATIVE, 0);
    init_trees_read(filename_SSimPL_root, filename_halos_root, filename_trees_root, TREE_READ_HEADER_ONLY, &trees);
    SID_set_verbosity(SID_SET_VERBOSITY_DEFAULT);

    // Turn given redshift range into snapshot range
    int i_snap_min_z = find_treesnap_z(trees, z_max_in);
    int i_snap_max_z = find_treesnap_z(trees, z_min_in);
    SID_log("z=%.2lf -> snapshot=%d", SID_LOG_COMMENT, z_min_in, trees->snap_list[i_snap_max_z]);
    SID_log("z=%.2lf -> snapshot=%d", SID_LOG_COMMENT, z_max_in, trees->snap_list[i_snap_min_z]);

    // Perform query
    int  n_groups_list    = 0;
    int  n_subgroups_list = 0;
    int *group_list       = NULL;
    int *subgroup_list    = NULL;
    for(int i_pass = 0; i_pass < 3; i_pass++) {
        if(i_pass == 0)
            SID_log("Counting halos to be queried...", SID_LOG_OPEN | SID_LOG_TIMER);
        else if(i_pass == 1)
            SID_log("Identifying halos to be queried...", SID_LOG_OPEN | SID_LOG_TIMER);
        else if(i_pass == 2)
            SID_log("Performing query...", SID_LOG_OPEN | SID_LOG_TIMER);
        // Write headers
        if(i_pass == 2) {
            for(int i_type = 0; i_type < 2; i_type++) {
                int  n_list    = 0;
                int *halo_list = NULL;
                if(i_type == 0) {
                    n_list    = n_groups_list;
                    halo_list = group_list;
                } else {
                    n_list    = n_subgroups_list;
                    halo_list = subgroup_list;
                }
                for(int i_list = 0; i_list < n_list; i_list++) {
                    int  i_column = 1;
                    char filename_out[SID_MAX_FILENAME_LENGTH];
                    if(i_type == 0)
                        sprintf(filename_out, "%s_group_%09d.txt", filename_out_root, halo_list[i_list]);
                    else
                        sprintf(filename_out, "%s_subgroup_%09d.txt", filename_out_root, halo_list[i_list]);
                    FILE *fp_out = fopen(filename_out, "w");
                    fprintf(fp_out, "# Column (%02d): Halo expansion factor\n", i_column++);
                    fprintf(fp_out, "#        (%02d): Halo redshift\n", i_column++);
                    fprintf(fp_out, "#        (%02d): Halo snapshot\n", i_column++);
                    fprintf(fp_out, "#        (%02d): Halo index\n", i_column++);
                    fprintf(fp_out, "#        (%02d): Halo ID\n", i_column++);
                    fprintf(fp_out, "#        (%02d): Halo log10(M_vir [M_sol/h])\n", i_column++);
                    fprintf(fp_out, "#        (%02d): Halo n_particles\n", i_column++);
                    fprintf(fp_out, "#        (%02d): Halo n_particles_peak\n", i_column++);
                    fprintf(fp_out, "#        (%02d): Halo x [Mpc/h])\n", i_column++);
                    fprintf(fp_out, "#        (%02d): Halo y [Mpc/h])\n", i_column++);
                    fprintf(fp_out, "#        (%02d): Halo z [Mpc/h])\n", i_column++);
                    fprintf(fp_out, "#        (%02d): Halo radius [Mpc/h])\n", i_column++);
                    fprintf(fp_out, "#        (%02d): Halo tree ID\n", i_column++);
                    fprintf(fp_out, "#        (%02d): Descendant file offset\n", i_column++);
                    fprintf(fp_out, "#        (%02d): Descendant snapshot\n", i_column++);
                    fprintf(fp_out, "#        (%02d): Descendant index\n", i_column++);
                    fprintf(fp_out, "#        (%02d): Descendant ID\n", i_column++);
                    fprintf(fp_out, "#        (%02d): Bridge forematch snapshot\n", i_column++);
                    fprintf(fp_out, "#        (%02d): Bridge forematch index\n", i_column++);
                    fprintf(fp_out, "#        (%02d): Bridge backmatch snapshot\n", i_column++);
                    fprintf(fp_out, "#        (%02d): Bridge backmatch index\n", i_column++);
                    fprintf(fp_out, "#        (%02d): Halo type\n", i_column++);
                    fprintf(fp_out, "#        (%02d): Halo type string\n", i_column++);
                    if(i_type == 1) {
                        fprintf(fp_out, "#        (%02d): Group index\n", i_column++);
                        fprintf(fp_out, "#        (%02d): Group ID\n", i_column++);
                        fprintf(fp_out, "#        (%02d): Subgroup index\n", i_column++);
                        fprintf(fp_out, "#        (%02d): FoF Centre dx [Mpc/h])\n", i_column++);
                        fprintf(fp_out, "#        (%02d): FoF Centre dy [Mpc/h])\n", i_column++);
                        fprintf(fp_out, "#        (%02d): FoF Centre dz [Mpc/h])\n", i_column++);
                    }
                    fclose(fp_out);
                }
            }
        }

        // Set the snapshot range we need to scan
        int i_snap_start;
        int i_snap_stop;
        if(i_pass < 2) {
            i_snap_start = i_snap_min_z;
            i_snap_stop  = i_snap_max_z;
        } else {
            i_snap_start = 0;
            i_snap_stop  = trees->n_snaps - 1;
        }

        // Loop over the range of snapshots
        int i_list = 0;
        for(int i_snap = i_snap_start; i_snap <= i_snap_stop; i_snap++) {
            // Get the snapshot
            int snapshot = trees->snap_list[i_snap];
            if(i_pass == 2)
                SID_log("Processing snapshot %03d...", SID_LOG_OPEN, snapshot);

            // Open properties for this snapshot
            fp_catalog_info      fp_properties_groups;
            fp_catalog_info      fp_properties_subgroups;
            halo_properties_info properties_groups;
            halo_properties_info properties_subgroups;
            fopen_catalog(filename_catalog_root, snapshot, READ_CATALOG_GROUPS | READ_CATALOG_PROPERTIES, &fp_properties_groups);
            fopen_catalog(filename_catalog_root, snapshot, READ_CATALOG_SUBGROUPS | READ_CATALOG_PROPERTIES, &fp_properties_subgroups);

            // Open horizontal trees for this snapshot
            SID_fp fp_in_trees;
            SID_fp fp_in_bridge_forematch;
            SID_fp fp_in_bridge_backmatch;
            char   filename_in[SID_MAX_FILENAME_LENGTH];
            sprintf(filename_in, "%s/trees/%s/horizontal/trees/horizontal_trees_%03d.dat", filename_SSimPL_root, filename_trees_root, snapshot);
            SID_fopen(filename_in, "r", &fp_in_trees);
            sprintf(filename_in,
                    "%s/trees/%s/horizontal/trees/horizontal_trees_forematch_pointers_%03d.dat",
                    filename_SSimPL_root,
                    filename_trees_root,
                    snapshot);
            SID_fopen(filename_in, "r", &fp_in_bridge_forematch);
            sprintf(filename_in,
                    "%s/trees/%s/horizontal/trees/horizontal_trees_backmatch_pointers_%03d.dat",
                    filename_SSimPL_root,
                    filename_trees_root,
                    snapshot);
            SID_fopen(filename_in, "r", &fp_in_bridge_backmatch);

            // Read trees header
            int n_step_in;
            int n_search_in;
            int n_groups;
            int n_subgroups;
            int n_groups_max_in;
            int n_subgroups_max_in;
            int n_trees_subgroup_in;
            int n_trees_group_in;
            SID_fread_all(&n_step_in, sizeof(int), 1, &fp_in_trees);
            SID_fread_all(&n_search_in, sizeof(int), 1, &fp_in_trees);
            SID_fread_all(&n_groups, sizeof(int), 1, &fp_in_trees);
            SID_fread_all(&n_subgroups, sizeof(int), 1, &fp_in_trees);
            SID_fread_all(&n_groups_max_in, sizeof(int), 1, &fp_in_trees);
            SID_fread_all(&n_subgroups_max_in, sizeof(int), 1, &fp_in_trees);
            SID_fread_all(&n_trees_subgroup_in, sizeof(int), 1, &fp_in_trees);
            SID_fread_all(&n_trees_group_in, sizeof(int), 1, &fp_in_trees);
            SID_fskip(sizeof(int), 8, &fp_in_bridge_forematch);
            SID_fskip(sizeof(int), 8, &fp_in_bridge_backmatch);

            // Scan through the trees
            int i_subgroup = 0;
            for(int i_group = 0; i_group < n_groups; i_group++) {
                // Read group
                int   group_id;
                int   group_type;
                int   group_descendant_id;
                int   group_tree_id;
                int   group_file_offset;
                int   group_index;
                int   group_n_particles_peak;
                int   n_subgroups_group;
                int   group_forematch_id;
                int   group_forematch_first_file;
                int   group_forematch_first_index;
                float group_forematch_first_score;
                int   group_forematch_default_file;
                int   group_forematch_default_index;
                float group_forematch_default_score;
                int   group_forematch_best_file;
                int   group_forematch_best_index;
                float group_forematch_best_score;
                float group_forematch_score_prog;
                int   group_backmatch_id;
                int   group_backmatch_file;
                int   group_backmatch_index;
                float group_backmatch_score;
                float group_backmatch_score_prog;
                SID_fread_all(&group_id, sizeof(int), 1, &fp_in_trees);
                SID_fread_all(&group_type, sizeof(int), 1, &fp_in_trees);
                SID_fread_all(&group_descendant_id, sizeof(int), 1, &fp_in_trees);
                SID_fread_all(&group_tree_id, sizeof(int), 1, &fp_in_trees);
                SID_fread_all(&group_file_offset, sizeof(int), 1, &fp_in_trees);
                SID_fread_all(&group_index, sizeof(int), 1, &fp_in_trees);
                SID_fread_all(&group_n_particles_peak, sizeof(int), 1, &fp_in_trees);
                SID_fread_all(&n_subgroups_group, sizeof(int), 1, &fp_in_trees);
                SID_fread_all(&group_forematch_id, sizeof(int), 1, &fp_in_bridge_forematch);
                SID_fread_all(&group_forematch_first_file, sizeof(int), 1, &fp_in_bridge_forematch);
                SID_fread_all(&group_forematch_first_index, sizeof(int), 1, &fp_in_bridge_forematch);
                SID_fread_all(&group_forematch_first_score, sizeof(float), 1, &fp_in_bridge_forematch);
                SID_fread_all(&group_forematch_default_file, sizeof(int), 1, &fp_in_bridge_forematch);
                SID_fread_all(&group_forematch_default_index, sizeof(int), 1, &fp_in_bridge_forematch);
                SID_fread_all(&group_forematch_default_score, sizeof(float), 1, &fp_in_bridge_forematch);
                SID_fread_all(&group_forematch_best_file, sizeof(int), 1, &fp_in_bridge_forematch);
                SID_fread_all(&group_forematch_best_index, sizeof(int), 1, &fp_in_bridge_forematch);
                SID_fread_all(&group_forematch_best_score, sizeof(float), 1, &fp_in_bridge_forematch);
                SID_fread_all(&group_forematch_score_prog, sizeof(float), 1, &fp_in_bridge_forematch);
                SID_fread_all(&group_backmatch_id, sizeof(int), 1, &fp_in_bridge_backmatch);
                SID_fread_all(&group_backmatch_file, sizeof(int), 1, &fp_in_bridge_backmatch);
                SID_fread_all(&group_backmatch_index, sizeof(int), 1, &fp_in_bridge_backmatch);
                SID_fread_all(&group_backmatch_score, sizeof(float), 1, &fp_in_bridge_backmatch);
                SID_fread_all(&group_backmatch_score_prog, sizeof(float), 1, &fp_in_bridge_backmatch);
                SID_fskip(sizeof(int), 1, &fp_in_bridge_forematch); // skip subhalo count
                SID_fskip(sizeof(int), 1, &fp_in_bridge_backmatch); // skip subhalo count
                fread_catalog_file(&fp_properties_groups, NULL, NULL, &properties_groups, NULL, i_group);

                // Process group
                process_local(trees,
                              i_pass,
                              filename_out_root,
                              radius2,
                              M_min,
                              i_snap,
                              i_group,
                              0,
                              i_group,
                              group_id,
                              group_file_offset,
                              group_type,
                              group_n_particles_peak,
                              group_tree_id,
                              group_index,
                              group_descendant_id,
                              group_forematch_first_index,
                              group_forematch_first_file,
                              group_backmatch_index,
                              group_backmatch_file,
                              group_id,
                              &properties_groups,
                              NULL,
                              &n_groups_list,
                              group_list,
                              x_cen,
                              y_cen,
                              z_cen);

                for(int j_subgroup = 0; j_subgroup < n_subgroups_group; i_subgroup++, j_subgroup++) {
                    // Read subgroup
                    int   subgroup_id;
                    int   subgroup_type;
                    int   subgroup_descendant_id;
                    int   subgroup_tree_id;
                    int   subgroup_file_offset;
                    int   subgroup_index;
                    int   subgroup_n_particles_peak;
                    int   subgroup_forematch_id;
                    int   subgroup_forematch_first_file;
                    int   subgroup_forematch_first_index;
                    float subgroup_forematch_first_score;
                    int   subgroup_forematch_default_file;
                    int   subgroup_forematch_default_index;
                    float subgroup_forematch_default_score;
                    int   subgroup_forematch_best_file;
                    int   subgroup_forematch_best_index;
                    float subgroup_forematch_best_score;
                    float subgroup_forematch_score_prog;
                    int   subgroup_backmatch_id;
                    int   subgroup_backmatch_file;
                    int   subgroup_backmatch_index;
                    float subgroup_backmatch_score;
                    float subgroup_backmatch_score_prog;
                    SID_fread_all(&subgroup_id, sizeof(int), 1, &fp_in_trees);
                    SID_fread_all(&subgroup_type, sizeof(int), 1, &fp_in_trees);
                    SID_fread_all(&subgroup_descendant_id, sizeof(int), 1, &fp_in_trees);
                    SID_fread_all(&subgroup_tree_id, sizeof(int), 1, &fp_in_trees);
                    SID_fread_all(&subgroup_file_offset, sizeof(int), 1, &fp_in_trees);
                    SID_fread_all(&subgroup_index, sizeof(int), 1, &fp_in_trees);
                    SID_fread_all(&subgroup_n_particles_peak, sizeof(int), 1, &fp_in_trees);
                    SID_fread_all(&subgroup_forematch_id, sizeof(int), 1, &fp_in_bridge_forematch);
                    SID_fread_all(&subgroup_forematch_first_file, sizeof(int), 1, &fp_in_bridge_forematch);
                    SID_fread_all(&subgroup_forematch_first_index, sizeof(int), 1, &fp_in_bridge_forematch);
                    SID_fread_all(&subgroup_forematch_first_score, sizeof(float), 1, &fp_in_bridge_forematch);
                    SID_fread_all(&subgroup_forematch_default_file, sizeof(int), 1, &fp_in_bridge_forematch);
                    SID_fread_all(&subgroup_forematch_default_index, sizeof(int), 1, &fp_in_bridge_forematch);
                    SID_fread_all(&subgroup_forematch_default_score, sizeof(float), 1, &fp_in_bridge_forematch);
                    SID_fread_all(&subgroup_forematch_best_file, sizeof(int), 1, &fp_in_bridge_forematch);
                    SID_fread_all(&subgroup_forematch_best_index, sizeof(int), 1, &fp_in_bridge_forematch);
                    SID_fread_all(&subgroup_forematch_best_score, sizeof(float), 1, &fp_in_bridge_forematch);
                    SID_fread_all(&subgroup_forematch_score_prog, sizeof(float), 1, &fp_in_bridge_forematch);
                    SID_fread_all(&subgroup_backmatch_id, sizeof(int), 1, &fp_in_bridge_backmatch);
                    SID_fread_all(&subgroup_backmatch_file, sizeof(int), 1, &fp_in_bridge_backmatch);
                    SID_fread_all(&subgroup_backmatch_index, sizeof(int), 1, &fp_in_bridge_backmatch);
                    SID_fread_all(&subgroup_backmatch_score, sizeof(float), 1, &fp_in_bridge_backmatch);
                    SID_fread_all(&subgroup_backmatch_score_prog, sizeof(float), 1, &fp_in_bridge_backmatch);
                    fread_catalog_file(&fp_properties_subgroups, NULL, NULL, &properties_subgroups, NULL, i_subgroup);

                    // Process subgroup
                    process_local(trees,
                                  i_pass,
                                  filename_out_root,
                                  radius2,
                                  M_min,
                                  i_snap,
                                  i_subgroup,
                                  j_subgroup,
                                  i_group,
                                  subgroup_id,
                                  subgroup_file_offset,
                                  subgroup_type,
                                  subgroup_n_particles_peak,
                                  subgroup_tree_id,
                                  subgroup_index,
                                  subgroup_descendant_id,
                                  subgroup_forematch_first_index,
                                  subgroup_forematch_first_file,
                                  subgroup_backmatch_index,
                                  subgroup_backmatch_file,
                                  group_id,
                                  &properties_subgroups,
                                  &properties_groups,
                                  &n_subgroups_list,
                                  subgroup_list,
                                  x_cen,
                                  y_cen,
                                  z_cen);
                }
            }
            fclose_catalog(&fp_properties_groups);
            fclose_catalog(&fp_properties_subgroups);
            SID_fclose(&fp_in_trees);
            SID_fclose(&fp_in_bridge_forematch);
            SID_fclose(&fp_in_bridge_backmatch);
            if(i_pass == 2)
                SID_log("Done.", SID_LOG_CLOSE);
        } // i_snap
        if(i_pass == 0) {
            group_list       = (int *)SID_malloc(sizeof(int) * n_groups_list);
            subgroup_list    = (int *)SID_malloc(sizeof(int) * n_subgroups_list);
            n_groups_list    = 0;
            n_subgroups_list = 0;
        } else if(i_pass == 1) {
            SID_log("(%d groups and %d subgroups found)...", SID_LOG_CONTINUE, n_groups_list, n_subgroups_list);
            // Write list files
            for(int i_type = 0; i_type < 2; i_type++) {
                char filename_out[SID_MAX_FILENAME_LENGTH];
                int  n_list    = 0;
                int *halo_list = NULL;
                if(i_type == 0) {
                    sprintf(filename_out, "%s_group_list.txt", filename_out_root);
                    n_list    = n_groups_list;
                    halo_list = group_list;
                } else {
                    sprintf(filename_out, "%s_subgroup_list.txt", filename_out_root);
                    n_list    = n_subgroups_list;
                    halo_list = subgroup_list;
                }
                FILE *fp_out = fopen(filename_out, "w");
                fprintf(fp_out, "# Halo IDs in list of tree tracks with base {%s}\n", filename_out_root);
                for(int i_list = 0; i_list < n_list; i_list++)
                    fprintf(fp_out, "%d\n", halo_list[i_list]);
                fclose(fp_out);
            }
        }
        SID_log("Done.", SID_LOG_CLOSE);
    }

    // Clean-up
    SID_free(SID_FARG group_list);
    SID_free(SID_FARG subgroup_list);
    free_trees(&trees);

    SID_log("Done.", SID_LOG_CLOSE);
    SID_Finalize();
}