136 {
138 FILE *fileID = NULL;
139 SCANNED_ARG *s_arg;
140 long i_arg;
141 char *input, *output;
142 unsigned long pipeFlags = 0;
143 long noWarnings = 0, tmpfile_used = 0;
144 long retval, sddsOutput = 1, steer = 0, firstOne, sliceOffset = 0;
145 int64_t i, j, rows, origRows;
146 long reverseOrder = 0;
147 char *chargeParameter;
148 unsigned long removePTailsFlags = 0;
149 double pTailsDeltaLimit, term;
150 char *pTailsOutputFile = NULL;
151 long localFit = 0;
152 short *selected;
153
154 char column[7][15] = {"x", "xp", "y", "yp", "t", "p", "particleID"};
155 long columnIndex[7];
156
157 double *tValues, *sValues, *xValues, *xpValues, *yValues, *ypValues, *pValues;
158 double sAverage, pAverage, pStDev;
159 double xAverage, xpAverage, xRMS, x2Average, xp2Average, xxpAverage, xEmittance;
160 double yAverage, ypAverage, yRMS, y2Average, yp2Average, yypAverage, yEmittance;
161 double sMin = 1, sMax = -1, s1, s2;
162
163
164
165 double alphax, alphay;
166
167 double wavelength = 0.0001;
168 double totalCharge = 0;
169 double current;
170 long slices = 4;
171 long tmp = 0;
172 long N = 0;
173 double Ne = 0;
174
175 input = output = NULL;
176 sValues = NULL;
177 chargeParameter = NULL;
178 selected = NULL;
179
181 argc =
scanargs(&s_arg, argc, argv);
182 if (argc < 3) {
183 fprintf(stderr, "Error: Insufficient arguments.\n");
184 fprintf(stderr, "%s", USAGE);
185 exit(EXIT_FAILURE);
186 }
187
188
189 for (i_arg = 1; i_arg < argc; i_arg++) {
190 if (s_arg[i_arg].arg_type == OPTION) {
191 switch (
match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) {
192 case SET_TOTALCHARGE:
193 if (s_arg[i_arg].n_items != 2) {
194 fprintf(stderr, "Error: Invalid -totalCharge syntax.\n");
195 exit(EXIT_FAILURE);
196 }
197 if (sscanf(s_arg[i_arg].list[1], "%lf", &totalCharge) != 1) {
198 fprintf(stderr, "Error: Invalid -totalCharge value.\n");
199 exit(EXIT_FAILURE);
200 }
201 break;
202 case SET_WAVELENGTH:
203 if (tmp == 2) {
204 fprintf(stderr, "Error: -wavelength and -slices cannot be used together.\n");
205 exit(EXIT_FAILURE);
206 }
207 if (s_arg[i_arg].n_items != 2) {
208 fprintf(stderr, "Error: Invalid -wavelength syntax.\n");
209 exit(EXIT_FAILURE);
210 }
211 if (sscanf(s_arg[i_arg].list[1], "%lf", &wavelength) != 1 || wavelength <= 0) {
212 fprintf(stderr, "Error: Invalid -wavelength value.\n");
213 exit(EXIT_FAILURE);
214 }
215 tmp = 1;
216 break;
217 case SET_SLICES:
218 if (tmp == 1) {
219 fprintf(stderr, "Error: -wavelength and -slices cannot be used together.\n");
220 exit(EXIT_FAILURE);
221 }
222 if (s_arg[i_arg].n_items != 2) {
223 fprintf(stderr, "Error: Invalid -slices syntax.\n");
224 exit(EXIT_FAILURE);
225 }
226 if (sscanf(s_arg[i_arg].list[1], "%ld", &slices) != 1 || slices <= 0) {
227 fprintf(stderr, "Error: Invalid -slices value.\n");
228 exit(EXIT_FAILURE);
229 }
230 tmp = 2;
231 break;
232 case SET_TEXTOUTPUT:
233 sddsOutput = 0;
234 break;
235 case SET_STEER:
236 steer = 1;
237 break;
238 case SET_CHARGEPARAMETER:
239 if (s_arg[i_arg].n_items != 2) {
240 fprintf(stderr, "Error: Invalid -chargeParameter syntax.\n");
241 exit(EXIT_FAILURE);
242 }
243 chargeParameter = s_arg[i_arg].list[1];
244 break;
245 case SET_PIPE:
246 if (!
processPipeOption(s_arg[i_arg].list + 1, s_arg[i_arg].n_items - 1, &pipeFlags)) {
248 }
249 break;
250 case SET_REMPTAILS:
251 if (s_arg[i_arg].n_items > 1) {
252 if (!
scanItemList(&removePTailsFlags, s_arg[i_arg].list + 1, &s_arg[i_arg].n_items, 0,
253 "deltalimit",
SDDS_DOUBLE, &pTailsDeltaLimit, 1, PTAILS_DELTALIMIT,
254 "fit", -1, NULL, 0, PTAILS_FITMODE,
255 "beamoutput",
SDDS_STRING, &pTailsOutputFile, 1, PTAILS_OUTPUT,
256 NULL) ||
257 !(removePTailsFlags & PTAILS_DELTALIMIT) || pTailsDeltaLimit <= 0) {
258 SDDS_Bomb(
"Invalid -removePTails syntax or values.");
259 }
260 }
261 break;
262 case SET_REVERSEORDER:
263 reverseOrder = 1;
264 break;
265 case SET_LOCAL_FIT:
266 localFit = 1;
267 break;
268 default:
269 fprintf(stderr, "Error: Unknown option '%s'.\n", s_arg[i_arg].list[0]);
270 exit(EXIT_FAILURE);
271 }
272 } else {
273 if (input == NULL) {
274 input = s_arg[i_arg].list[0];
275 } else if (output == NULL) {
276 output = s_arg[i_arg].list[0];
277 } else {
278 fprintf(stderr, "Error: Too many filenames provided.\n");
279 exit(EXIT_FAILURE);
280 }
281 }
282 }
283
284 processFilenames(
"elegant2genesis", &input, &output, pipeFlags, noWarnings, &tmpfile_used);
285
286
289 exit(EXIT_FAILURE);
290 }
291
292
293 for (i = 0; i < 7; i++) {
295 fprintf(stderr, "Error: Column '%s' does not exist.\n", column[i]);
296 exit(EXIT_FAILURE);
297 }
298 }
299
300 if (removePTailsFlags & PTAILS_OUTPUT) {
310 exit(EXIT_FAILURE);
311 }
312 }
313
314 retval = -1;
315 firstOne = 1;
317 rows = origRows = SDDS_RowCount(&SDDS_input);
318 if (rows < 1) {
319 fprintf(stderr, "Error: No rows found for page %ld.\n", retval);
320 exit(EXIT_FAILURE);
321 }
322
329
331 SDDS_Bomb(
"Unable to read charge from file.");
332 }
333 if (!(tValues && xValues && xpValues && yValues && ypValues && pValues)) {
334 fprintf(stderr, "Error: Invalid data for page %ld.\n", retval);
335 exit(EXIT_FAILURE);
336 }
337 selected = realloc(selected, sizeof(*selected) * rows);
338 sValues = realloc(sValues, sizeof(*sValues) * rows);
339 if (!selected || !sValues) {
341 }
342
343 sAverage = xAverage = xpAverage = yAverage = ypAverage = 0;
344 for (i = 0; i < rows; i++) {
345 sValues[i] = tValues[i] * 2.99792458e8;
346 sAverage += tValues[i];
347 xAverage += xValues[i];
348 xpAverage += xpValues[i];
349 yAverage += yValues[i];
350 ypAverage += ypValues[i];
351 }
352 sAverage = sAverage * 2.99792458e8 / rows;
353 xAverage /= rows;
354 xpAverage /= rows;
355 yAverage /= rows;
356 ypAverage /= rows;
357
358 for (i = 0; i < rows; i++) {
359
360 sValues[i] = sAverage - sValues[i];
361 }
362
363
364 if (removePTailsFlags) {
365 rows = removeMomentumTails(xValues, xpValues, yValues, ypValues, sValues, pValues, tValues, rows, pTailsDeltaLimit, removePTailsFlags);
366 if (rows == 0) {
367 SDDS_Bomb(
"All data removed by P-tails filter.");
368 }
369 if (removePTailsFlags & PTAILS_OUTPUT) {
371 !
SDDS_SetColumn(&SDDS_pTails, SDDS_SET_BY_NAME, xValues, rows,
"x") ||
372 !
SDDS_SetColumn(&SDDS_pTails, SDDS_SET_BY_NAME, xpValues, rows,
"xp") ||
373 !
SDDS_SetColumn(&SDDS_pTails, SDDS_SET_BY_NAME, yValues, rows,
"y") ||
374 !
SDDS_SetColumn(&SDDS_pTails, SDDS_SET_BY_NAME, ypValues, rows,
"yp") ||
375 !
SDDS_SetColumn(&SDDS_pTails, SDDS_SET_BY_NAME, tValues, rows,
"t") ||
376 !
SDDS_SetColumn(&SDDS_pTails, SDDS_SET_BY_NAME, pValues, rows,
"p") ||
379 exit(EXIT_FAILURE);
380 }
381 }
382 }
383
384 if (steer) {
385
386 for (i = 0; i < rows; i++) {
387 xValues[i] -= xAverage;
388 xpValues[i] -= xpAverage;
389 yValues[i] -= yAverage;
390 ypValues[i] -= ypAverage;
391 }
392 }
393
394
396 if ((tmp == 1) || (tmp == 0)) {
397 slices = (sMax - sMin) / wavelength + 1;
398 }
399 if (tmp == 2) {
400 wavelength = (sMax - sMin) / slices;
401 }
402
403 if (firstOne) {
404
405 if (sddsOutput) {
408 exit(EXIT_FAILURE);
409 }
430 exit(EXIT_FAILURE);
431 }
434 exit(EXIT_FAILURE);
435 }
436 } else {
437 fileID = fopen(output, "w");
438 if (fileID == NULL) {
439 fprintf(stderr, "Error: Unable to open output file for writing.\n");
440 exit(EXIT_FAILURE);
441 }
442 fprintf(fileID, "%ld", slices);
443 }
444 firstOne = 0;
445 } else {
448 exit(EXIT_FAILURE);
449 }
450 sliceOffset += slices;
451 }
452
453
454 for (i = 0; i < slices; i++) {
455 N = 0;
456 xAverage = xpAverage = xRMS = x2Average = xp2Average = xxpAverage = 0;
457 yAverage = ypAverage = yRMS = y2Average = yp2Average = yypAverage = 0;
458 pAverage = alphax = alphay = 0;
459
460 if (!reverseOrder) {
461 s1 = sMin + (wavelength * i);
462 s2 = sMin + (wavelength * (i + 1));
463 } else {
464 s1 = sMin + (wavelength * (slices - i - 1));
465 s2 = sMin + (wavelength * (slices - i));
466 }
467
468 for (j = 0; j < rows; j++) {
469 selected[j] = 0;
470 if ((s1 <= sValues[j]) && (s2 > sValues[j])) {
471 selected[j] = 1;
472 N++;
473 xAverage += xValues[j];
474 xpAverage += xpValues[j];
475 yAverage += yValues[j];
476 ypAverage += ypValues[j];
477 pAverage += pValues[j];
478 }
479 }
480
481 if (N > 2) {
482 current = N * 2.99792458e8 * (totalCharge / (origRows * wavelength));
483 Ne = totalCharge * N / (e_mks * origRows);
484 xAverage /= N;
485 yAverage /= N;
486 xpAverage /= N;
487 ypAverage /= N;
488 pAverage /= N;
489
490 pStDev = 0;
491 if (localFit) {
492 removeLocalFit(pValues, sValues, selected, pAverage, rows);
493 }
494 for (j = 0; j < rows; j++) {
495 if (selected[j]) {
496 pStDev += (pValues[j] - pAverage) * (pValues[j] - pAverage);
497 x2Average += (xValues[j] - xAverage) * (xValues[j] - xAverage);
498 y2Average += (yValues[j] - yAverage) * (yValues[j] - yAverage);
499 xp2Average += (xpValues[j] - xpAverage) * (xpValues[j] - xpAverage);
500 yp2Average += (ypValues[j] - ypAverage) * (ypValues[j] - ypAverage);
501 xxpAverage += (xValues[j] - xAverage) * (xpValues[j] - xpAverage);
502 yypAverage += (yValues[j] - yAverage) * (ypValues[j] - ypAverage);
503 }
504 }
505 pStDev = sqrt(pStDev / (N - 1.0));
506 x2Average /= N;
507 y2Average /= N;
508 xp2Average /= N;
509 yp2Average /= N;
510 xxpAverage /= N;
511 yypAverage /= N;
512 xRMS = sqrt(x2Average);
513 yRMS = sqrt(y2Average);
514
515 if ((term = (x2Average * xp2Average) - (xxpAverage * xxpAverage)) > 0) {
516 xEmittance = sqrt(term) * pAverage;
517 } else {
518 xEmittance = 0;
519 }
520
521 if ((term = (y2Average * yp2Average) - (yypAverage * yypAverage)) > 0) {
522 yEmittance = sqrt(term) * pAverage;
523 } else {
524 yEmittance = 0;
525 }
526
527 alphax = -xxpAverage / ((xEmittance > 0) ? (xEmittance / pAverage) : 1);
528 alphay = -yypAverage / ((yEmittance > 0) ? (yEmittance / pAverage) : 1);
529 } else {
530 pAverage = pStDev = xEmittance = yEmittance = xRMS = yRMS = xAverage = yAverage = xpAverage = ypAverage = alphax = alphay = current = Ne = 0;
531 }
532
533 if (sddsOutput) {
534 if (
SDDS_SetRowValues(&SDDS_output, SDDS_SET_BY_NAME | SDDS_PASS_BY_VALUE, i + sliceOffset,
535 "s", s1,
536 "t", -s1 / c_mks,
537 "gamma", pAverage,
538 "dgamma", pStDev,
539 "Sdelta", (pAverage > 0) ? (pStDev / pAverage) : 0,
540 "xemit", xEmittance,
541 "yemit", yEmittance,
542 "xrms", xRMS,
543 "yrms", yRMS,
544 "xavg", xAverage,
545 "yavg", yAverage,
546 "pxavg", xpAverage,
547 "pyavg", ypAverage,
548 "alphax", alphax,
549 "alphay", alphay,
550 "current", current,
551 "wakez", 0.0,
552 "N", N,
553 "Ne", Ne,
554 NULL) != 1) {
556 exit(EXIT_FAILURE);
557 }
558 } else {
559 fprintf(fileID, "\n%.6E %.6E %.6E %.6E %.6E %.6E %.6E %.6E %.6E %.6E %.6E %.6E %.6E %.6E 0.000000E+00",
560 s1, pAverage, pStDev, xEmittance, yEmittance, xRMS, yRMS,
561 xAverage, yAverage, xpAverage, ypAverage, alphax, alphay, current);
562 }
563 }
564
565 free(tValues);
566 free(xValues);
567 free(xpValues);
568 free(yValues);
569 free(ypValues);
570 free(pValues);
571 }
572 free(sValues);
573
574
575 if (sddsOutput) {
578 exit(EXIT_FAILURE);
579 }
582 exit(EXIT_FAILURE);
583 }
584 } else {
585 fclose(fileID);
586 }
587
588
591 exit(EXIT_FAILURE);
592 }
593 if ((removePTailsFlags & PTAILS_OUTPUT) && !
SDDS_Terminate(&SDDS_pTails)) {
595 exit(EXIT_FAILURE);
596 }
597
598 return EXIT_SUCCESS;
599}
int32_t SDDS_LengthenTable(SDDS_DATASET *SDDS_dataset, int64_t n_additional_rows)
int32_t SDDS_SetRowValues(SDDS_DATASET *SDDS_dataset, int32_t mode, int64_t row,...)
int32_t SDDS_StartPage(SDDS_DATASET *SDDS_dataset, int64_t expected_n_rows)
int32_t SDDS_SetColumn(SDDS_DATASET *SDDS_dataset, int32_t mode, void *data, int64_t rows,...)
Sets the values for one data column in the current data table of an SDDS dataset.
int32_t SDDS_InitializeOutput(SDDS_DATASET *SDDS_dataset, int32_t data_mode, int32_t lines_per_row, const char *description, const char *contents, const char *filename)
Initializes the SDDS output dataset.
int32_t SDDS_DefineSimpleColumn(SDDS_DATASET *SDDS_dataset, const char *name, const char *unit, int32_t type)
Defines a simple data column within the SDDS dataset.
int32_t SDDS_WritePage(SDDS_DATASET *SDDS_dataset)
Writes the current data table to the output file.
int32_t SDDS_DefineColumn(SDDS_DATASET *SDDS_dataset, const char *name, const char *symbol, const char *units, const char *description, const char *format_string, int32_t type, int32_t field_length)
Defines a data column within the SDDS dataset.
int32_t SDDS_WriteLayout(SDDS_DATASET *SDDS_dataset)
Writes the SDDS layout header to the output file.
int32_t SDDS_GetColumnIndex(SDDS_DATASET *SDDS_dataset, char *name)
Retrieves the index of a named column in the SDDS dataset.
void SDDS_PrintErrors(FILE *fp, int32_t mode)
Prints recorded error messages to a specified file stream.
void SDDS_RegisterProgramName(const char *name)
Registers the executable program name for use in error messages.
void SDDS_Bomb(char *message)
Terminates the program after printing an error message and recorded errors.
#define SDDS_STRING
Identifier for the string data type.
#define SDDS_LONG
Identifier for the signed 32-bit integer data type.
#define SDDS_DOUBLE
Identifier for the double data type.
int find_min_max(double *min, double *max, double *list, int64_t n)
Finds the minimum and maximum values in a list of doubles.
long match_string(char *string, char **option, long n_options, long mode)
Matches a given string against an array of option strings based on specified modes.
int scanargs(SCANNED_ARG **scanned, int argc, char **argv)
long processPipeOption(char **item, long items, unsigned long *flags)
void processFilenames(char *programName, char **input, char **output, unsigned long pipeFlags, long noWarnings, long *tmpOutputUsed)
long scanItemList(unsigned long *flags, char **item, long *items, unsigned long mode,...)
Scans a list of items and assigns values based on provided keywords and types.