inlib  1.2.0
/Users/barrand/private/dev/softinex/old/inexlib-1.2/inlib/inlib/histo/slice
Go to the documentation of this file.
00001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
00002 // See the file inlib.license for terms.
00003 
00004 #ifndef inlib_histo_slice
00005 #define inlib_histo_slice
00006 
00007 #include "h1"
00008 #include "h2"
00009 #include "h3"
00010 
00011 namespace inlib {
00012 namespace histo {
00013 
00014 template <class TC,class TN,class TW,class TH>
00015 inline bool fill_slice_x(const h2<TC,TN,TW,TH>& a_from,
00016                          int aJbeg,int aJend,
00017                          h1<TC,TN,TW,TH>& a_to) {
00018 
00019   if(!a_from.dimension()) return false;
00020   
00021   typedef typename axis<TC>::bn_t bn_t;
00022 
00023   bn_t jbeg;
00024   if(!a_from.axis_y().in_range_to_absolute_index(aJbeg,jbeg)) return false;
00025   bn_t jend;
00026   if(!a_from.axis_y().in_range_to_absolute_index(aJend,jend)) return false;
00027   if(jbeg>jend) return false;
00028 
00029   if(a_from.axis_x().bins()!=a_to.axis().bins()) return false;
00030 
00031   histo_data<TC,TN,TW> hdata = a_to.get_histo_data();
00032 
00033   bn_t aoffset,offset,jbin;     
00034   bn_t yoffset = a_from.axis_y().m_offset;
00035   
00036   const std::vector<TN>& af_bin_entries = a_from.bins_entries();
00037   const std::vector<TW>& af_bin_Sw = a_from.bins_sum_w();
00038   const std::vector<TW>& af_bin_Sw2 = a_from.bins_sum_w2();
00039   const std::vector< std::vector<TC> >& af_bin_Sxw = a_from.bins_sum_xw();
00040   const std::vector< std::vector<TC> >& af_bin_Sx2w = a_from.bins_sum_x2w();
00041 
00042   // Fill also the outflow.
00043   bn_t abins = hdata.m_axes[0].bins()+2;
00044   for(bn_t aibin=0;aibin<abins;aibin++) {
00045     //offset1D = ibin
00046     aoffset = aibin;
00047     for(jbin=jbeg;jbin<=jend;jbin++) {
00048       //offset2D = ibin + jbin * yoffset
00049       // hdata booked with x then :
00050       offset = aibin + jbin * yoffset;
00051       // Bin :
00052       hdata.m_bin_entries[aoffset] += af_bin_entries[offset];
00053       hdata.m_bin_Sw[aoffset] += af_bin_Sw[offset];
00054       hdata.m_bin_Sw2[aoffset] += af_bin_Sw2[offset];
00055       hdata.m_bin_Sxw[aoffset][0] += af_bin_Sxw[offset][0];
00056       hdata.m_bin_Sx2w[aoffset][0] += af_bin_Sx2w[offset][0];
00057     }
00058   }
00059   a_to.copy_from_data(hdata);
00060   a_to.update_fast_getters();
00061   return true;
00062 }
00063 
00064 template <class TC,class TN,class TW,class TH>
00065 inline h1<TC,TN,TW,TH>* slice_x(const h2<TC,TN,TW,TH>& a_from,
00066                              int aJbeg,int aJend,
00067                              const std::string& a_title) {
00068   h1<TC,TN,TW,TH>* slice = new h1<TC,TN,TW,TH>(a_title,
00069                                    a_from.axis_x().bins(),
00070                                    a_from.axis_x().lower_edge(),
00071                                    a_from.axis_x().upper_edge());
00072   if(!fill_slice_x(a_from,aJbeg,aJend,*slice)) {delete slice;return 0;}
00073   return slice;
00074 }
00075 
00076 template <class TC,class TN,class TW,class TH>
00077 inline h1<TC,TN,TW,TH>* projection_x(const h2<TC,TN,TW,TH>& a_from,const std::string& a_title) {
00078   return slice_x(a_from,axis<TC>::UNDERFLOW_BIN,axis<TC>::OVERFLOW_BIN,a_title);
00079 }
00080 
00081 template <class TC,class TN,class TW,class TH>
00082 inline bool fill_slice_y(const h2<TC,TN,TW,TH>& a_from,
00083                          int aIbeg,int aIend,
00084                          h1<TC,TN,TW,TH>& a_to) {
00085 
00086   if(!a_from.dimension()) return false;
00087 
00088   typedef typename axis<TC>::bn_t bn_t;
00089 
00090   bn_t ibeg;
00091   if(!a_from.axis_x().in_range_to_absolute_index(aIbeg,ibeg)) return false;
00092   bn_t iend;
00093   if(!a_from.axis_x().in_range_to_absolute_index(aIend,iend)) return false;
00094   if(ibeg>iend) return false;
00095 
00096   if(a_from.axis_y().bins()!=a_to.axis().bins()) return false;
00097 
00098   histo_data<TC,TN,TW> hdata = a_to.get_histo_data();
00099 
00100   bn_t aibin,aoffset,offset,ibin;     
00101   bn_t yoffset = a_from.axis_y().m_offset;
00102 
00103   const std::vector<TN>& af_bin_entries = a_from.bins_entries();
00104   const std::vector<TW>& af_bin_Sw = a_from.bins_sum_w();
00105   const std::vector<TW>& af_bin_Sw2 = a_from.bins_sum_w2();
00106   const std::vector< std::vector<TC> >& af_bin_Sxw = a_from.bins_sum_xw();
00107   const std::vector< std::vector<TC> >& af_bin_Sx2w = a_from.bins_sum_x2w();
00108 
00109   // Fill also the outflow.
00110   bn_t abins = hdata.m_axes[0].bins()+2;
00111   for(aibin=0;aibin<abins;aibin++) {
00112     //offset1D = ibin
00113     aoffset = aibin;
00114     for(ibin=ibeg;ibin<=iend;ibin++) {
00115       //offset2D = ibin + jbin * yoffset
00116       // hdata booked with y then :
00117       offset = ibin + aibin * yoffset;
00118       // Bin :
00119       hdata.m_bin_entries[aoffset] += af_bin_entries[offset];
00120       hdata.m_bin_Sw[aoffset] += af_bin_Sw[offset];
00121       hdata.m_bin_Sw2[aoffset] += af_bin_Sw2[offset];
00122       hdata.m_bin_Sxw[aoffset][0] += af_bin_Sxw[offset][1];
00123       hdata.m_bin_Sx2w[aoffset][0] += af_bin_Sx2w[offset][1];
00124     }
00125   }
00126   a_to.copy_from_data(hdata);
00127   a_to.update_fast_getters();  
00128   return true;
00129 }
00130 
00131 template <class TC,class TN,class TW,class TH>
00132 inline h1<TC,TN,TW,TH>* slice_y(const h2<TC,TN,TW,TH>& a_from,
00133                              int aIbeg,int aIend,
00134                              const std::string& a_title) {
00135   h1<TC,TN,TW,TH>* slice = new h1<TC,TN,TW,TH>(a_title,
00136                                    a_from.axis_y().bins(),
00137                                    a_from.axis_y().lower_edge(),
00138                                    a_from.axis_y().upper_edge());
00139   if(!fill_slice_y(a_from,aIbeg,aIend,*slice)) {delete slice;return 0;}
00140   return slice;
00141 }
00142 
00143 template <class TC,class TN,class TW,class TH>
00144 inline h1<TC,TN,TW,TH>* projection_y(const h2<TC,TN,TW,TH>& a_from,const std::string& a_title) {
00145   return slice_y(a_from,axis<TC>::UNDERFLOW_BIN,axis<TC>::OVERFLOW_BIN,a_title);
00146 }
00147 
00148 
00149 template <class TC,class TN,class TW,class TH>
00150 inline bool fill_slice_yz(const h3<TC,TN,TW,TH>& a_from,
00151                           int aIbeg,int aIend,h2<TC,TN,TW,TH>& a_to) {
00152 
00153   if(!a_from.dimension()) return false;
00154 
00155   typedef typename axis<TC>::bn_t bn_t;
00156 
00157   bn_t ibeg;
00158   if(!a_from.axis_x().in_range_to_absolute_index(aIbeg,ibeg)) return false;
00159   bn_t iend;
00160   if(!a_from.axis_x().in_range_to_absolute_index(aIend,iend)) return false;
00161   if(ibeg>iend) return false;
00162 
00163   if(a_from.axis_y().bins()!=a_to.axis_x().bins()) return false;
00164   if(a_from.axis_z().bins()!=a_to.axis_y().bins()) return false;
00165 
00166   histo_data<TC,TN,TW> hdata = a_to.get_histo_data();
00167   
00168   bn_t aibin,ajbin,aoffset,offset,ibin;     
00169   
00170   bn_t ayoffset = hdata.m_axes[1].m_offset;
00171   bn_t yoffset = a_from.axis_y().m_offset;
00172   bn_t zoffset = a_from.axis_z().m_offset;
00173   
00174   bn_t axbins = hdata.m_axes[0].bins()+2;
00175   bn_t aybins = hdata.m_axes[1].bins()+2;
00176 
00177   const std::vector<TN>& af_bin_entries = a_from.bins_entries();
00178   const std::vector<TW>& af_bin_Sw = a_from.bins_sum_w();
00179   const std::vector<TW>& af_bin_Sw2 = a_from.bins_sum_w2();
00180   const std::vector< std::vector<TC> >& af_bin_Sxw = a_from.bins_sum_xw();
00181   const std::vector< std::vector<TC> >& af_bin_Sx2w = a_from.bins_sum_x2w();
00182 
00183   // Fill also the outflow.
00184   for(aibin=0;aibin<axbins;aibin++) {
00185     for(ajbin=0;ajbin<aybins;ajbin++) {
00186       //offset2D = ibin + jbin * m_axes[1].m_offset
00187       aoffset = aibin + ajbin * ayoffset;
00188       for(ibin=ibeg;ibin<=iend;ibin++) {
00189         //offset3D = ibin + jbin * m_axes[1].m_offset + kbin*m_axes[2].m_offset;
00190         // hdata booked with y-z then :
00191         offset = ibin + aibin * yoffset + ajbin * zoffset;
00192 
00193         // Bin :
00194         hdata.m_bin_entries[aoffset] += af_bin_entries[offset];
00195         hdata.m_bin_Sw[aoffset] += af_bin_Sw[offset];
00196         hdata.m_bin_Sw2[aoffset] += af_bin_Sw2[offset];
00197         hdata.m_bin_Sxw[aoffset][0] += af_bin_Sxw[offset][1];
00198         hdata.m_bin_Sxw[aoffset][1] += af_bin_Sxw[offset][2];
00199         hdata.m_bin_Sx2w[aoffset][0] += af_bin_Sx2w[offset][1];
00200         hdata.m_bin_Sx2w[aoffset][1] += af_bin_Sx2w[offset][2];
00201       }
00202     }
00203   }
00204   a_to.copy_from_data(hdata);
00205   a_to.update_fast_getters();
00206   return true;
00207 }
00208 
00209 template <class TC,class TN,class TW,class TH>
00210 inline bool fill_slice_xy(const h3<TC,TN,TW,TH>& a_from,
00211                           int aKbeg,int aKend,h2<TC,TN,TW,TH>& a_to) {
00212   
00213   if(!a_from.dimension()) return false;
00214 
00215   typedef typename axis<TC>::bn_t bn_t;
00216 
00217   bn_t kbeg;
00218   if(!a_from.axis_z().in_range_to_absolute_index(aKbeg,kbeg)) return false;
00219   bn_t kend;
00220   if(!a_from.axis_z().in_range_to_absolute_index(aKend,kend)) return false;
00221   if(kbeg>kend) return false;
00222   
00223   if(a_from.axis_x().bins()!=a_to.axis_x().bins()) return false;
00224   if(a_from.axis_y().bins()!=a_to.axis_y().bins()) return false;
00225 
00226   histo_data<TC,TN,TW> hdata = a_to.get_histo_data();
00227   
00228   bn_t kbin;
00229   bn_t aibin,ajbin,aoffset,offset;     
00230   
00231   bn_t ayoffset = hdata.m_axes[1].m_offset;
00232   bn_t yoffset = a_from.axis_y().m_offset;
00233   bn_t zoffset = a_from.axis_z().m_offset;
00234   
00235   bn_t axbins = hdata.m_axes[0].bins()+2;
00236   bn_t aybins = hdata.m_axes[1].bins()+2;
00237 
00238   const std::vector<TN>& af_bin_entries = a_from.bins_entries();
00239   const std::vector<TW>& af_bin_Sw = a_from.bins_sum_w();
00240   const std::vector<TW>& af_bin_Sw2 = a_from.bins_sum_w2();
00241   const std::vector< std::vector<TC> >& af_bin_Sxw = a_from.bins_sum_xw();
00242   const std::vector< std::vector<TC> >& af_bin_Sx2w = a_from.bins_sum_x2w();
00243 
00244   // Fill also the outflow.
00245   for(aibin=0;aibin<axbins;aibin++) {
00246     for(ajbin=0;ajbin<aybins;ajbin++) {
00247       //offset2D = ibin + jbin * m_axes[1].m_offset
00248       aoffset = aibin + ajbin * ayoffset;
00249       for(kbin=kbeg;kbin<=kend;kbin++) {
00250         //offset3D = ibin + jbin * m_axes[1].m_offset + kbin*m_axes[2].m_offset;
00251         // hdata booked with x-y then :
00252         offset = aibin + ajbin * yoffset + kbin * zoffset;
00253 
00254         // Bin :
00255         hdata.m_bin_entries[aoffset] += af_bin_entries[offset];
00256         hdata.m_bin_Sw[aoffset] += af_bin_Sw[offset];
00257         hdata.m_bin_Sw2[aoffset] += af_bin_Sw2[offset];
00258         hdata.m_bin_Sxw[aoffset][0] += af_bin_Sxw[offset][0];
00259         hdata.m_bin_Sxw[aoffset][1] += af_bin_Sxw[offset][1];
00260         hdata.m_bin_Sx2w[aoffset][0] += af_bin_Sx2w[offset][0];
00261         hdata.m_bin_Sx2w[aoffset][1] += af_bin_Sx2w[offset][1];
00262       }
00263     }
00264   }
00265   a_to.copy_from_data(hdata);
00266   a_to.update_fast_getters();
00267   return true;
00268 }
00269 
00270 template <class TC,class TN,class TW,class TH>
00271 inline bool fill_slice_xz(const h3<TC,TN,TW,TH>& a_from,
00272                           int aJbeg,int aJend,h2<TC,TN,TW,TH>& a_to) {
00273   
00274   if(!a_from.dimension()) return false;
00275 
00276   typedef typename axis<TC>::bn_t bn_t;
00277 
00278   bn_t jbeg;
00279   if(!a_from.axis_y().in_range_to_absolute_index(aJbeg,jbeg)) return false;
00280   bn_t jend;
00281   if(!a_from.axis_y().in_range_to_absolute_index(aJend,jend)) return false;
00282   if(jbeg>jend) return false;
00283 
00284   if(a_from.axis_x().bins()!=a_to.axis_x().bins()) return false;
00285   if(a_from.axis_z().bins()!=a_to.axis_y().bins()) return false;
00286   
00287   histo_data<TC,TN,TW> hdata = a_to.get_histo_data();
00288 
00289   bn_t aibin,ajbin,aoffset,offset,jbin;     
00290   
00291   bn_t ayoffset = hdata.m_axes[1].m_offset;
00292   bn_t yoffset = a_from.axis_y().m_offset;
00293   bn_t zoffset = a_from.axis_z().m_offset;
00294   
00295   bn_t axbins = hdata.m_axes[0].bins()+2;
00296   bn_t aybins = hdata.m_axes[1].bins()+2;
00297 
00298   const std::vector<TN>& af_bin_entries = a_from.bins_entries();
00299   const std::vector<TW>& af_bin_Sw = a_from.bins_sum_w();
00300   const std::vector<TW>& af_bin_Sw2 = a_from.bins_sum_w2();
00301   const std::vector< std::vector<TC> >& af_bin_Sxw = a_from.bins_sum_xw();
00302   const std::vector< std::vector<TC> >& af_bin_Sx2w = a_from.bins_sum_x2w();
00303 
00304   // Fill also the outflow.
00305   for(aibin=0;aibin<axbins;aibin++) {
00306     for(ajbin=0;ajbin<aybins;ajbin++) {
00307       //offset2D = ibin + jbin * m_axes[1].m_offset
00308       aoffset = aibin + ajbin * ayoffset;
00309       for(jbin=jbeg;jbin<=jend;jbin++) {
00310         //offset3D = ibin + jbin * m_axes[1].m_offset + kbin*m_axes[2].m_offset;
00311         // hdata booked with x-z then :
00312         offset = aibin + jbin * yoffset + ajbin * zoffset;
00313   
00314         // Bin :
00315         hdata.m_bin_entries[aoffset] += af_bin_entries[offset];
00316         hdata.m_bin_Sw[aoffset] += af_bin_Sw[offset];
00317         hdata.m_bin_Sw2[aoffset] += af_bin_Sw2[offset];
00318         hdata.m_bin_Sxw[aoffset][0] += af_bin_Sxw[offset][0];
00319         hdata.m_bin_Sxw[aoffset][1] += af_bin_Sxw[offset][2];
00320         hdata.m_bin_Sx2w[aoffset][0] += af_bin_Sx2w[offset][0];
00321         hdata.m_bin_Sx2w[aoffset][1] += af_bin_Sx2w[offset][2];
00322       }
00323     }
00324   }
00325   
00326   a_to.copy_from_data(hdata);
00327   a_to.update_fast_getters();
00328 
00329   return true;
00330 }
00331 
00332 template <class TC,class TN,class TW,class TH>
00333 inline h2<TC,TN,TW,TH>* slice_xy(const h3<TC,TN,TW,TH>& a_from,
00334                              int aKbeg,int aKend,
00335                              const std::string& a_title) {
00336   h2<TC,TN,TW,TH>* slice = new h2<TC,TN,TW,TH>(a_title,
00337                                               a_from.axis_x().bins(),
00338                                               a_from.axis_x().lower_edge(),
00339                                               a_from.axis_x().upper_edge(),
00340                                               a_from.axis_y().bins(),
00341                                               a_from.axis_y().lower_edge(),
00342                                               a_from.axis_y().upper_edge());
00343   if(!fill_slice_xy(a_from,aKbeg,aKend,*slice)) {delete slice;return 0;}
00344   return slice;
00345 }
00346 
00347 template <class TC,class TN,class TW,class TH>
00348 inline h2<TC,TN,TW,TH>* slice_yz(const h3<TC,TN,TW,TH>& a_from,
00349                              int aIbeg,int aIend,
00350                              const std::string& a_title) {
00351   h2<TC,TN,TW,TH>* slice = new h2<TC,TN,TW,TH>(a_title,
00352                                               a_from.axis_y().bins(),
00353                                               a_from.axis_y().lower_edge(),
00354                                               a_from.axis_y().upper_edge(),
00355                                               a_from.axis_z().bins(),
00356                                               a_from.axis_z().lower_edge(),
00357                                               a_from.axis_z().upper_edge());
00358   if(!fill_slice_yz(a_from,aIbeg,aIend,*slice)) {delete slice;return 0;}
00359   return slice;
00360 }
00361 
00362 template <class TC,class TN,class TW,class TH>
00363 inline h2<TC,TN,TW,TH>* slice_xz(const h3<TC,TN,TW,TH>& a_from,
00364                              int aJbeg,int aJend,
00365                              const std::string& a_title) {
00366   h2<TC,TN,TW,TH>* slice = new h2<TC,TN,TW,TH>(a_title,
00367                                               a_from.axis_x().bins(),
00368                                               a_from.axis_x().lower_edge(),
00369                                               a_from.axis_x().upper_edge(),
00370                                               a_from.axis_z().bins(),
00371                                               a_from.axis_z().lower_edge(),
00372                                               a_from.axis_z().upper_edge());
00373   if(!fill_slice_xz(a_from,aJbeg,aJend,*slice)) {delete slice;return 0;}
00374   return slice;
00375 }
00376 
00377 }}
00378 
00379 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines