Guest User

Untitled

a guest
Jul 23rd, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. /* The following defines the structure of info for
  5. one particle. The relevant fields are:
  6. - type: particle type
  7. - momentum: px py pz p0 mass
  8. - position: rx ry rz r0
  9. - freezeout: frrx frry frrz frr0
  10. - lastcoll: time of last collision
  11. - origin: origin tag. Ignored in this code */
  12.  
  13. typedef struct {
  14. float momentum[5];
  15. float position[4];
  16. float freezeout[4];
  17. float lastcoll;
  18. int type;
  19. int origin;
  20. } particle;
  21.  
  22. enum { MAX_READ_BYTES = 1024 };
  23.  
  24. int main(void) {
  25. FILE *datafile = fopen("data.txt", "r"); /* File of data */
  26. /* FILE *config = fopen("config.txt", "r"); */
  27.  
  28. /* Initialize output files (and maybe structs?) */
  29. analyze(datafile);
  30.  
  31. fclose(datafile);
  32. /* fclose(config); */
  33.  
  34. return 0;
  35. }
  36.  
  37. void analyze( FILE *data ) {
  38. char buffer[MAX_READ_BYTES];
  39. char first_three[4];
  40. int num_particles;
  41. int dum1, fl1, fl2, dum2, dum3;
  42. particle *event_data;
  43.  
  44. /* Must start with a fileheader */
  45. fgets(buffer, MAX_READ_BYTES, data);
  46. strncpy(first_three, buffer, 3);
  47. first_three[3] = '\0';
  48. assert( strcmp(first_three, "OSC") == 0 );
  49.  
  50. /* Drop two lines and parse the next */
  51. fgets(buffer, MAX_READ_BYTES, data);
  52. fgets(buffer, MAX_READ_BYTES, data);
  53.  
  54. while (fgets(buffer, MAX_READ_BYTES, data) != NULL) {
  55. strncpy(first_three, buffer, 3);
  56. first_three[3] = '\0';
  57. if( strcmp(first_three, "OSC") == 0 ) {
  58. fgets(buffer, MAX_READ_BYTES, data);
  59. fgets(buffer, MAX_READ_BYTES, data);
  60. }
  61. sscanf(&buffer, "%d %d %f %f %d %d", &dum1, &num_particles,
  62. &fl1, &fl2, &dum2, &dum3);
  63.  
  64. *event_data = particle_init(num_particles);
  65. read_event_data(data, event_data);
  66. printf("%f", event_data[1].momentum[2]); /* TEST LINE */
  67. particle_free(event_data);
  68. }
  69. }
  70.  
  71. void read_event_data (FILE data, particle *event_data) {
  72. int i;
  73. float px, py, pz, p0, mass, rx, ry, rz, r0,
  74. frrx, frry, frrz, frr0, lastcoll1;
  75. int origin1, type1, dum1;
  76.  
  77. for(i = 0; i <= num; i++) {
  78. particle *current_particle = &event_data[i];
  79. fscanf(data, "%d2 %f14 %d", &dum1, &type1, &px1, &py1, &pz1, &p01,
  80. &mass1, &rx1, &ry1, &rz1, &r01, &lastcoll1,
  81. &frrx1, &frry1, &frrz1, &frr01, &origin1);
  82. current_particle.type = type1;
  83. current_particle.momentum[0] = px;
  84. current_particle.momentum[1] = py;
  85. current_particle.momentum[2] = pz;
  86. current_particle.momentum[3] = p0;
  87. current_particle.momentum[4] = mass;
  88. current_particle.position[0] = rx;
  89. current_particle.position[1] = ry;
  90. current_particle.position[2] = rz;
  91. current_particle.position[3] = r0;
  92. current_particle.lastcoll = lastcoll1;
  93. current_particle.freezeout[0] = frrx;
  94. current_particle.freezeout[1] = frry;
  95. current_particle.freezeout[2] = frrz;
  96. current_particle.freezeout[3] = frr0;
  97. current_particle.origin = origin1;
  98. }
  99. }
  100.  
  101. /* Following two functions are from Hao Lian. */
  102.  
  103. /* Dynamically allocates an "amount" amount of particles and
  104. returns the pointer. The client must later call
  105. particle_free() on the pointer to avoid a memory leak. */
  106. int particle_init(int amount) {
  107. particle *particles = (particle *) malloc(sizeof(particle) * amount);
  108. return particles;
  109. }
  110.  
  111. /* Frees the particles allocated by particle_init(). Every
  112. particle_init() call needs to be paired with a
  113. particle_free() call. */
  114. void particle_free(particle *particles) {
  115. free(particles);
  116. }
Add Comment
Please, Sign In to add comment