ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MCGIDI_pop.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file MCGIDI_pop.cc
1 /*
2 # <<BEGIN-copyright>>
3 # <<END-copyright>>
4 */
5 
6 #include <string.h>
7 #include <PoPs.h>
8 
9 #include "MCGIDI.h"
10 
11 #if defined __cplusplus
12 namespace GIDI {
13 using namespace GIDI;
14 #endif
15 
16 /*
17 ************************************************************
18 */
20 
21  MCGIDI_POPs *pops;
22 
23  if( ( pops = (MCGIDI_POPs *) smr_malloc2( smr, sizeof( MCGIDI_POPs ), 0, "pops->sorted" ) ) == NULL ) return( NULL );
24  if( MCGIDI_POPs_initial( smr, pops, size ) ) smr_freeMemory( (void **) &pops );
25  return( pops );
26 }
27 /*
28 ************************************************************
29 */
30 int MCGIDI_POPs_initial( statusMessageReporting * /*smr*/, MCGIDI_POPs *pops, int size ) {
31 
32  memset( pops, 0, sizeof( MCGIDI_POPs ) );
33  if( size < 10 ) size = 10;
34  pops->increment = size;
35 
36  return( 0 );
37 }
38 /*
39 ************************************************************
40 */
41 void *MCGIDI_POPs_free( MCGIDI_POPs *pops ) {
42 
43  if( pops == NULL ) return( NULL );
44  MCGIDI_POPs_release( pops );
45  smr_freeMemory( (void **) &pops );
46  return( NULL );
47 }
48 /*
49 ************************************************************
50 */
52 
53  MCGIDI_POP *pop, *next;
54 
55  if( pops == NULL ) return( 0 );
56  for( pop = pops->first; pop != NULL; pop = next ) {
57  next = pop->next;
58  MCGIDI_POP_free( pop );
59  }
60  smr_freeMemory( (void **) &(pops->sorted) );
61  MCGIDI_POPs_initial( NULL, pops, 0 );
62  return( 0 );
63 }
64 /*
65 ************************************************************
66 */
68  double level_MeV, MCGIDI_POP *parent, int globalParticle ) {
69 
70  int i, index;
71  MCGIDI_POP *pop;
72 
73  if( ( index = MCGIDI_POPs_findParticleIndex( pops, name ) ) >= 0 ) return( pops->sorted[index] );
74  if( pops->size == pops->numberOfPOPs ) {
75  int size = pops->size + pops->increment;
76  MCGIDI_POP **sorted = (MCGIDI_POP **) smr_malloc2( smr, size * sizeof( MCGIDI_POP * ), 0, "sorted" );
77 
78  if( sorted == NULL ) return( NULL );
79  for( i = 0; i < pops->numberOfPOPs; i++ ) sorted[i] = pops->sorted[i];
80  smr_freeMemory( (void **) &(pops->sorted) );
81  pops->sorted = sorted;
82  pops->size = size;
83  }
84  index = -index - 1;
85  if( ( pop = MCGIDI_POP_new( smr, name, mass_MeV, level_MeV, parent ) ) == NULL ) return( NULL );
86  for( i = pops->numberOfPOPs; i > index; i-- ) pops->sorted[i] = pops->sorted[i-1];
87  pops->sorted[index] = pop;
88  if( pops->first == NULL ) {
89  pops->first = pop; }
90  else {
91  pops->last->next = pop;
92  }
93  pops->last = pop;
94  pops->numberOfPOPs++;
95  pop->globalPoPsIndex = -1;
96  if( globalParticle ) {
97  if( ( pop->globalPoPsIndex = lPoPs_addParticleIfNeeded( smr, name, "LLNL" ) ) < 0 ) return( NULL );
98  }
99  return( pop );
100 }
101 /*
102 ************************************************************
103 */
104 int MCGIDI_POPs_findParticleIndex( MCGIDI_POPs *pops, char const *name ) {
105 
106  int iCmp = 0, min = 0, mid = 0, max = pops->numberOfPOPs;
107 
108  if( max == 0 ) return( -1 );
109  while( ( max - min ) > 1 ) {
110  mid = ( min + max ) / 2;
111  iCmp = strcmp( name, pops->sorted[mid]->name );
112  if( iCmp == 0 ) return( mid );
113  if( iCmp < 0 ) {
114  max = mid; }
115  else {
116  min = mid;
117  }
118  } // Loop checking, 11.05.2015, T. Koi
119  if( max == 1 ) { /* First point is not checked as loop exits when ( max = 1 ) - ( min = 0 ) !> 1 ). */
120  if( strcmp( name, pops->sorted[0]->name ) == 0 ) return( 0 );
121  }
122  if( max < pops->numberOfPOPs ) {
123  if( strcmp( name, pops->sorted[max]->name ) == 0 ) return( max );
124  }
125  if( max == 1 ) {
126  if( strcmp( name, pops->sorted[0]->name ) < 0 ) return( -1 );
127  }
128  return( -max - 1 );
129 }
130 /*
131 ************************************************************
132 */
134 
135  int index = MCGIDI_POPs_findParticleIndex( pops, name );
136 
137  if( index < 0 ) return( NULL );
138  return( pops->sorted[index] );
139 }
140 /*
141 ************************************************************
142 */
144 
145  int i;
146 
147  fprintf( f, "POPs Information: n = %d\n", pops->numberOfPOPs );
148  for( i = 0; i < pops->numberOfPOPs; i++ ) fprintf( f, " %-20s %e\n", pops->sorted[i]->name, pops->sorted[i]->mass_MeV );
149 }
150 /*
151 ************************************************************
152 */
154 
155  MCGIDI_POPs_writeSortedList( pops, stdout );
156 }
157 
158 
159 /*
160 ********* MCGIDI_POP routines *********
161 */
162 /*
163 ************************************************************
164 */
165 MCGIDI_POP *MCGIDI_POP_new( statusMessageReporting *smr, char const *name, double mass_MeV, double level_MeV, MCGIDI_POP *parent ) {
166 
167  int Z, A, m, level;
168  MCGIDI_POP *pop = (MCGIDI_POP *) smr_malloc2( smr, sizeof( MCGIDI_POP ), 0, "pop" );
169 
170  if( pop == NULL ) return( NULL );
171  pop->next = NULL;
172  pop->parent = parent;
173  if( ( pop->name = smr_allocateCopyString2( smr, name, "pop->name" ) ) == NULL ) {
174  smr_freeMemory( (void **) &pop );
175  return( NULL );
176  }
177  MCGIDI_miscNameToZAm( smr, name, &Z, &A, &m, &level );
178  pop->Z = Z;
179  pop->A = A;
180  pop->level = level;
181  pop->m = m;
182  pop->mass_MeV = mass_MeV;
183  pop->level_MeV = level_MeV;
184  pop->numberOfGammaBranchs = 0;
185  pop->gammas = NULL;
186  return( pop );
187 }
188 /*
189 ************************************************************
190 */
192 
193  if( pop == NULL ) return( NULL );
194  MCGIDI_POP_release( pop );
195  smr_freeMemory( (void **) &pop );
196  return( NULL );
197 }
198 /*
199 ************************************************************
200 */
202 
203  if( pop == NULL ) return( NULL );
204  smr_freeMemory( (void **) &(pop->name) );
205  pop->numberOfGammaBranchs = 0;
206  if( pop->gammas != NULL ) smr_freeMemory( (void **) &(pop->gammas) );
207  return( NULL );
208 }
209 /*
210 ************************************************************
211 */
213 
214  return( pop->mass_MeV );
215 }
216 
217 #if defined __cplusplus
218 }
219 #endif
220