00001
00002 #include <cmath>
00003
00004
00005 #include "TClonesArray.h"
00006
00007
00008 #include "Conventions/PlaneView.h"
00009 #include "CandNtupleSR/NtpSREvent.h"
00010 #include "CandNtupleSR/NtpSRShower.h"
00011 #include "CandNtupleSR/NtpSRStrip.h"
00012 #include "CandNtupleSR/NtpSRTrack.h"
00013 #include "MessageService/MsgService.h"
00014 #include "StandardNtuple/NtpStRecord.h"
00015
00016
00017 #include "PhysicsNtuple/Basic.h"
00018 #include "PhysicsNtuple/Vertex.h"
00019
00020
00021 #include "FillBasic.h"
00022
00023 CVSID("$Id: FillBasic.cxx,v 1.10 2007/07/19 16:27:05 rustem Exp $");
00024
00025 using namespace std;
00026
00027
00028 Anp::FillBasic::FillBasic()
00029 :fCheck(true)
00030 {
00031 }
00032
00033
00034 Anp::FillBasic::~FillBasic()
00035 {
00036 }
00037
00038
00039 void Anp::FillBasic::Config(const Registry ®)
00040 {
00041 const char *value_char = 0;
00042 if(reg.Get("FillBasicCheck", value_char) && value_char)
00043 {
00044 if(strcmp(value_char, "yes") == 0)
00045 {
00046 fCheck = true;
00047 }
00048 else if(strcmp(value_char, "no") == 0)
00049 {
00050 fCheck = false;
00051 }
00052 }
00053 }
00054
00055
00056 const Anp::Basic Anp::FillBasic::Fill(const NtpStRecord &ntprec, const NtpSREvent &record)
00057 {
00058 Basic basic = Get(ntprec, record.stp, record.nstrip);
00059
00060 if(!Fill(basic, record.ph))
00061 {
00062 MSG("FillAlg", Msg::kWarning) << "Fill failed for NtpSREvent \n" << record;
00063 }
00064
00065 if(fCheck && !Check(basic, record.plane))
00066 {
00067 MSG("FillAlg", Msg::kWarning) << "NtpSREvent plane check failed \n" << record.plane;
00068 }
00069
00070 return basic;
00071 }
00072
00073
00074 const Anp::Basic Anp::FillBasic::Fill(const NtpStRecord &ntprec, const NtpSRShower &record)
00075 {
00076 Basic basic = Get(ntprec, record.stp, record.nstrip);
00077
00078 if(!Fill(basic, record.ph))
00079 {
00080 MSG("FillAlg", Msg::kWarning) << "Fill failed for NtpSRShower \n" << record;
00081 }
00082
00083 if(fCheck && !Check(basic, record.plane))
00084 {
00085 MSG("FillAlg", Msg::kWarning) << "NtpSRShower plane check failed \n" << record.plane;
00086 }
00087
00088 return basic;
00089
00090 }
00091
00092
00093 const Anp::Basic Anp::FillBasic::Fill(const NtpStRecord &ntprec, const NtpSRTrack &record)
00094 {
00095 Basic basic = Get(ntprec, record.stp, record.nstrip);
00096
00097 if(!Fill(basic, record.ph))
00098 {
00099 MSG("FillAlg", Msg::kWarning) << "Fill failed for NtpSREvent \n" << record;
00100 }
00101
00102 if(fCheck && !Check(basic, record.plane))
00103 {
00104 MSG("FillAlg", Msg::kWarning) << "NtpSRTrack plane check failed \n" << record.plane;
00105 }
00106
00107 return basic;
00108
00109 }
00110
00111
00112 const Anp::Vertex Anp::FillBasic::Fill(const NtpSRVertex &vtx) const
00113 {
00114 return Vertex(vtx.u, vtx.v, vtx.z, vtx.dcosu, vtx.dcosv, vtx.dcosz);
00115 }
00116
00117
00118 bool Anp::FillBasic::Fill(Basic &basic, const NtpSRStripPulseHeight &ph) const
00119 {
00120 basic.siglin = ph.siglin;
00121 basic.pe = ph.pe;
00122 basic.sigmap = ph.sigmap;
00123 basic.mip = ph.mip;
00124
00125 if(std::fabs(basic.adcu + basic.adcv - ph.raw) > 1.001)
00126 {
00127 static unsigned int count = 0;
00128 MSG("FillAlg", Msg::kWarning) << "Error " << ++count << " in NtpSRStripPulseHeight adc "
00129 << basic.adcu + basic.adcv << " - " << ph.raw << " = "
00130 << basic.adcu + basic.adcv - ph.raw << std::endl;
00131 return false;
00132 }
00133 if(std::fabs(basic.sigcoru + basic.sigcorv - ph.sigcor) > 1.001)
00134 {
00135 static unsigned int count = 0;
00136 MSG("FillAlg", Msg::kWarning) << "Error " << ++count << " in NtpSRStripPulseHeight sigcor "
00137 << basic.sigcoru + basic.sigcorv << " - " << ph.sigcor << " = "
00138 << basic.sigcoru + basic.sigcorv - ph.sigcor << std::endl;
00139 return false;
00140 }
00141
00142 return true;
00143 }
00144
00145
00146 bool Anp::FillBasic::Check(const Basic &basic, const NtpSRPlane &plane) const
00147 {
00148 bool result = true;
00149
00150 if(plane.nu + plane.nv != plane.n)
00151 {
00152 MSG("FillAlg", Msg::kWarning) << "Number of planes does not match" << endl;
00153 result = false;
00154 }
00155 if(basic.nuplane != short(plane.nu))
00156 {
00157 MSG("FillAlg", Msg::kWarning) << "Mismatched number of U planes" << endl;
00158 result = false;
00159 }
00160 if(basic.nvplane != short(plane.nv))
00161 {
00162 MSG("FillAlg", Msg::kWarning) << "Mismatched number of V planes" << endl;
00163 result = false;
00164 }
00165
00166 if(plane.beg < plane.end)
00167 {
00168 if(basic.beg_uplane != short(plane.begu))
00169 {
00170 MSG("FillAlg", Msg::kWarning) << "Mismatched beg U plane" << endl;
00171 result = false;
00172 }
00173 if(basic.beg_vplane != short(plane.begv))
00174 {
00175 MSG("FillAlg", Msg::kWarning) << "Mismatched beg V plane" << endl;
00176 result = false;
00177 }
00178 if(basic.end_uplane != short(plane.endu))
00179 {
00180 MSG("FillAlg", Msg::kWarning) << "Mismatched end U plane" << endl;
00181 result = false;
00182 }
00183 if(basic.end_vplane != short(plane.endv))
00184 {
00185 MSG("FillAlg", Msg::kWarning) << "Mismatched end V plane" << endl;
00186 result = false;
00187 }
00188 }
00189 else if(plane.beg > plane.end)
00190 {
00191 if(basic.beg_uplane != short(plane.endu))
00192 {
00193 MSG("FillAlg", Msg::kWarning) << "Mismatched beg U plane" << endl;
00194 result = false;
00195 }
00196 if(basic.beg_vplane != short(plane.endv))
00197 {
00198 MSG("FillAlg", Msg::kWarning) << "Mismatched beg V plane" << endl;
00199 result = false;
00200 }
00201 if(basic.end_uplane != short(plane.begu))
00202 {
00203 MSG("FillAlg", Msg::kWarning) << "Mismatched end U plane" << endl;
00204 result = false;
00205 }
00206 if(basic.end_vplane != short(plane.begv))
00207 {
00208 MSG("FillAlg", Msg::kWarning) << "Mismatched end V plane" << endl;
00209 result = false;
00210 }
00211 }
00212 else
00213 {
00214 MSG("FillAlg", Msg::kWarning) << "beg and end plane are equal" << endl;
00215 result = false;
00216 }
00217
00218 return result;
00219 }
00220
00221
00222 const Anp::Basic Anp::FillBasic::Get(const NtpStRecord &record, const int *index_array, int nstrip) const
00223 {
00224 Basic data;
00225
00226 if(nstrip < 1 || !index_array)
00227 {
00228 return data;
00229 }
00230
00231 const TClonesArray *strip_array = record.stp;
00232 if(!strip_array)
00233 {
00234 MSG("FillAlg", Msg::kWarning) << "NtpStRecord does not have valid strip array." << endl;
00235 return data;
00236 }
00237
00238 vector<short> uplane_vec, vplane_vec;
00239
00240 const int entries = strip_array -> GetEntries();
00241
00242 const Detector::Detector_t detector = record.GetHeader().GetVldContext().GetDetector();
00243
00244 bool init_time = false;
00245 for(int i = 0; i < nstrip; ++i)
00246 {
00247 const short index = index_array[i];
00248 if(index < 0 || index >= entries)
00249 {
00250 MSG("FillAlg", Msg::kWarning) << "NtpSRStrip index is out of range" << std::endl;
00251 continue;
00252 }
00253
00254 NtpSRStrip *ntpstp = GetStrip(record, index);
00255 if(!ntpstp)
00256 {
00257 continue;
00258 }
00259
00260 if(index != ntpstp -> index)
00261 {
00262 MSG("FillAlg", Msg::kWarning) << "Mismatched strip and TClonesArray index" << std::endl;
00263 continue;
00264 }
00265
00266 const short plane = ntpstp -> plane;
00267
00268 if(ntpstp -> planeview == PlaneView::kU)
00269 {
00270 if(std::find(uplane_vec.begin(), uplane_vec.end(), plane) == uplane_vec.end())
00271 {
00272 uplane_vec.push_back(plane);
00273 }
00274 }
00275 else if(ntpstp -> planeview == PlaneView::kV)
00276 {
00277 if(std::find(vplane_vec.begin(), vplane_vec.end(), plane) == vplane_vec.end())
00278 {
00279 vplane_vec.push_back(plane);
00280 }
00281 }
00282 else
00283 {
00284 MSG("FillAlg", Msg::kWarning) << "Unknown planeview " << int(ntpstp -> planeview) << endl;
00285 continue;
00286 }
00287
00288 if(detector == Detector::kNear)
00289 {
00290 if(ntpstp -> pmtindex0 > 0)
00291 {
00292 MSG("FillAlg", Msg::kWarning) << "In ND East PMT index is positive" << std::endl;
00293 continue;
00294 }
00295
00296 if(ntpstp -> planeview == PlaneView::kU)
00297 {
00298 ++data.nustrip;
00299 data.adcu += ntpstp -> ph1.raw;
00300 data.sigcoru += ntpstp -> ph1.sigcor;
00301 }
00302 else if(ntpstp -> planeview == PlaneView::kV)
00303 {
00304 ++data.nvstrip;
00305 data.adcv += ntpstp -> ph1.raw;
00306 data.sigcorv += ntpstp -> ph1.sigcor;
00307 }
00308
00309 if(!init_time)
00310 {
00311 data.max_time = ntpstp -> time1;
00312 data.min_time = ntpstp -> time1;
00313 init_time = true;
00314 }
00315 else
00316 {
00317 data.max_time = std::max(ntpstp -> time1, data.max_time);
00318 data.min_time = std::min(ntpstp -> time1, data.min_time);
00319 }
00320 }
00321 else if(detector == Detector::kFar)
00322 {
00323 if(ntpstp -> planeview == PlaneView::kU)
00324 {
00325 ++data.nustrip;
00326
00327 if(ntpstp -> pmtindex0 > 0 ||
00328 (ntpstp -> pmtindex0 == 0 && ntpstp -> ph0.raw > 0.0))
00329 {
00330 data.adcu += ntpstp -> ph0.raw;
00331 data.sigcoru += ntpstp -> ph0.sigcor;
00332 }
00333
00334 if(ntpstp -> pmtindex1 > 0 ||
00335 (ntpstp -> pmtindex1 == 0 && ntpstp -> ph1.raw > 0.0))
00336 {
00337 data.adcu += ntpstp -> ph1.raw;
00338 data.sigcoru += ntpstp -> ph1.sigcor;
00339 }
00340 }
00341 else if(ntpstp -> planeview == PlaneView::kV)
00342 {
00343 ++data.nvstrip;
00344
00345 if(ntpstp -> pmtindex0 > 0 ||
00346 (ntpstp -> pmtindex0 == 0 && ntpstp -> ph0.raw > 0.0))
00347 {
00348 data.adcv += ntpstp -> ph0.raw;
00349 data.sigcorv += ntpstp -> ph0.sigcor;
00350 }
00351
00352 if(ntpstp -> pmtindex1 > 0 ||
00353 (ntpstp -> pmtindex1 == 0 && ntpstp -> ph1.raw > 0.0))
00354 {
00355 data.adcv += ntpstp -> ph1.raw;
00356 data.sigcorv += ntpstp -> ph1.sigcor;
00357 }
00358 }
00359
00360
00361 if(ntpstp -> pmtindex0 > 0 ||
00362 (ntpstp -> pmtindex0 == 0 && ntpstp -> ph0.raw > 0.0))
00363 {
00364 if(!init_time)
00365 {
00366 data.max_time = ntpstp -> time0;
00367 data.min_time = ntpstp -> time0;
00368 init_time = true;
00369 }
00370 else
00371 {
00372 data.max_time = std::max(data.max_time, ntpstp -> time0);
00373 data.min_time = std::min(data.min_time, ntpstp -> time0);
00374 }
00375 }
00376
00377 if(ntpstp -> pmtindex1 > 0 ||
00378 (ntpstp -> pmtindex1 == 0 && ntpstp -> ph1.raw > 0.0))
00379 {
00380 if(!init_time)
00381 {
00382 data.max_time = ntpstp -> time1;
00383 data.min_time = ntpstp -> time1;
00384 init_time = true;
00385 }
00386 else
00387 {
00388 data.max_time = std::max(data.max_time, ntpstp -> time1);
00389 data.min_time = std::min(data.min_time, ntpstp -> time1);
00390 }
00391 }
00392
00393 if(!init_time)
00394 {
00395 MSG("FillAlg", Msg::kWarning) << "Failed to find initial time" << endl;
00396 continue;
00397 }
00398 }
00399 else
00400 {
00401 MSG("FillAlg", Msg::kWarning) << "Unknown detector type" << std::endl;
00402 }
00403 }
00404
00405 data.nuplane = uplane_vec.size();
00406 data.nvplane = vplane_vec.size();
00407
00408 if(!uplane_vec.empty())
00409 {
00410 data.beg_uplane = *std::min_element(uplane_vec.begin(), uplane_vec.end());
00411 data.end_uplane = *std::max_element(uplane_vec.begin(), uplane_vec.end());
00412 }
00413
00414 if(!vplane_vec.empty())
00415 {
00416 data.beg_vplane = *std::min_element(vplane_vec.begin(), vplane_vec.end());
00417 data.end_vplane = *std::max_element(vplane_vec.begin(), vplane_vec.end());
00418 }
00419
00420 if(detector == Detector::kNear)
00421 {
00422 short min_plane = -1, max_plane = -1, count = 0;
00423
00424 for(vector<short>::const_iterator it = uplane_vec.begin(); it != uplane_vec.end(); ++it)
00425 {
00426 const short plane = *it;
00427 if(plane > 120)
00428 {
00429 break;
00430 }
00431
00432 ++count;
00433
00434 if(min_plane < 0 || min_plane > plane)
00435 {
00436 min_plane = plane;
00437 }
00438 if(max_plane < 0 || max_plane < plane)
00439 {
00440 max_plane = plane;
00441 }
00442 }
00443
00444 for(vector<short>::const_iterator it = vplane_vec.begin(); it != vplane_vec.end(); ++it)
00445 {
00446 const short plane = *it;
00447 if(plane > 120)
00448 {
00449 break;
00450 }
00451
00452 ++count;
00453
00454 if(min_plane < 0 || min_plane > plane)
00455 {
00456 min_plane = plane;
00457 }
00458 if(max_plane < 0 || max_plane < plane)
00459 {
00460 max_plane = plane;
00461 }
00462 }
00463
00464 if(max_plane - min_plane > 0)
00465 {
00466 data.active_frac = double(count)/double(1 + max_plane - min_plane);
00467 }
00468 }
00469 else
00470 {
00471 short min_plane = data.beg_uplane;
00472 short max_plane = data.end_uplane;
00473
00474 if(min_plane > data.beg_vplane)
00475 {
00476 min_plane = data.beg_vplane;
00477 }
00478 if(max_plane < data.end_vplane)
00479 {
00480 max_plane = data.end_vplane;
00481 }
00482
00483 const double nactive = uplane_vec.size() + vplane_vec.size();
00484 if(max_plane - min_plane > 0)
00485 {
00486 data.active_frac = nactive/double(max_plane - min_plane);
00487 }
00488 }
00489
00490 return data;
00491 }
00492
00493
00494 NtpSRStrip* Anp::FillBasic::GetStrip(const NtpStRecord &record, const int index) const
00495 {
00496 const TClonesArray *strip_array = record.stp;
00497 if(!strip_array)
00498 {
00499 MSG("FillAlg", Msg::kWarning) << "NtpStRecord does not have valid strip array." << endl;
00500 return 0;
00501 }
00502
00503 NtpSRStrip *ntpstp = dynamic_cast<NtpSRStrip *>(strip_array -> At(index));
00504 if(!ntpstp)
00505 {
00506 MSG("FillAlg", Msg::kWarning) << "Could not find NtpSRStrip at " << index << endl;
00507 return 0;
00508 }
00509
00510 return ntpstp;
00511 }
00512
00513
00514 void Anp::FillBasic::Print(const NtpStRecord &record, const int *index_array, int nstrip) const
00515 {
00516 if(nstrip < 1 || !index_array)
00517 {
00518 return;
00519 }
00520
00521 const TClonesArray *strip_array = record.stp;
00522 if(!strip_array)
00523 {
00524 MSG("FillAlg", Msg::kError) << "NtpStRecord does not have valid strip array." << endl;
00525 return;
00526 }
00527
00528 const int entries = strip_array -> GetEntries();
00529
00530 double raw_east = 0.0, raw_west = 0.0;
00531
00532 for(int i = 0; i < nstrip; ++i)
00533 {
00534 const short index = index_array[i];
00535 if(index < 0 || index >= entries)
00536 {
00537 MSG("FillAlg", Msg::kWarning) << "NtpSRStrip index is out of range" << std::endl;
00538 continue;
00539 }
00540
00541 NtpSRStrip *ntpstp = GetStrip(record, index);
00542 if(!ntpstp)
00543 {
00544 continue;
00545 }
00546
00547 if(index != ntpstp -> index)
00548 {
00549 MSG("FillAlg", Msg::kWarning) << "Mismatched strip and TClonesArray index" << std::endl;
00550 continue;
00551 }
00552
00553 if(ntpstp -> pmtindex0 >= 0 && ntpstp -> ph0.raw > 0.0)
00554 {
00555 raw_east += ntpstp -> ph0.raw;
00556 }
00557 if(ntpstp -> pmtindex1 >= 0 && ntpstp -> ph1.raw > 0.0)
00558 {
00559 raw_west += ntpstp -> ph1.raw;
00560 }
00561
00562 cout << "----------------------------------------------------------------------------" << endl;
00563 cout << "NtpSRStrip #" << i + 1 << " raw (east, west) = ("
00564 << ntpstp -> ph0.raw << ", " << ntpstp -> ph1.raw << ")" << endl;
00565 ntpstp -> Print();
00566 }
00567
00568 cout << "total raw (east, west, total) = (" << raw_east << ", " << raw_west << ", "
00569 << raw_east + raw_west << ")" << endl;
00570 }