Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- /* The following defines the structure of info for
- one particle. The relevant fields are:
- - type: particle type
- - momentum: px py pz p0 mass
- - position: rx ry rz r0
- - freezeout: frrx frry frrz frr0
- - lastcoll: time of last collision
- - origin: origin tag. Ignored in this code */
- typedef struct {
- float momentum[5];
- float position[4];
- float freezeout[4];
- float lastcoll;
- int type;
- int origin;
- } particle;
- enum { MAX_READ_BYTES = 1024 };
- int main(void) {
- FILE *datafile = fopen("data.txt", "r"); /* File of data */
- /* FILE *config = fopen("config.txt", "r"); */
- /* Initialize output files (and maybe structs?) */
- analyze(datafile);
- fclose(datafile);
- /* fclose(config); */
- return 0;
- }
- void analyze( FILE *data ) {
- char buffer[MAX_READ_BYTES];
- char first_three[4];
- int num_particles;
- int dum1, fl1, fl2, dum2, dum3;
- particle *event_data;
- /* Must start with a fileheader */
- fgets(buffer, MAX_READ_BYTES, data);
- strncpy(first_three, buffer, 3);
- first_three[3] = '\0';
- assert( strcmp(first_three, "OSC") == 0 );
- /* Drop two lines and parse the next */
- fgets(buffer, MAX_READ_BYTES, data);
- fgets(buffer, MAX_READ_BYTES, data);
- while (fgets(buffer, MAX_READ_BYTES, data) != NULL) {
- strncpy(first_three, buffer, 3);
- first_three[3] = '\0';
- if( strcmp(first_three, "OSC") == 0 ) {
- fgets(buffer, MAX_READ_BYTES, data);
- fgets(buffer, MAX_READ_BYTES, data);
- }
- sscanf(&buffer, "%d %d %f %f %d %d", &dum1, &num_particles,
- &fl1, &fl2, &dum2, &dum3);
- *event_data = particle_init(num_particles);
- read_event_data(data, event_data);
- printf("%f", event_data[1].momentum[2]); /* TEST LINE */
- particle_free(event_data);
- }
- }
- void read_event_data (FILE data, particle *event_data) {
- int i;
- float px, py, pz, p0, mass, rx, ry, rz, r0,
- frrx, frry, frrz, frr0, lastcoll1;
- int origin1, type1, dum1;
- for(i = 0; i <= num; i++) {
- particle *current_particle = &event_data[i];
- fscanf(data, "%d2 %f14 %d", &dum1, &type1, &px1, &py1, &pz1, &p01,
- &mass1, &rx1, &ry1, &rz1, &r01, &lastcoll1,
- &frrx1, &frry1, &frrz1, &frr01, &origin1);
- current_particle.type = type1;
- current_particle.momentum[0] = px;
- current_particle.momentum[1] = py;
- current_particle.momentum[2] = pz;
- current_particle.momentum[3] = p0;
- current_particle.momentum[4] = mass;
- current_particle.position[0] = rx;
- current_particle.position[1] = ry;
- current_particle.position[2] = rz;
- current_particle.position[3] = r0;
- current_particle.lastcoll = lastcoll1;
- current_particle.freezeout[0] = frrx;
- current_particle.freezeout[1] = frry;
- current_particle.freezeout[2] = frrz;
- current_particle.freezeout[3] = frr0;
- current_particle.origin = origin1;
- }
- }
- /* Following two functions are from Hao Lian. */
- /* Dynamically allocates an "amount" amount of particles and
- returns the pointer. The client must later call
- particle_free() on the pointer to avoid a memory leak. */
- int particle_init(int amount) {
- particle *particles = (particle *) malloc(sizeof(particle) * amount);
- return particles;
- }
- /* Frees the particles allocated by particle_init(). Every
- particle_init() call needs to be paired with a
- particle_free() call. */
- void particle_free(particle *particles) {
- free(particles);
- }
Add Comment
Please, Sign In to add comment