group_spline.pl
Go to the documentation of this file.
1 #!/usr/bin/perl
2 #---------------------------------------------------------------------
3 # Given a directory, the script looks for file in the form
4 # v*_on_*_*.xml
5 # then it calls the appropriate gspladd to obtain v*_on_*.xml comprehensive
6 # file. The file name will be total_xsec.xml
7 # The scpript also prodces a root output for the splines
8 #
9 # Options:
10 # [--dir] : Is the directory which contains all the xml files to be converted. Default: $PWD
11 # [--tune] : Tune option
12 # [--add-list] : additional file list to be included when the total_xsec.xml file is created
13 # [--root-output] : Create an output file with all the splines
14 # [--def-nu-list] : Default list of neutrino PDG to be used if that cannot be derived from local files. Default 14
15 # [--evet-gen-list] : Event generator list used in the root file output
16 # [--add-nucleons] : When the ROOT file is created, also splines for proton and neutrons are created
17 # [--save-space] : remove intermadiate xml files
18 #
19 #---------------------------------------------------------------------
20 
21 $iarg=0;
22 foreach (@ARGV) {
23  if($_ eq '--dir') { $dir = $ARGV[$iarg+1]; }
24  if($_ eq '--tune') { $tune = $ARGV[$iarg+1]; }
25  if($_ eq '--add-list') { $add_list = $ARGV[$iarg+1]; }
26  if($_ eq '--root-output') { $root_output = 1 ; }
27  if($_ eq '--event-gen-list') { $event_gen_list = $ARGV[$iarg+1]; }
28  if($_ eq '--def-nu-list') { $def_nu_list = $ARGV[$iarg+1]; }
29  if($_ eq '--add-nucleons') { $add_nucleons = 1 ; }
30  if($_ eq '--save-space' ) { $save_space = 1 ; }
31  $iarg++;
32 }
33 
34 $dir=$ENV{'PWD'} unless defined $dir;
35 $def_nu_list = "14" unless defined $def_nu_list ;
36 
37 opendir(DIR, $dir) or die $!;
38 
39 %nus = ();
40 %tags = ();
41 %lists = ();
42 
43 while (my $file = readdir(DIR) ) {
44 
45  # We only want files
46  next unless (-f "$dir/$file");
47 
48  # Use a regular expression to find files ending in .xml
49  next unless ($file =~ m/\.xml$/);
50 
51  next unless ( index($file, "_on_") != -1 );
52 
53  my $under_counter = $file =~ tr/_//;
54  next unless ( $under_counter == 3 );
55 
56  my $nu = substr($file, 0, index($file, '_') );
57  if ( ! exists( $nus{$nu} ) ) { $nus{$nu}=1; }
58 
59  my $tgt = substr($file, index($file, "on_")+3, rindex($file, '_')-index($file, "on_")-3 );
60  if ( ! exists( $tgts{$tgt} ) ) { $tgts{$tgt}=1; }
61 
62  my $list = substr($file, rindex($file, '_')+1, index($file,'.')-rindex($file, '_')-1 );
63  if ( ! exists( $lists{$list} ) ) { $lists{$list}=1; }
64 
65  print "$file \n";
66 
67 }
68 
69 closedir(DIR);
70 
71 print " Neutrinos: \n";
72 foreach my $nu ( keys %nus ) {
73  print "$nu ";
74 }
75 
76 print "\n targets: \n";
77 foreach my $tgt ( keys %tgts ) {
78  print "$tgt ";
79 }
80 
81 print "\n Processes: \n";
82 foreach my $proc ( keys %lists ) {
83  print "$proc ";
84 }
85 print "\n";
86 
87 $nus{'ve'}=12 if ( exists( $nus{'ve'} ) );
88 $nus{'vebar'}=-12 if ( exists( $nus{'vebar'} ) );
89 $nus{'vmu'}=14 if ( exists( $nus{'vmu'} ) );
90 $nus{'vmubar'}=-14 if ( exists( $nus{'vmubar'} ) );
91 $nus{'vtau'}=16 if ( exists( $nus{'vtau'} ) );
92 $nus{'vtaubar'}=-16 if ( exists( $nus{'vtaubar'} ) );
93 
94 $nu_list="";
95 foreach $nu ( keys %nus ) {
96  $nu_list .= "," if ( $nu_list ne "" );
97  $nu_list .= $nus{$nu};
98 }
99 ##print $nu_list."\n";
100 
101 $proc_list = "";
102 foreach $proc ( keys %lists ) {
103  $proc_list .= "," if ($proc_list ne "" );
104  $proc_list .= $proc ;
105 }
106 
107 $tgt_file_list = "";
108 $tgt_list = "";
109 
110 foreach my $tgt ( keys %tgts ) {
111 
112  $tmp_tgt;
113  if ( length($tgt) == 1 ) {
114  $tmp_tgt = 1000010010 if ( $tgt eq 'p' );
115  $tmp_tgt = 1000000010 if ( $tgt eq 'n' );
116  }
117  else { $tmp_tgt = $tgt; }
118 
119  $nu_file_list = "";
120 
121  foreach my $nu ( keys %nus ) {
122 
123  $proc_file_list = "";
124 
125  foreach my $proc ( keys %lists ) {
126 
127  $tmp_proc_file="$dir/".$nu."_on_".$tgt."_".$proc.".xml";
128  if ( -f $tmp_proc_file ) {
129  $proc_file_list .= "," if ( $proc_file_list ne "" );
130  $proc_file_list .= "$tmp_proc_file";
131  }
132  else {
133  print "Warning: Missing file $tmp_proc_file \n";
134  }
135  } ##proc list
136 
137  $tmp_nu_file = "$dir/".$nu."_on_".$tgt.".xml";
138 
139  $gspladd_opt = " -o $tmp_nu_file -f $proc_file_list ";
140  $gspladd_cmd = "gspladd $gspladd_opt";
141  print "$gspladd_cmd \n \n";
142  `$gspladd_cmd \n`;
143 
144  if ( -f $tmp_nu_file ) {
145  $nu_file_list .= "," if ( $nu_file_list ne "" );
146  $nu_file_list .= "$tmp_nu_file";
147  }
148  else {
149  print "Error: $tmp_nu_file not created \n";
150  }
151 
152  } ## nu loop
153 
154  $tmp_tgt_file = "$dir/".$tgt.".xml";
155 
156  $nus_size = keys %nus ;
157 
158  if ( $nus_size > 1 ) {
159  $gspladd_opt = " -o $tmp_tgt_file -f $nu_file_list ";
160  $gspladd_cmd = "gspladd $gspladd_opt";
161  print "$gspladd_cmd \n \n";
162  `$gspladd_cmd \n`;
163  }
164  elsif ( $nus_size == 1 ) {
165  $cp_cmd = "cp $nu_file_list $tmp_tgt_file ";
166  print "$cp_cmd \n \n";
167  `$cp_cmd \n`;
168  }
169  elsif ( $nus_size == 0 ) {
170  print "\nError: No Neutrino flavours \n";
171  }
172 
173  if ( -f $tmp_tgt_file ) {
174  $tgt_file_list .= "," if ( $tgt_file_list ne "" );
175  $tgt_file_list .= "$tmp_tgt_file";
176  $tgt_list .= "," if ( $tgt_list ne "" );
177  $tgt_list .= "$tmp_tgt";
178  }
179  else {
180  print "\nError: $tmp_tgt_file not created \n";
181  }
182 
183 } ##tgt loop
184 
185 $glob_file = $dir."/total_xsec.xml";
186 
187 $tgt_size = keys %tgts;
188 if ( ($tgt_size > 1) or (defined $add_list ) ) {
189 
190  if ( defined $add_list ) {
191  $tgt_file_list = "$add_list" . "," . "$tgt_file_list" ;
192  }
193 
194  if ( defined $add_nucleons ) {
195  if ( index($tgt_list, "1000010010") == -1 ) {
196  $tgt_list .= ",1000010010";
197  }
198 
199  if ( index($tgt_list, "1000000010") == -1 ) {
200  $tgt_list .= ",1000000010";
201  }
202  }
203 
204  ##$grep_pipe = "grep -B 100 -A 30 -i \"warn\\|error\\|fatal\"";
205  $gspladd_opt = " -o $glob_file -f $tgt_file_list ";
206  $gspladd_cmd = "gspladd $gspladd_opt";
207  print "$gspladd_cmd \n \n";
208  `$gspladd_cmd \n`;
209 }
210 elsif ( $tgt_size == 1 ) {
211 
212  ###if there is only one target, the global file has to be created manually
213  my $temp_name = "$dir/".(keys %tgts)[0].".xml";
214 
215  my $cmd = "ln -s ";
216  $cmd = "cp " if defined $save_space ;
217 
218  $cmd .= " $temp_name $glob_file ";
219  print "Executing: $cmd \n" ;
220  `$cmd` ;
221 }
222 
223 if ( defined $root_output ) {
224 
225 ##
226 ## Create an output file with all the splines in root format
227 ##
228 
229  if ( $nu_list eq "" ) {
230  $nu_list = $def_nu_list ;
231  }
232 
233  if ( index($tgt_list, "1000010010") == -1 ) {
234  $tgt_list .= "," unless ( $tgt_list eq "" ) ;
235  $tgt_list .= "1000010010" ;
236  }
237 
238  if ( index($tgt_list, "1000000010") == -1 ) {
239  $tgt_list .= "," unless ( $tgt_list eq "" ) ;
240  $tgt_list .= "1000000010";
241  }
242 
243  my $cmd = "gspl2root ";
244  $cmd .= " -p $nu_list ";
245  $cmd .= " -t $tgt_list ";
246  $cmd .= " -f $glob_file ";
247  $cmd .= " -o $dir"."/total_xsec.root " ;
248  if ( defined $event_gen_list ) {
249  $cmd .= " --event-generator-list $event_gen_list "
250  }
251  $cmd .= " --tune $tune " if ( defined $tune ) ;
252  print "\n$cmd\n";
253  `$cmd`;
254 
255 }
256 
257 
258 if ( defined $save_space ) {
259 
260 ##
261 ## removing intermediate xml file
262 ##
263 
264  foreach my $tgt ( keys %tgts ) {
265 
266  foreach my $nu ( keys %nus ) {
267 
268  my $tmp_nu_file = "$dir/".$nu."_on_".$tgt.".xml";
269 
270  if ( -f $tmp_nu_file ) {
271  `rm $tmp_nu_file` ;
272  }
273 
274  } ## nu loop
275 
276  my $tmp_tgt_file = "$dir/".$tgt.".xml";
277 
278  if ( -f $tmp_tgt_file ) {
279  `rm $tmp_tgt_file` ;
280  }
281 
282  } ##tgt loop
283 
284 } ## if defined $save_space
285 
286 
287 
288